简体   繁体   English

如何使用列表数据创建 Javascript 饼图?

[英]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"}?我如何最好地将我的数据转换为 {y: data, label: "data"}?

or would I be best using a different Javascript pie chart which supports the format of my data better?或者我最好使用不同的Javascript饼图来更好地支持我的数据格式?

try this尝试这个

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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