Skip to content Skip to sidebar Skip to footer

Dynamically Adding Nodes To D3.js Force Directed Graph

I am having trouble with dynamically adding nodes on to a d3.js force directed graph and I was wondering if anyone here could shed some light on the subject. The problem I am havin

Solution 1:

It looks like you're adding nodes only to the DOM, not to the force layout. To recap, here's what you need to do to add nodes to the force layout.

  • Add elements to the array that the force layout uses for its nodes. This needs to be the same array that you passed in initially, i.e. you can't create a new array and pass that it if you want smooth behaviour. Modifying force.nodes() should work fine.
  • Do the same for the links.
  • Add the new DOM elements using .data().enter() with the new data.
  • No change to the tick function should be required as adding the nodes and DOM elements is done elsewhere.

After adding the new nodes/links, you need to call force.start() again to have it take them into account.

Post a Comment for "Dynamically Adding Nodes To D3.js Force Directed Graph"