我知道,在d3.js v4中,通过使用d3.v4层次结构(),父级的值可能大于其子级的总和.sum()
我也知道hierarchy()。sum()函数将返回一个值,该值是节点及其子节点的总和。
但是,我只想绘制仅包含其值的相应矩形(不是其值与其子项之和的总和)
有可能吗?
例如,这是JSON文件:
{"name":"Top Level", "size": "50000" ,"children":[{"name":"Level 2: A","size":"10000","children":[{"name":"Son of A","size":"2000"},{"name":"Daughter of A","size":"3000"}]},{"name":"Level 2: B","size":"1000"}]}
所以对于顶级,我只有一个尺寸为50000的盒子。(不是50000 + 10000 + 2000 + 3000 + 1000 = 66000)
答案 0 :(得分:0)
您可以手动覆盖层次结构对象的值并跳过sum
功能
var rawData = {
"name": "Top Level",
"size": "50000",
"children": [
{
"name": "Level 2: A",
"size": "10000",
"children": [
{
"name": "Son of A",
"size": "2000"
},
{
"name": "Daughter of A",
"size": "3000"
}
]
},
{
"name": "Level 2: B",
"size": "1000"
}
]
}
var root = d3.hierarchy(rawData); //apply hierarchy
root.each(d=> d.value = +d.data.size); //override values