我有这个更新我的强制有向图的代码,每次我从相应的数组中删除节点和链接时,我都会调用它:
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/
提前感谢
发布于 2016-05-08 22:18:40
您可以为文本创建不同的变量
node_updateText = svg.selectAll(".text").data(
force.nodes(),
function (d) {
return d.id;
}
).enter();并相应地移除它们。
https://stackoverflow.com/questions/37055096
复制相似问题