Chartjs-How can I build some chart with Chart.js and ng2-charts using an array of revenus & budgets per month date as Data source?

0👍

Shalom Eli, you should map your original data to the chart’s data arrays: week.map(e => e.revenu) for the revenu dataset, and week.map(e => e.budges) for the budget dataset.

Update

Based on your comment, you want to convert your week array to a map keyed by the day of the month of each record:

week.reduce((acc,v)=> ({
  ...acc,
  [new Date(v.date).getDate()]: v
}), {})

And then use your labels array to fetch each value from this map:

revenuData = labels.map(r => this.weekMap[+r] && this.weekMap[+r].revenu);
budgetData = labels.map(r => this.weekMap[+r] && this.weekMap[+r].budget);

See this updated stackblitz: https://stackblitz.com/edit/ng2-charts-line-template-3dfmxn

enter image description here

Leave a comment