flattree
converts a tree in flat table form into a d3-hierarchy
.
In the R package d3r
, I have a converter to go from data.frame
to d3-hierarchy
. Recently, I needed this functionality all in JavaScript to hack a hierarchical sort into datatables
.
A flat tree table contains a row for each node with columns for each of the levels of groupings and additional columns for attributes. It will look something like below and can be an array of object or an array of arrays.
grp subgrp attr1 attr2 A null 50 'black' A A.1 20 'red' B null 5 'yellow' B B.1 2 'green' B B.2 3 'green'
In R
this structure is well illuminated in the treemap
package.
Below are some very simplified examples.
d3.flattree([["A",null],["A","A.1",20],["A","A.2",30]], [0,1])
d3.flattree(
[
{grp:"A"},
{grp:"A", subgrp:"A.1", value:20},
{grp:"A", subgrp:"A.2", value:30}
],
["grp", "subgrp"]
)