【问题标题】:D3js: force directed, collapsing internal nodesD3js:强制定向,折叠内部节点
【发布时间】:2016-02-11 00:16:22
【问题描述】:

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

【问题讨论】:

    标签: javascript d3.js


    【解决方案1】:

    这部分完成了将 cmets 添加到 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();
      }
    }
    

    希望对你有帮助!

    【讨论】:

    • 这将改变var nodes = flatten(root), links = d3.layout.tree().links(nodes);。我猜这些值必须改变没有?
    • no flatten 意味着从根的嵌套结构中获取所有节点。 links = d3.layout.tree().links(nodes) 生成链接。
    • 在你的评论中你说making d.children as null so that the exit function will remove the nodes in update. 这意味着link = link.data(links, function(d) { return d.target.id; }); 将改变选择。我不明白为什么如果d.children 为空,选择会受到影响。
    • 对不起,如果我不明白,但如果链接被删除(节点相同),这意味着link.exit().remove(); 已更新,不是吗?否则 d3js 怎么知道哪些链接被删除了?
    • 是的,如果您删除节点,链接也将消失,原因是这一行 link = link.data(links, function(d) { return d.target.id; }); 将为所有具有 traget.id 的链接创建选择,因此所有那些具有 target.id 或节点的链接通过link.exit().remove() 删除。我认为您需要阅读本教程medium.com/@c_behrens/enter-update-exit-6cafc6014c36#.e77lbxc9d...
    猜你喜欢
    • 2021-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 2015-02-15
    • 2013-10-07
    • 2015-12-04
    相关资源
    最近更新 更多