2016-05-05 30 views
1

我有更新我的力量向圖,這是我每一次我從respectives陣列刪除節點和鏈路將調用此代碼:刪/改標籤

function update_graph() { 

link_update = svg.selectAll(".link").data(
     force.links(), 
     function (d) { 
      return d.source.id + "-" + d.target.id; 
     } 
); 

link_update.enter() 
     .insert("line", ".node") 
     .attr("class", "link"); 

link_update.exit() 
     .remove(); 

node_update = svg.selectAll(".node").data(
     force.nodes(), 
     function (d) { 
      return d.id; 
     } 
); 

node_update.enter() 
     .append("circle") 
     .attr("class", function (d) { 
      return "node " + d.id; 
     }) 
     .attr("r", function (d) { 
      return radio/d.group; 
     }) 
     .call(force.drag) 
     .on('dblclick', connectedNodes); 


//labels 
node_update.enter().append("text") 
.attr("dx", function (d) { 
return (radio-7)/d.group; 
}) 
.attr("dy", function (d) { 
return (radio-7)/d.group; 
}) 
.text(function (d) { 
return d.id; 
}) 
.style("stroke", "black"); 


// Remove the SVG circle whenever a node vanishes from the node list. 
node_update.exit() 
     .remove(); 

// Start calling the tick() method repeatedly to lay out the graph. 
force.start(); 

}

節點和鏈接從圖中正確刪除,但標籤保留在那裏,我如何讓標籤與節點同步並只顯示圖上當前的內容?

小提琴複製問題:提前

https://jsfiddle.net/vzes4h8y/

感謝

+1

你可以放在一起工作小提琴 – thatOneGuy

+1

下面是一個小提琴複製問題,請單擊「添加節點」,然後在「重置圖形」 HTTPS幾次://的jsfiddle .net/vzes4h8y/ – RCRH89

回答

1

您可以爲文本

node_updateText = svg.selectAll(".text").data(
     force.nodes(), 
     function (d) { 
      return d.id; 
     } 
).enter(); 

並據此消除他們創造一個不同的變量。

這裏是小提琴:https://jsfiddle.net/LmqLtr8e/

+0

我明白了,這比我想象的要容易。謝謝你的時間! – RCRH89

+0

不用擔心。我幾乎沒有找到按鈕! –