Does d3.hierarchy require explicit sum and sort to match d3.layout.hierarchy behavior?
0 reputation · 26 Jan 2024, 18:29 UTC
0 reputation · 26 Jan 2024, 18:29 UTC
I am migrating a tree visualization from d3.layout.hierarchy in d3.js v3 to d3.hierarchy in d3.js v5. The layout must preserve the same node aggregation and sibling ordering as the previous implementation.
Available notes indicate that d3.hierarchy does not apply a default sum accessor and that its sort method replaces the legacy sort property with a comparator. It is unclear whether omitting these calls silently changes aggregation or leaves children unsorted, and whether running mixed d3 versions in one bundle could cause namespace collisions.
Does d3.hierarchy default sum to null and leave children unsorted unless sum and sort are explicitly configured? Which comparator signature should replace the legacy sort property to preserve v3 ordering? Is checking d3.version at runtime sufficient to detect mixed-version conflicts?
36775 reputation · 27 Jan 2024, 02:25 UTC
Yes. d3.hierarchy (v5) does not apply a default sum accessor and does not sort children unless you explicitly configure both. Omitting either changes the tree structure produced by the new API relative to d3.layout.hierarchy (v3), even when the underlying data are identical.
.sum(), each node’s value defaults to 1.layout.hierarchy, the default value accessor returned d.value, so aggregation depended on the data’s existing value property..sum accessor that mirrors the v3 behavior.sort property (boolean true, string array, or implicit comparator) is replaced by .sort(comparator) in d5, which requires a function (a, b) => number.(a, b) => d3.ascending(a.data.name, b.data.name).Checking d3.version at runtime identifies the active major version’s API, but it does not prevent conflicts when code mixes v3 layout aggregation/sort calls with v5 hierarchy methods on the same root node. The two APIs live in distinct namespace objects (d3.layout vs d3.hierarchy), so loading both globally does not cause namespace collisions, but it does mean that the last .sum() or .sort() call wins for a given root.
// Minimal tree, no sum
const rootV5 = d3.hierarchy(data);
console.log(rootV5.children[0].value); // → 1 if .sum() omitted
// Explicit sum to match v3
const rootV5Sum = d3.hierarchy(data).sum(d => d.value);
// Explicit sort matching v3 sort=true
rootV5Sum.sort((a, b) => d3.ascending(a.data.name, b.data.name));
// Compare children order to what v3’s sort=true produced
If your data structure or ordering property differs from name, adjust the comparator path. The two checks above confirm whether aggregation and sibling order match the previous implementation.
Use comments to ask for clarification. Post a solution as an answer.
7,230 reputation · 27 Jan 2024, 02:36 UTC
Yes. In D3 v4 through v7, d3.hierarchy initializes sum and sort as null, so node.value stays undefined and children keep the accessor order until those methods are called explicitly. The .sort() comparator receives hierarchy node objects, not raw data, so a v3 comparator that read properties directly on its arguments needs a wrapper mapping a.data and b.data before applying the legacy logic. A runtime d3.version check is not enough to prove mixed versions are present; check for d3.layout or audit the bundle for duplicate D3 distributions. Verify by calling d3.hierarchy(data) without .sum() or .sort(), then inspect root.descendants() for undefined values and unchanged sibling order.