【发布时间】:2014-11-05 12:00:03
【问题描述】:
更新: 移除节点后,图表现在可以正常运行。 但是,当我有条件地删除非顺序节点(例如 4、5、10)时,图表不会显示正确的节点。
请查看下面更新的点击处理程序(大量调试)。 我正在尝试删除所有具有“d.source”值==“news24”的节点,这些节点都是蓝色的大圆圈。 尽管“删除”后数组是正确的,但正在显示不正确的节点。 我怀疑它与 node.exit() 有关,但我不确定。
完整更新代码:http://www.darkness.co.za/code/graph_test_2.zip
$('#btnRemove').click(function(e) {
// These are the large blue circles
var sourceToHide = "news24";
// DEBUG - Before Removal
for (i=0; i < nodes.length; i++) {
console.log("node_object[" + i + "].source = " + nodes[i].source);
console.log("-----------------------------------");
}
// Hold indices of items to remove
var itemsToRemove = [];
// Find items with the source to remove
for (i=0; i < nodes.length; i++) {
var nodeSource = nodes[i].source;
console.log("Node source = " + nodeSource + " sourceToHide = " + sourceToHide);
if (nodeSource == sourceToHide) {
itemsToRemove.push(i);
}
}
// Reverse removal array - makes splice work as expected
itemsToRemove.reverse();
// Remove items
for (i=0; i < itemsToRemove.length; i++) {
nodes.splice(itemsToRemove[i], 1);
}
// DEBUG - After Removal
for (i=0; i < nodes.length; i++) {
console.log("node_object[" + i + "].source = " + nodes[i].source);
console.log("-----------------------------------");
}
// Rebind the nodes to the array
node = svg.selectAll("circle")
.data(nodes)
// Remove the exit'ed node
node.exit().remove();
// Tell the layout that this is the new set of nodes
// so that it doesn't include the old one in the computation
force
.nodes(nodes)
// That's it. No need to restart the force layout
});
我进行了很多搜索,并单独尝试了许多示例,但无法针对我的特定设置和数据解决此问题。
代码:
抱歉,我在 JSFiddle(外部文件等)上设置它时遇到问题,因此您可以在此处获取完整代码:
http://darkness.co.za/code/graph_test.zip
我想要达到的目标:
(对于这个测试)我想删除一个节点,然后重新绘制/重新启动图形
我尝试过的:
我试图从节点数组中删除最后一个元素,然后重新绘制图形(圆和线),然后调用 force.start()
问题:
该节点确实会根据需要消失,但整个图表会停止响应。
如何正确删除节点,然后以正常的拖动行为成功重新启动图形?
注意:我在“drawGraph()”函数的末尾调用“force.start()”
谢谢
【问题讨论】:
标签: javascript svg d3.js force-layout