When looking to see if Highcharts had the ability to animate in the series when the user scrolled to that part of the page, I came across this function that seemed to work great. I've rewritten it to use the Intersection Observer API instead of attaching to a scroll event. Here is the JSFiddle demo, with relevant code below.
(function (H) {
var pendingRenders = [];
H.wrap(H.Series.prototype, 'render', function deferRender(proceed) {
var series = this,
renderTo = this.chart.container.parentNode;
if (!series.options.animation) {
proceed.call(series);
} else {
pendingRenders.push({
element: renderTo,
appear: function () {
proceed.call(series);
}
});
}
});
function observerSetup() {
const observer = new IntersectionObserver(entries => {
entries.forEach((entry,i) => {
if (entry.isIntersecting) {
const pendingRender = pendingRenders.find(r => r.element === entry.target)
pendingRender.appear();
observer.unobserve(entry.target);
}
});
});
pendingRenders.forEach(item => observer.observe(item.element));
}
document.addEventListener("DOMContentLoaded", observerSetup);
}(Highcharts));
This works great if you manually set the data like in the example. However, this no longer works if you use the data module to load in data via Google Sheets. In the JSFiddle Demo, you can see that the first chart no longer loads when the data is set to come from Google Sheets.
I believe the issue is that I'm not hooking into the right Highcharts class (H.Series.prototype
), but I'm not sure what to do change it to in order to get this to work with the Data module.
Any help is greatly appreciated!