I'm actually using chart.js to make small chart like this :
I have some integer values in y-axis and date in x-axis. Here you can see an example of my dataset : dataset
I want to add a new line, with as a value : the sum of the values at each date. My datasets are dynamically generated. With the dataset I uploaded, the data of this line should be
0: Object { x: "2021-09-02", y: 20 }
1: Object { x: "2021-09-08", y: 13 }
2: Object { x: "2021-09-08", y: 10 }
Can someone help me to get the loop which will sum that please ?
You can try this solutio
var array = [
{ x: "2021-09-02", y: 20 },
{ x: "2021-09-08", y: 13 },
{ x: "2021-09-08", y: 10 },
];
var result = [];
array.reduce(function(res, value) {
if (!res[value.x]) {
res[value.x] = { x: value.x, y: 0 };
result.push(res[value.x])
}
res[value.x].y += value.y;
return res;
}, {});
console.log(result);