【问题标题】:D3 JS - Force Graph - Doesn't restart layout after removing NodeD3 JS - 强制图 - 删除节点后不重新启动布局
【发布时间】: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


    【解决方案1】:

    在这种情况下,您不需要“重新启动”图表。您所要做的就是从 DOM 中删除节点并告诉强制布局有一组新节点(与之前的相同设置减去已删除的节点)。所以,在按钮的点击处理程序中:

    // Button Listeners
    $('#btnRemove').click(function(e) {
      // Remove the node from the array
      nodes.splice((nodes.length - 1), 1);
    
      // Rebind the nodes to the array
      node = svg.selectAll(".node")
        .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
    });
    

    这并没有解决节点链接线的删除问题,但您可以按照相同的方法来执行此操作。

    【讨论】:

    • 感谢相遇!这修复了图表在移除后被“破坏”。当我尝试删除具有条件的多个非顺序节点时,我遇到了一个新问题。如果我无法弄清楚,我将不得不发布一个新问题。再次感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-07
    • 2016-08-15
    • 1970-01-01
    • 2014-01-30
    • 1970-01-01
    • 2019-05-14
    • 2015-09-17
    相关资源
    最近更新 更多