pie chart from json using c3 js

岁酱吖の 提交于 2019-12-03 11:37:56

问题


The code is taken as an example..

I need to generate a pie chart having 4 divisions (site1,site2...)each division correspond to its respective upload value.

In the above code i am not able to achieve this(I have specified value:['upload'])...

what is the exact value i have to specify?

Thanks..

  chart = c3.generate({
            data: {
                json: [
                    {name: 'www.site1.com', upload: 200},
                    {name: 'www.site2.com', upload: 100},
                    {name: 'www.site3.com', upload: 300},
                    {name: 'www.site4.com', upload: 400},
                ],
                keys: {
    //                x: 'name', // it's possible to specify 'x' when category axis
                    value: ['upload'],
                },
                type:'pie'
            },
            axis: {
                x: {
    //                type: 'category'
                }
            }
        });

回答1:


The pie chart maps each property to a pie sector. You could reformat your JSON like

var jsonData = [
    {name: 'www.site1.com', upload: 200},
    {name: 'www.site2.com', upload: 100},
    {name: 'www.site3.com', upload: 300},
    {name: 'www.site4.com', upload: 400}
]

var data = {};
var sites = [];
jsonData.forEach(function(e) {
    sites.push(e.name);
    data[e.name] = e.upload;
})    

chart = c3.generate({
    data: {
        json: [ data ],
        keys: {
            value: sites,
        },
        type:'pie'
    },
});

Working fiddle - http://jsfiddle.net/2nf9a7x4/



来源:https://stackoverflow.com/questions/30886203/pie-chart-from-json-using-c3-js

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!