简体   繁体   中英

How to create a Javascript pie chart with list data?

So my data is currently in the format of

[
    {
        "B": 1
    },
    {
        "A": 1
    },
    {
        "A": 1
    }
]

To which I want to create a pie chart with. However the sample pie chart I have used online expects data like this.

        [
            {y: 1, label: "User Group A"},
            {y: 1, label: "User Group B"},
        ]

This is the pie chart code I found online

var chart = new CanvasJS.Chart("chartContainer", {
    animationEnabled: true,
    title: {
        text: "Did user group view page on ebay?"
    },
    data: [{
        type: "pie",
        startAngle: 240,
        yValueFormatString: "##0.00\"%\"",
        indexLabel: "{label} {y}",
        dataPoints: [
            {y: 1, label: "User Group A"},
            {y: 1, label: "User Group B"},
        ]
    }]
});
chart.render();

How would I best convert my data to {y: data, label: "data"}?

or would I be best using a different Javascript pie chart which supports the format of my data better?

try this

 [ { "B": 1 }, { "A": 1 }, { "A": 1 } ].map(data => { let key = Object.keys(data)[0] return {y : data[key], label: key} })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM