Skip to content

Commit

Permalink
Remove the children array for childless nodes.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jasondavies committed Oct 11, 2013
1 parent bfcdc04 commit 7b4a772
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
7 changes: 5 additions & 2 deletions d3.js
Original file line number Diff line number Diff line change
Expand Up @@ -6118,8 +6118,11 @@ d3 = function() {
}
if (sort) c.sort(sort);
if (value) node.value = v;
} else if (value) {
node.value = +value.call(hierarchy, node, depth) || 0;
} else {
delete node.children;
if (value) {
node.value = +value.call(hierarchy, node, depth) || 0;
}
}
return node;
}
Expand Down
2 changes: 1 addition & 1 deletion d3.min.js

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions src/layout/hierarchy.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ d3.layout.hierarchy = function() {
}
if (sort) c.sort(sort);
if (value) node.value = v;
} else if (value) {
node.value = +value.call(hierarchy, node, depth) || 0;
} else {
delete node.children;
if (value) {
node.value = +value.call(hierarchy, node, depth) || 0;
}
}
return node;
}
Expand Down
6 changes: 6 additions & 0 deletions test/layout/hierarchy-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ suite.addBatch({
assert.equal(nodes[0].value, 0);
h.nodes(nodes[0]);
assert.equal(nodes[0].value, 0);
},
"removes the children array for a node that has no children": function(hierarchy) {
var h = hierarchy(),
nodes = h.children(function() { return null; }).nodes({children: [{}]});
assert.equal(nodes[0].value, 0);
assert.isUndefined(nodes[0].children);
}
}
});
Expand Down

0 comments on commit 7b4a772

Please sign in to comment.