I am trying to plot a graph using chart JS that has around 300,000 data points. It runs pretty slowly so I'm trying to improve its performance by using the data decimation plugin. However, the data isn't being decimated at all.
Sample of my code:
const dataset = {
datasets: {[,
data : [{
x : timetag,
y : data,
}],
id : 'id',
label: 'label',
indexAxis: 'x'
,]}
const config = {
type: 'line',
data: dataset,
options: {
// parsing: false,
animation: false,
scales : {
x: {
type: 'time',
title: {
display: true,
text: 'Time Of Day'},
},
y:{
min : 0,
},
}}};
const myChart = new Chart(
document.getElementById("Chart"),
config
);
function decimateData(myChart) {
myChart.options.plugins.decimation.algorithm = 'lttb';
myChart.options.plugins.decimation.enabled = true;
myChart.options.plugins.decimation.samples = 50;
myChart.update()
}
sample of the data structure
[
{
"x": "2022-02-24 00:00:26",
"y": "11"
},
{
"x": "2022-02-24 00:00:27",
"y": "7"
},
{
"x": "2022-02-24 00:00:28",
"y": "8"
}
]
1,2,3 are fulfilled I believe. 4, turning on parsing: false in the config stops my plot from working altogether.
What is incorrect about my data structure that wont allow chart js to read it.
Any guidance is much appreciated thanks
Update
Here is the function I used to create the data options in the config block:
function genDatasets() {
base = {
datasets: [],
parsing: false,,
fill: false};
for (var i = 0; i < sources.length; i++){
set =[{
data : [{
x : timetags,
y : data,
}],
id : sources[i].slice(0,-4),
label: sources[i].slice(0,-4),
borderColor: colours[i],
indexAxis: 'x'
,}]
base['datasets'].push(set[0]) ;
}
return base;
};
Look at the documentation about parsing:
https://www.chartjs.org/docs/latest/api/interfaces/ParsingOptions.html#parsing
How to parse the dataset. The parsing can be disabled by specifying parsing: false at chart options or dataset. If parsing is disabled, data must be sorted and in the formats the associated chart type and scales use internally.
Solution: your x-axis data (timetags) need to be "milli-second from epoch", which is the internal representation chartjs use for time value. Currently your x-axis data is a date string, that's why it's not working