Skip to content

Commit 50da22d

Browse files
fix: render lazy charts already in viewport on page load (#1287)
The scroll listener for lazy charts never fired if the chart was already visible on page load, causing it to remain blank until the user scrolled. Extracted the logic into a named function with a viewport bounds check and call it once at page load in addition to on scroll.
1 parent 34f994f commit 50da22d

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

js/render-facade.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,21 +129,33 @@ window['vizClipboard2'] = window['vizClipboard2'] || null;
129129
}
130130

131131
function displayChartsOnFrontEnd() {
132-
$(window).on('scroll', function() {
132+
function renderVisibleLazyCharts() {
133133
$('div.visualizer-front:not(.viz-facade-loaded):not(.visualizer-lazy):not(.visualizer-cw-error):empty').each(function(index, element){
134134
// Do not render charts that are intentionally hidden.
135135
const style = window.getComputedStyle(element);
136136
if (style.display === 'none' || style.visibility === 'hidden') {
137137
return;
138138
}
139139

140+
// Only render charts that are currently within the viewport.
141+
const rect = element.getBoundingClientRect();
142+
const inViewport = rect.bottom >= 0 && rect.top <= (window.innerHeight || document.documentElement.clientHeight);
143+
if (!inViewport) {
144+
return;
145+
}
146+
140147
const id = $(element).addClass('viz-facade-loaded').attr('id');
141148
setTimeout(function(){
142149
// Add a short delay between each chart to avoid overloading the browser event loop.
143150
showChart(id);
144151
}, ( index + 1 ) * 100);
145152
});
146-
});
153+
}
154+
155+
$(window).on('scroll', renderVisibleLazyCharts);
156+
157+
// Run once on page load to render any lazy charts already in the viewport.
158+
renderVisibleLazyCharts();
147159

148160
$('div.visualizer-front-container:not(.visualizer-lazy-render)').each(function(index, element){
149161
// Do not render charts that are intentionally hidden.

0 commit comments

Comments
 (0)