简体   繁体   English

如何在c3js饼图上绘制自定义JSON数据?

[英]how to plot customized JSON data on c3js pie chart?

I want to plot C3js pie chart based on the following JSON (received from HQL-hibernate Java Play framework)... JSON looks like this (This is dynamic data): 我想基于以下JSON(从HQL-hibernate Java Play框架接收)绘制C3js饼图... JSON看起来像这样(这是动态数据):

{"ug":["K","M2","M3","M4"],"wtv":[10,20,35,60]}

HTML: HTML:

<div id="chart"></div>

JS (what I actually tried): JS(我实际尝试过的):

var json ={"ug":["K","M2","M3","M4"],"wtv":[10,20,35,60]};
var ug =json.ug;
var wtv=json.wtv;     
var chart = c3.generate({
    data: {
       json: {
          ug:wtv[0],
          ug1:wtv[1],
          ug2:wtv[2],
          ug3:wtv[3],
        },
        type : 'pie',
    }
});

Here is a fiddle for reference. 这是一个小提琴供参考。

In places of ug,ug1,ug2,ug3 I need K,M2,M3,M4(To be noted these are dynamic data) Can anyone help me to manipulate the data to draw the pie chart as required? 在ug,ug1,ug2,ug3的地方,我需要K,M2,M3,M4(需要注意的是,这些是动态数据)有人可以帮助我根据需要操纵数据以绘制饼图吗? I hope changing the structure of JSON at the back end is not the only solution (it is very difficult in my case though). 我希望在后端更改JSON的结构不是唯一的解决方案(尽管对我而言这很困难)。 Any other solutions are appreciated. 任何其他解决方案都值得赞赏。

so long as "ug" and "wtv" have the same number of elements... 只要"ug""wtv"具有相同数量的元素...

var json = {"ug":["K","M2","M3","M4"],"wtv":[10,20,35,60]};
var pieJson = {};
json.ug.forEach(function (ug, index) {
  pieJson[ug] = json.wtv[index];
});

var chart = c3.generate({
  data: {
    // iris data from R
    json: pieJson,
    type : 'pie',
  }
});

jsfiddle.net/4h4z00g7/ jsfiddle.net/4h4z00g7/

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

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