I am trying to create a fixed graph in D3js. The nodes and the links are given below. Its an undirected graph. MY goal is to display all nodes of the same age. The links displayed should be between these nodes only . for example if I click a checkbox to display all nodes of age 23 then the nodes displayed should be:
- Chris(1),
- tina(2),
- Mary(5),
- ronaldo(6),
- cs(9)
The links displayed should be
{'source': 7, 'target': 1, 'value': '1'},{'source': 9, 'target': 1, 'value': '1'},{'source': 5, 'target': 8, 'value': '1'},{'source': 5, 'target': 2, 'value': '1'},{'source': 2, 'target': 1, 'value': '1'},{'source': 5, 'target': 1, 'value': '1'},{'source': 5, 'target': 6, 'value': '1'},{'source': 5, 'target': 9, 'value': '1'},{'source': 5, 'target': 4, 'value': '1'},
Here the number against source and target are the count of the node that is chris is assigned as 1 , tina as 2 and so on. The number is done based on the order it appears in the nodes list.
Data =
{nodes: [{‘name’: ‘chris’ , ‘age’: 23 , ‘group’: 0}, {‘name’: ‘tina’ , ‘age’: 23 , ‘group’: 1},{‘name’: ‘shawn’ , ‘age’: 29 , ‘group’: 0}, {‘name’: ‘alex’ , ‘age’: 25 , ‘group’: 0} , {‘name’: ‘Mary’ , ‘age’: 23 , ‘group’: 0}, {‘name’: ‘ronaldo’ , ‘age’: 23 , ‘group’: 0}, {‘name’: ‘kaka’ , ‘age’: 25 , ‘group’: 0}, {‘name’: ‘cent’ , ‘age’: 25 , ‘group’: 0}, {‘name’: ‘cs’ , ‘age’: 23 , ‘group’: 4}],
links: [{'source': 7, 'target': 1, 'value': '1'}, {'source': 9, 'target': 1, 'value': '1'}, {'source': 5, 'target': 8, 'value': '1'},{'source': 3, 'target': 7, 'value': '1'} {'source': 5, 'target': 2, 'value': '1'}, {'source': 2, 'target': 1, 'value': '1'},{'source': 3, 'target': 4, 'value': '1'}, {'source': 8, 'target': 1, 'value': '7'}
{'source': 5, 'target': 1, 'value': '1'}, {'source': 5, 'target': 6, 'value': '1'}, {'source': 5, 'target': 9, 'value': '1'}, {'source': 5, 'target': 4, 'value': '1'}, {'source': 7, 'target': 4, 'value': '1'}]}
While searching I came across code to reduce the opacity of the other nodes, but that's not what I am looking for.
I want the nodes and the edges that don't fulfil the criteria to disappear.
Another similar question is : How to approach filtering nodes and edges rendered via d3.js according to user preference? They have taken an example of 3 nodes only and statically written which nodes to display. I have to take a user Input and filter out accordingly.