简体   繁体   English

json对象的d3.js图表

[英]d3.js chart from json object

I'd like to create a simple pie chart in d3.js. 我想在d3.js中创建一个简单的饼图。 The request response that I'm working with looks like this: 我正在使用的请求响应如下所示:

{"record":[{"status":"request"},{"status":"requirements"},{"status":"request"}]};

From what I've seen in examples, I need to convert that object to this: 从我在示例中看到的,我需要将该对象转换为:

[{"label":"Request", "value":2}, {"label":"Requirements", "value":1}];

I'm happy to use d3 functions or underscore or straight javascript to accomplish this, I just haven't been able to crack it on my own. 我很乐意使用d3函数或下划线或直接的javascript来完成此操作,但我只是无法自行破解它。 Any help would be much appreciated! 任何帮助将非常感激!

You can use d3.nest() to reformat your data: 您可以使用d3.nest()重新格式化数据:

var data = d3.nest()
    .key(function(d){ return d.status; })
    .rollup(function(leaves){ return leaves.length; })
    .entries(input.record)
    .map(function(d){ return {label: d.key, value: d.values}; });

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

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