On a tree layout using d3.js (example), I'd like to collapse nodes that are not in the branch that has been clicked on.
For example, in the above demo, try the following:
Now you should see both the children of "Child 1" and "Child 2".
I would like to have the following happen:
So children of nodes other than on the "active" branch should be hidden.
How can I best approach this? (efficiently of course, as I'll be using a fairly large dataset)
One simple solution is to modify the click function such that if the node has a parent, the parent's children are each collapsed, but only if the child isn't the node that was clicked on.
function click(d) {
if (d.children) {
d._children = d.children;
d.children = null;
} else {
d.children = d._children;
d._children = null;
}
// If the node has a parent, then collapse its child nodes
// except for this clicked node.
if (d.parent) {
d.parent.children.forEach(function(element) {
if (d !== element) {
collapse(element);
}
});
}
update(d);
}
Updated jsbin: http://jsbin.com/etIJABU/2/edit
if you want to collapse other nodes only when you click on nodes with children, add "&& d.children" in the second IF..
function click(d) {
if (d.children) {
d._children = d.children;
d.children = null;
}
else {
d.children = d._children;
d._children = null;
}
if (d.parent && d.children) { //add here,
d.parent.children.forEach(function(element) {
if (d !== element) {
collapse(element);
}});
}
update(d);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With