I am trying to fetch more data to my chartjs line chart (or use some backup data cached) onZoom/onPan, but having many issues doing this.
My initial code base is very complex which I can't copy here completely but I'll share what I tried already.
1st solution: Chartjs-plugin-zoom
shares onZoom
and onPan
callbacks as mentioned here
but these doesn't provide any context (chart, x, y) with these call backs, and calls while panning/zooming and it is not even correct.
This PR addresses few issues but it is not merged yet: Link
2nd solution: I am trying to get change data in chart js in beforeUpdate
callback, but it is also throwing many issues to me.
I am showing around 1000 datapoints in a graph by default and as my graph is updating every second so what I am trying to do is to have a backup of data which will contain passed datapoints outside of 1000 datapoints, so when someone zoomOut I am checking the coordinates of left and right and if these are changed (increase) I want to increase data in the dataset in 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;
if (diffSec < 240) {
chart.chart.options.scales.xAxes[0].time.unit = 'second'
} else if (diffSec < 4*3600) {
chart.chart.options.scales.xAxes[0].time.unit = 'minute'
} else {
chart.chart.options.scales.xAxes[0].time.unit = 'minute'
chart.chart.options.scales.xAxes[0].time.stepSize = 10
}
}
// Ignore this if condition it is just for testing/debugging
if (chart.chart.data.labels.length > 1) {
// there are three line in graph
chart.chart.data.datasets[0].data = getMinArray(diffSec)
chart.chart.data.datasets[1].data = getAvgArray(diffSec)
chart.chart.data.datasets[2].data = getMaxArray(diffSec)
chart.chart.data.labels = getLabels(diffSec)
chart.update()
}
}
}
In second method I am getting more errors like:
TypeError: Cannot read property 'hidden' of undefined
Do you've a 3rd solution? please throw it below.
EDIT: I've tried with different versions
"chart.js": "2.8.0",
"chartjs-plugin-zoom": "^0.7.0",
It seems that 1st solution is working now, but there is another problem:
onZoom
is being called while zooming rather thanFunction called once zooming is completed
as mentioned in Readme so this wont really help in fetching data from API.