Collapse/expand child nodes of tree in d3.js?

wataraptor picture wataraptor · Mar 2, 2012 · Viewed 18.8k times · Source

I'm building a tree structure (or rather modifying one of the examples with a set of my own data in my own json) and I'm trying to create some functionality:

My tree's layout is that from the tree example: http://mbostock.github.com/d3/ex/cluster.html

I am adding (to the circles) an onclick event which I would like to collapse the children of the clicked node. That is to say, when a user clicks the steelblue circle associated with a node, I want that nodes children to disappear.

I've scoured the documentation and I haven't turned up anything which would allow me to make nodes collapse or disappear.

What could I do?

Answer

mbostock picture mbostock · Mar 3, 2012

There's this:

http://mbostock.github.com/d3/talk/20111018/tree.html

There are a number of other interactive hierarchical layout examples from my SVG Open keynote.