Chartjs-How to create an array of objects dynamically in javascript for a pie chart

1👍

You need to make a couple of changes to your code to make this work.

pieData should be an array of objects. So instead of appending a string, you need to push the object into the array. Once you do that that elements is an array of objects and you can directly assign it to pieData

Below is the changed code block

for (var i = 0; i < jsonData.length; i++) {
    counter = jsonData[i];
    var sale = roundFloat(counter.sales, 2);
    elements.push({
        value: sale,
        color: "#FDB45C",
        highlight: "#FFC870",
        label: counter.sku
    })
}
var pieData = elements;

Leave a comment