d3.stratify() converts flat CSV records into a hierarchy: a root node containing parent-child relationships for hierarchical visualizations. section 11.1.1 section 11.5
Build the hierarchy by configuring the columns that identify each relationship:
- Import stratify from d3-hierarchy when using the focused module instead of the complete D3 library. section 11.1.1
- Configure id(d => d.child) to identify each node and parentId(d => d.parent) to identify its parent. section 11.1.1
- Pass the flat records to the configured generator to obtain root, then read root.descendants() and root.leaves(). section 11.1.1
Convert language records
import { stratify } from "d3-hierarchy"; const hierarchyGenerator = stratify() .id(d => d.child) .parentId(d => d.parent); const root = hierarchyGenerator(flatData); const descendants = root.descendants(); const leaves = root.leaves(); section 11.1.1
Table 11.2 The parent-child relationships communicated via columns in a CSV file
child
|
parent
|
total_speakers
|
native_speakers
|
|---|---|---|---|
|
Languages
|
|||
|
Indo-European
|
Languages
|
||
|
Sino-Tibetan
|
Languages
|
||
|
Germanic
|
Indo-European
|
||
|
Sinitic
|
Sino-Tibetan
|
||
|
Indo-Aryan
|
Indo-European
|
||
|
Romance
|
Indo-European
|
||
|
Romance
|
Indo-European
|
||
|
English
|
Germanic
|
1,132 M
|
379 M
|
|
Mandarin Chinese
|
Sinitic
|
1,117 M
|
918 M
|
|
Hindi
|
Indo-Aryan
|
615 M
|
341 M
|
|
Spanish
|
Romance
|
534 M
|
460 M
|
|
French
|
Romance
|
280 M
|
77 M
|
Inspect the child and parent columns used to express language relationships before configuring the generator. section 11.1.1
