K.M.J
K.M.J

Reputation: 145

force directed graph filter nodes and links

I am currently doing data visualization using force-directed graph in D3.js. I have a use case in which I have to filter the nodes and links(by filtering I mean should not be displayed in the data visualization.) based on the threshold value of a score.

following is a piece of data json

"links": [
{
"source": 17,
"target": 9,
"score": 0.428
},
{
"source": 3,
"target": 9,
"score": 0.198
},
{
"source": 17,
"target": 13,
"score": 0.336
},
{
"source": 11,
"target": 13,
"score": 0.178
},
{
"source": 17,
"target": 13,
"score": 0.336
}]

"nodes": [
{
"size": 8,
"score": 0.5,
"id": "Node1",
"name": "Node1",
"type": "triangle-up"
},
{
"size": 10,
"score": 0.1,
"id": "Node2",
"name": "Node2",
"type": "circle"
},
{
"size": 10,
"score": 0.1,
"id": "Node3",
"name": "Node3",
"type": "circle"
},
{
"size": 10,
"score": 0.1,
"id": "Node4",
"name": "Node4",
"type": "circle"
},
{
"size": 10,
"score": 0.1,
"id": "Node5",
"name": "Node5",
"type": "circle"
}]

so what I did is based on the score parameter in the links, I have deleted it an item from the links array. and when I try to delete the node from the nodes array my graph gives multiple errors. So I want to know is there any way through which I can find the nodes which are not linked with any other nodes so that I can delete or not display them in my data visualization.

Upvotes: 0

Views: 760

Answers (1)

Tess8702
Tess8702

Reputation: 11

I do it in this way:

var links=data.links.filter(function(d) {
    return d.score > 0.5;
});

var nodes=data.nodes.filter(function(d) {
    if (d3.set(links.map(function(v) { return v.source_id })).values().includes(d.id) | d3.set(links.map(function(m){ return m.target_id })).values().includes(d.id)) {
        return d.id
    };
});

Then I have a function like drawNetwork(links,nodes){...}

Hope this helps!

Upvotes: 1

Related Questions