D3js:力导向,内部节点崩溃

D3js: force directed, collapsing internal nodes

本文关键字:内部节点 崩溃 D3js      更新时间:2023-09-26

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

这部分完成了向 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();
  }
}

希望对您有所帮助!