D3js:强制定向,折叠内部节点

时间:2016-02-11 00:16:22

标签: javascript d3.js

在d3js http://bl.ocks.org/mbostock/1062288的示例中,代码的哪一部分处理单击它们的折叠内部节点。事件处理程序click设置颜色的数据,但我看不到哪个部分正在处理折叠效果。当然link.exit().remove()node.exit().remove()会删除积分和链接。但是如何创建“退出”部分?

1 个答案:

答案 0 :(得分:0)

这部分为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();
  }
}

希望它有所帮助!