{
"name": "Max",
"value": 107,
"children": [
{
"name": "Don",
"value": 60,
"children" [
{"name": "CC", "value": 25},
{"name": "Jim", "value": 35}
]
},
{
"name": "David",
"value": 47,
"children": [
{"name": "Jeff", "value": 32},
{"name": "Buffy", "value": 15}
]
}
]
}
如何訪問最內層的兒童名稱與D3?Json d3訪問每個對象
我試着這樣做:
的.text(函數(d){返回d.children空:d.name;});
但它沒有工作....
當我做
的.text(函數(d){回報d.name});
它只顯示外部循環的名稱 - > Don,David。
d3.json('flare.json', function (data) {
var canvas = d3.select('p1')
.append('svg')
.attr('width', 800)
.attr('height', 800)
var color = d3.scale.category20c();
var data1 = data.children;
canvas.selectAll('text')
.data(data1)
.enter()
.append('text')
.attr('x', function (d) { return 2; })
.attr('y', function (d, i) { return i * 15; })
.attr('fill', 'black')
.style('font-size', '12px')
.text(function (d) { return d.children ? null: d.name; })
數據我之前↓↓
{
"name": "Don",
"value": 75,
"children" [
{"name": "CC", "value": 25},
{"name": "Jim", "value": 35}
]
}
當數據在這個單一的嵌套形式有,我的代碼工作完美,但是當我做了這雙窩,它不再工作
你可以發佈其餘的代碼? –