【发布时间】:2016-02-11 00:16:22
【问题描述】:
在 d3js http://bl.ocks.org/mbostock/1062288 的示例中,哪部分代码处理了单击它们的折叠内部节点。事件处理程序click 设置颜色的数据,但我看不到哪个部分正在处理折叠效果。当然link.exit().remove() 和node.exit().remove() 删除点和链接。但是“退出”部分是如何创建的呢?
【问题讨论】:
标签: javascript d3.js
在 d3js http://bl.ocks.org/mbostock/1062288 的示例中,哪部分代码处理了单击它们的折叠内部节点。事件处理程序click 设置颜色的数据,但我看不到哪个部分正在处理折叠效果。当然link.exit().remove() 和node.exit().remove() 删除点和链接。但是“退出”部分是如何创建的呢?
【问题讨论】:
标签: javascript d3.js
这部分完成了将 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);。我猜这些值必须改变没有?
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...