[英]How do you call data from nested JSON array using d3?
I have nested arrays in my json data, but I don't know how to call them in the d3 (beginner). 我在json数据中嵌套了数组,但是我不知道如何在d3(初学者)中调用它们。
In the example below, I am trying to build an SVG bar chart, using data from the "January" array, nested in the "meals" array in Json. 在下面的示例中,我试图使用嵌套在Json的“ meals”数组中的“ January”数组中的数据来构建SVG条形图。
The Json looks like this: Json看起来像这样:
{
"meals":[
{"january":[
{},{}
]},
{"february":[
{},{}
]},
{"march":[
{},{}
]},
}
And the d3 code looks like this. d3代码如下所示。 "chart" takes the user input of a drop down menu.
“图表”接受下拉菜单的用户输入。 In this case, it basically returns "meals":
在这种情况下,它基本上返回“ meals”:
var chart = selection.options[selection.selectedIndex].id;
var dataset = data[chart];
var svg = d3.select ("body")
.append ("svg")
.attr("width", w)
.attr("height", svgHeight+30);
svg.selectAll("rect")
.data(dataset.january) //***HERE is where I'm having trouble***
.enter()
.append("rect")
d3.nest()
can convert data like that into parseable arrays. d3.nest()
可以将类似的数据转换为可解析的数组。 These 'tutorial' examples are great: http://bl.ocks.org/phoebebright/raw/3176159/ 这些“教程”示例很棒: http : //bl.ocks.org/phoebebright/raw/3176159/
Given that it's feasible with your project, it might otherwise be better to simply change your data formatting for an easier traversal eg 鉴于您的项目可行,否则最好简单地更改数据格式以方便遍历,例如
{
"meals":[
{"name":"salad","month":"january"},
{"name":"burger","month":"february"}, //etc...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.