问题描述
我目前正在进行D3数据可视化的第一步。到目前为止,D3教程已经帮助了我很多。饼图教程虽然不进一步解释饼图的必要数据结构。
I am currently taking my first steps in D3 data visualisation. So far, the D3 tutorials have helped me a lot. The pie chart tutorial http://bl.ocks.org/mbostock/3887235 though does not further explain the necessary data structure for pie charts.
我的数据比示例的标签/值结构更复杂。我有年度总导入数据和导入数据的特定商品存储在JSON:
My data is more complex than the label/value structure of the example. I have the annual total import data and import data of a specific good stored in JSON:
var data = [{"year":"2001","total_import":"100000","import_specific_good":"25000"},{"year":"2002",...}];
如果我理解教程正确pie()迭代每个DIFFERENT对象的SAME条目。
If I understand the tutorial correctly pie() iterates over the SAME entry of each DIFFERENT object.
如果我需要SAME物件的特定DIFFERENT值,该怎么办?
What if I need specific DIFFERENT values of the SAME object?
所有年度总进口作为部分,而每年进口特定商品作为年度总进口的一部分。我的值将是1.(total_import - import_specific_good)和2. import_specific_good。
I am not interested in a pie showing all annual total imports as portions, but the annual import of a specific good as a portion of the annual total import. My values would be 1. (total_import - import_specific_good) and 2. import_specific_good.
我建议的数据结构是否正确,我想做什么?
Is my proposed data structure correct for what I want to do? Or do I have to restructure everything so that the values for every year are stored in a separate variable?
var data_2001 = [{"label":"Total Import","value":"100000"},{"label":"Import of Specific Good","value":"25000"}];
var data_2002 = [{"label": ...}];
推荐答案
您不必使用特定的数据结构 - 你可以(并将需要反正)修改示例,所以你可以使用任何你喜欢的!所以你可以使用你的第一个JSON就好了。因为你只有两个值可以显示,你可以简单的构造结构传递到 .data()
即时。
You don't have to use a specific data structure -- you can (and will need to anyway) modify the example so you can use anything you like! So you can use your first JSON just fine. As you have only 2 values to show, you could simply construct the structure to pass to .data()
on the fly.
// for pie chart
...data([json[index].total_import - json[index].import_specific_good,
json[index].import_specific_good])...
...
// similarly for the labels
我建议您在JSON中将数字存储为数字(例如,不使用引号),否则您必须将它们转换为Javascript中的数字。
I would advise to store the numbers as numbers (e.g. without quotes) in your JSON though -- otherwise you'll have to convert them to numbers in Javascript.
这篇关于D3饼图的数据结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!