Reputation: 73
I have too many points on x-axis in my chart. I would like to limit maximum of displayed points to 100. But at the same time still displaying same chart. Exactly same chart - but with fewer points.
screenshot of my chart with too many points
I have no idea how to do it in PHP at server side so I was thinking if there is any solution on client side in chart.js?
Upvotes: 6
Views: 6430
Reputation: 683
For large datasets, chart.js
does offer a data decimation plugin that automatically decimates data at the start of the chart lifecycle, reducing the number of data points drawn.
According to chart.js
docs, to use the decimation plugin, the following requirements must be met:
indexAxis
of 'x'
The dataset must be a line'linear'
or 'time'
type axisparsing
must be false
dataset._data
and then defines a new data
property on the dataset.Your chart options should look something like this:
options: {
parsing: false,
plugins: {
decimation: {
enabled: false,
algorithm: 'min-max',
},
},
scales: {
x: {
type: 'time',
ticks: {
source: 'auto',
autoSkip: true,
}
}
}
Remember also that if you disable parsing the data you pass should be in the right format for the type of chart you chose, so epoch timestamps in this case. More info here.
Finally, you can find a working example here.
Upvotes: 1
Reputation: 1788
It's your responsibility to give chart.js
the points it should display. You have to limit the arrays with the labels and the data to 100 datapoints. Use every second data or so. Just do it in plain JavaScript. There's no function in chart.js
for that.
Upvotes: 2