Description
I'm currently building tree layouts (https://vega.github.io/vega/examples/tree-layout/) as seen here:
Typically, hierarchical data is stored in source systems in one of two different formats:
- As a narrow table of parent and child columns. e.g.
[
{"id": "A", "parent": null},
{"id": "B", "parent": "A"},
{"id": "C", "parent": "A"},
{"id": "D", "parent": "C"},
{"id": "E", "parent": "C"}
]
- As a wide, flattened table with each level of the hierarchy as its own column.
[
{"id": "A", "job": "Doctor", "region": "East"},
{"id": "B", "job": "Doctor", "region": "East"},
{"id": "C", "job": "Lawyer", "region": "East"},
{"id": "D", "job": "Lawyer", "region": "East"},
{"id": "E", "job": "Doctor", "region": "West"},
{"id": "F", "job": "Doctor", "region": "West"},
{"id": "G", "job": "Lawyer", "region": "West"},
{"id": "H", "job": "Lawyer", "region": "West"}
]
The first format is already catered for in Vega by supplying it to a Stratify transform and then a Tree transform. However, I don't believe there is currently any support for the second format which is quite common from my experience. I originally thought the Nest transform would handle these cases but the Nest transform only supports data where all leaves are at the same level. i.e. nest cannot handle something like the following ragged hierarchy (the last record for Accountant stops at a depth of 3 where as others records go to a depth of 4):
"values": [
{"id": "A", "job": "Doctor", "region": "East", "company": "Acme"},
{"id": "B", "job": "Doctor", "region": "East", "company": "Acme"},
{"id": "C", "job": "Lawyer", "region": "East", "company": "Acme"},
{"id": "D", "job": "Lawyer", "region": "East", "company": "Acme"},
{"id": "E", "job": "Doctor", "region": "West", "company": "Acme"},
{"id": "F", "job": "Doctor", "region": "West", "company": "Acme"},
{"id": "G", "job": "Lawyer", "region": "West", "company": "Acme"},
{"id": "H", "job": "Lawyer", "region": "West", "company": "Acme"},
{"id": null, "job": "Accountant", "region": "North", "company": "Acme"}
],
Incidentally, it might be an idea to update the documentation on Nest to clarify that only hierarchies with leaves at the same level are supported as it wasn't 100% clear to me until I tested.
In summary, a transform to turn the wide format of example 2 into the narrow format in example 1 before feeding to a stratify would be really helpful. Relevant d3 issues is here: d3/d3-hierarchy#149