Explanation of D3 "pack" layout.
click to see official example
The concept of "node" in a pack layout is important to understand how pack layout works.
Each node has the following attributes:
- parent - the parent node, or null for the root. Automatically computed!
- children - the array of child nodes, or null for leaf nodes.
- value - the node value, as returned by the value accessor.Line 40. If not existing, it is the sum of its children's size.
- depth - the depth of the node, starting at 0 for the root.Automatically computed!
- x - the computed x-coordinate of the node position. Automatically computed!
- y - the computed y-coordinate of the node position.Automatically computed!
- r - the computed node radius.Automatically computed!
So only "children" and "value" are assigned from the given input dataset. I will use an example to explain the concept.
{
"name": "flare",
"children": [
{"name": "sub1", "size": 3938},
{
"name": "sub2",
"children": [
{"name": "sub2_sub1", "size": 6714},
{"name": "sub2_sub2", "size": 743}
]
}
]
}
We have 5 objects in the json file:
Object { name: "flare", children: Array[2], depth: 0, value: 11395, y: 478, x: 478, r: 478 }
Object { name: "sub1", size: 3938, parent: Object, depth: 1, value: 3938, r: 174.4465055101499, x: 174.4465055101499, y: 478 }
Object { name: "sub2", children: Array[2], parent: Object, depth: 1, value: 7457, r: 303.5534944898501, x: 652.4465055101499, y: 478 }
Object { name: "sub2_sub1", size: 6714, parent: Object, depth: 2, value: 6714, r: 227.7797367518277, x: 728.2202632481724, y: 478 }
Object { name: "sub2_sub2", size: 743, parent: Object, depth: 2, value: 743, r: 75.77375773802244, x: 424.6667687583222, y: 478 }
"Node" of the pack corresponds to each object. So we have 5 nodes in this pack layout. "x", "y", "r" and "parent" are already there which means they are computed by pack layout. "name" and "children" are assigned by the dataset in the given json file. The size of each node is controlled by pack.value (Line 40). Note object "flare" does not have a "size" attribute, so its size is the sum of its children's sizes.