【发布时间】:2015-01-13 19:58:35
【问题描述】:
我正在尝试使用以下嵌套的 json 文件创建一个简单的 d3 条形图 (http://bost.ocks.org/mike/bar/2/)。
{
"clustername": "cluster1",
"children": [
{
"neighborhoodname": "Shaw",
"children": [
{
"totpop2000": "1005",
"children": [
{
"name": "demographic",
"children": [
{"name": "Black", "size2000":0.18},
{"name": "White", "size2000":0.6},
{"name": "Hispanic", "size2000":0.40},
{"name": "Asian", "size2000":0.10}
]
},
{
"name": "wellbeing",
"children": [
{"name": "Unemployment", "size2000":0.40},
{"name": "Poverty", "size2000":0.1},
{"name": "Without HS Education", "size2000":0.31}
]
},
{
"name": "crime",
"children": [
{"name": "Violent Crime", "size2000":0.09},
{"name": "Property Crime", "size2000":0.08}
]
},
{
"name": "housing",
"children": [
{"name": "Home Ownership", "size2000":0.34},
{"name": "Houses for Rent", "size2000":0.50}
]
}
]
}
]
}
]
}
到目前为止,我的 javascript 包括以下内容,用于将标记为 size2000 的金额添加到 svg 但是当我运行代码时,我似乎无法附加适当的数据数组(即我得到第一个数组/@ 987654324@) 在我的 json 文件中。
var data = d3.json("data/test.json", function(json) {
console.log(json);
var bar = chart2000.selectAll("g")
.data(data)
.enter().append("g")
.attr("transform", function(d, i) { return "translate(0," + i * height + ")"; });
bar.append("rect")
.attr("width", function(d) { return x((d.size2000)*100); })
.attr("height", height - 1);
bar.append("text")
.attr("x", function(d) { return x((d.size2000)*100) - 3; })
.attr("y", height / 2)
.attr("dy", ".35em")
.text(function(d) { return (d.size2000)*100); });
});
有没有办法保留 json 数据结构并将 size2000 值附加到 svg?如果有,怎么做?
【问题讨论】:
标签: javascript arrays json d3.js appendchild