Chartjs-How to mapping variable data for pie chart (react.js)

0👍

Since this.state.list is a list of objects, so you can simply use map method to loop through each object https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map. Then create new object with custom value.

let new_series = [];
this.state.list.map((obj) => {
    let info = {
        name: obj.channel_name + obj.channel_number, //from your code
        data: obj.VALUE //from your code
    }
    new_series.push(info)
});

Then assign new list to your chart.

//chart data
var data = {
  categories: ['June, 2015'],
  series: new_series
  ]
};

Leave a comment