Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

364
Views
Chart.js : How to get a line created by sum of others line?

I'm actually using chart.js to make small chart like this :

screenshot

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 ?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!