首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >删除/更改强制有向图中的标签

删除/更改强制有向图中的标签
EN

Stack Overflow用户
提问于 2016-05-06 00:00:32
回答 1查看 340关注 0票数 1

我有这个更新我的强制有向图的代码,每次我从相应的数组中删除节点和链接时,我都会调用它:

代码语言:javascript
复制
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/

提前感谢

EN

回答 1

Stack Overflow用户

发布于 2016-05-08 22:18:40

您可以为文本创建不同的变量

代码语言:javascript
复制
node_updateText = svg.selectAll(".text").data(
        force.nodes(),
        function (d) {
            return d.id;
        }
).enter();

并相应地移除它们。

这里是小提琴:https://jsfiddle.net/LmqLtr8e/

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37055096

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档