D3.js 删除/更改力定向图中的标签
我有以下代码用于更新我的力定向图,每当我从各自的数组中删除节点和链接时,我都会调用它:D3.js 删除/更改力定向图中的标签,d3.js,label,force-layout,directed-graph,D3.js,Label,Force Layout,Directed Graph,我有以下代码用于更新我的力定向图,每当我从各自的数组中删除节点和链接时,我都会调用它: function update_graph() { link_update = svg.selectAll(".link").data( force.links(), function (d) { return d.source.id + "-" + d.target.id; } ); link_update.enter()
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();
}
节点和链接已从图形中正确删除,但标签仍保留在那里,如何使标签与节点同步并仅显示图形上当前的内容
Fiddle复制问题:
提前感谢您可以为文本创建不同的变量
node_updateText = svg.selectAll(".text").data(
force.nodes(),
function (d) {
return d.id;
}
).enter();
并相应地将其移除
这是一把小提琴:你能把一把工作的小提琴放在一起吗这是一把复制问题的小提琴,在“添加节点”中单击几次,然后在“重置图”中单击几次,这比我想象的要容易。谢谢你的时间!别担心。我差点没找到扣子!