Attempting to enhance my Chart.js line chart by fetching more data or utilizing cached backup data during onZoom/onPan events has proven quite challenging.
The original code base is too intricate to share entirely, but I will outline the approaches I have taken so far.
1st approach: Using Chartjs-plugin-zoom
for the onZoom
and onPan
callbacks. However, these callbacks do not provide any context (chart, x, y) during panning/zooming and are not functioning correctly. More details can be found here.
A pull request has been created to address some of these issues which is currently pending: Link
2nd approach: Attempting to modify data in the Chart.js chart using the beforeUpdate
callback, but encountering various challenges. The graph initially displays around 1000 data points, updating every second. To handle zooming out, a backup data set containing additional data beyond the initial 1000 points is being considered. When zoomed out, the data set should expand based on changes in left and right coordinates detected in the beforeUpdate
callback.
scales: {
xAxes: [{
id: 'x-axis-0',
type: 'time',
beforeUpdate: function (chart) {
const scale = chart.chart.scales['x-axis-0']
if (scale.margins) {
const left = scale.getValueForPixel(scale.left)
const right = scale.getValueForPixel(scale.right)
const diffSec = (right - left) / 1000;
// Code snippet continued...
}
}
}
The second method is resulting in errors such as:
TypeError: Cannot read property 'hidden' of undefined
Would appreciate insights into a potential 3rd solution.
EDIT: Experimentation with different versions has led to a semi-functional state where the first solution appears to be working. However, a new issue has arisen:
onZoom
is triggered continuously while zooming instead ofFunction called once zooming is completed
as indicated in the Readme, rendering it ineffective for fetching data from an API.