How to get the same node positions in a d3 force layout plot

I would like to create a force oriented graph, but I need it to remain the same every time I create it (with the same data). Is there a way to do this using d3.js?

UPDATE:

I found a working solution based on using visited random number generator

// set the random seed
Math.seedrandom('mySeed');

      

+3


source to share


1 answer


You could change the D3 force modification or create your own layout based on this. There are at least 3 places where random (Math.Random) is used when positioning nodes (there could be more given that the link to the power device refers to different code). You will need to eliminate all the randomness so that each graph displays the same way:

https://github.com/mbostock/d3/blob/master/src/layout/force.js



However, this will make the layout more difficult to work with - it uses randomness to quickly sort itself into a clear chart. If the number of nodes is small, then this probably won't be a problem, but a large number of nodes may just end up in the club.

+2


source







All Articles