简体   繁体   English

如何删除d3.js中的现有节点

[英]How to remove existing nodes in d3.js


I have already visit add and remove nodes in D3js but it don't solve my problem. 我已经访问过D3js中的添加和删除节点,但它无法解决我的问题。

There are some nodes at first time, then I want to add nodes dynamically and want if node is already exists it update that nodes and don't do duplicate. 第一次有一些节点,然后我想动态添加节点,并且想要节点已经存在它更新节点并且不重复。

now it is making duplicate not updating existing ones. 现在它正在复制不更新现有的复制品。
Here is main code and full code and working fiddle is here 这里是主要代码和完整代码,工作小提琴就在这里

//handles node elements
var circles = svg.selectAll('g');


//update graph (called when needed)
function restart() {

/***************************************
    Draw circles (nodes)
****************************************/


circles = circles.data(data.nodes);

var g = circles.enter()
               .append("g")
               .attr("class", "gNode")
               .attr("cursor", "pointer")
               .attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; })
               .call(force.drag);



g.append("circle")                      
.attr({
     "class": "node", 
     "cx": function(d) { return rScale(d.NumOccurrences); },
     "cy": function(d) { return rScale(d.NumOccurrences); },
     "r": function(d) { return rScale(d.NumOccurrences); }
 })             
 .style("fill", function(d, i) { return colors(i); })
 .style("stroke", "#000");



 g.append("text")
.attr({
    "x": function(d) { return rScale(d.NumOccurrences); },
    "y": function(d) { return rScale(d.NumOccurrences); },
    "font-family": "sans-serif",
    "font-size": "20px",
    "fill": "black",
    "text-anchor": "middle"
   })
   .text( function (d) { return d.name; });

   g.append("title")        
    .text(function(d) { return d.name; });

 // remove old nodes
 circles.exit().remove();

 // set the graph in motion
 force.start();
 }

// app starts here
restart();


 function dynamicAddNodes() {

var updatedata = {"name":"ios","NumOccurrences":"500","color":"green","x":0,"y":1}

data.nodes.push(updatedata);    

restart();
 }

 setInterval(dynamicAddNodes, 10000);

try it: 试试吧:

circles = circles.data(data.nodes,function (d) {
     return d.id;
   });

that the node's Id is unique. 节点的Id是唯一的。

you can see : jsfiddle.net/MoHSenMHS/5r62N/ 你可以看到: jsfiddle.net/MoHSenMHS/5r62N/

Your problem is not the update nodes process, but the data you are updating them to. 您的问题不是更新节点进程,而是您要更新它们的数据。

When restart() runs, it doesn't remove any nodes from the data, it just adds them. restart()运行时,它不会从数据中删除任何节点,只是添加它们。 Since the data is never taken away, nodes are never taken away. 由于数据永远不会被带走,因此节点永远不会被带走。 Each time the function is called, a new data node is added, and a new circle is added corresponding to that data node. 每次调用该函数时,都会添加一个新数据节点,并添加一个与该数据节点对应的新圆。

I've updated your example here to show this behavior. 我在这里更新了您的示例以显示此行为。 Each time I change the data, I remove an entry from your data, and replace it with a new data point. 每次更改数据时,都会从数据中删除一个条目,并将其替换为新的数据点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM