简体   繁体   中英

D3js: force directed, collapsing internal nodes

In that example of d3js http://bl.ocks.org/mbostock/1062288 , which part of code handle the collapsing internal nodes where they are clicked. The event handler click sets the data for the colour but I don't see which part is handling the collapsing effect. Of course link.exit().remove() and node.exit().remove() remove the points and links. But how the "exit" section is created?

This part does the job added comments to the scriplet:

function click(d) {
  if (!d3.event.defaultPrevented) {
    if (d.children) { 
      //if the node is expanded the d.children will have nodes in it.
      //we are moving the nodes into another variable d._children
      d._children = d.children;
      //making d.children as null so that the exit function will remove the nodes in update.
      d.children = null;
    } else {
      //the node is collapsed state so moving the d._children back into variable d.children
      d.children = d._children;
      //making the d._children null
      d._children = null;
    }
    //after resetting/setting the d.children call update for appending nodes in case of expand(d.children have values) or remove nodes and links when (d.children have no values) 
    update();
  }
}

Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM