Skip to content

Commit 7b4a772

Browse files
committed
Remove the children array for childless nodes.
If data is updated so that some nodes no longer have children, and a custom children accessor is used, then the children array was not being removed for such nodes, which meant that hierarchical layouts were incorrectly taking into account these children. Fixes d3#1579.
1 parent bfcdc04 commit 7b4a772

4 files changed

Lines changed: 17 additions & 5 deletions

File tree

d3.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6118,8 +6118,11 @@ d3 = function() {
61186118
}
61196119
if (sort) c.sort(sort);
61206120
if (value) node.value = v;
6121-
} else if (value) {
6122-
node.value = +value.call(hierarchy, node, depth) || 0;
6121+
} else {
6122+
delete node.children;
6123+
if (value) {
6124+
node.value = +value.call(hierarchy, node, depth) || 0;
6125+
}
61236126
}
61246127
return node;
61256128
}

d3.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/layout/hierarchy.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,11 @@ d3.layout.hierarchy = function() {
2828
}
2929
if (sort) c.sort(sort);
3030
if (value) node.value = v;
31-
} else if (value) {
32-
node.value = +value.call(hierarchy, node, depth) || 0;
31+
} else {
32+
delete node.children;
33+
if (value) {
34+
node.value = +value.call(hierarchy, node, depth) || 0;
35+
}
3336
}
3437
return node;
3538
}

test/layout/hierarchy-test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ suite.addBatch({
2020
assert.equal(nodes[0].value, 0);
2121
h.nodes(nodes[0]);
2222
assert.equal(nodes[0].value, 0);
23+
},
24+
"removes the children array for a node that has no children": function(hierarchy) {
25+
var h = hierarchy(),
26+
nodes = h.children(function() { return null; }).nodes({children: [{}]});
27+
assert.equal(nodes[0].value, 0);
28+
assert.isUndefined(nodes[0].children);
2329
}
2430
}
2531
});

0 commit comments

Comments
 (0)