How to check d3 js force graph for nodes without links and delete them?

I want my nodes to be in control, so that each of them is connected and there are no lonely nodes.

My script adds a couple of new nodes every 30 seconds from a json request. If any of the new nodes is a duplicate of an existing node, the graph will only be updated using a unique node and associate it with another existing node.

While this is happening, I’m moving from the oldest nodes so that there are no more than 10 nodes on the graph. This is where I seem to run into problems. How can I remove nodes and check and remove any apostates, nodes that are not related to any other?

The script is based on a knoren post when adding new nodes .

this.checkLength = function () { if (nodes.length > 10) { var i = links.shift(); nodes.splice(findNodeIndex(i),1); update(); } } 
+4
source share
1 answer

As paxRoman suggested, to remove a node, you can do:

 node.exit().remove(); 

Now, to find the empty nodes, you can use the weight property for the force nodes, as described in the sequencing layout documentation :

weight - node weight; number of related links.

So finally, to get all the empty voids, you can do:

force.nodes (). Filter (function (d) {d.weight == 0})

with force is your power layout.

Also note that the weight property will only be initialized when force.start() called, as described in the documentation:

These attributes do not need to be set before passing the nodes to the layout; if they are not set, suitable defaults will be initialized by the layout when the start is called

+2
source

Source: https://habr.com/ru/post/1440160/


All Articles