Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the same node positions in d3's force layout graph

I would like to create a force directed graph, but i need it to stay the same every time it is generated (with the same data). Is there any way to do this using d3.js?

UPDATE:

I found working solution, which is based on using seeded random number generator

// set the random seed
Math.seedrandom('mySeed');
like image 426
otter Avatar asked Nov 01 '22 05:11

otter


1 Answers

You could by modifying D3's force layout, or by creating your own layout based on it. There are at least 3 places where randomness (Math.Random) is used in the positioning of nodes (there may be more, given the force layout references other code). You would have to eliminate all randomness in order to get the graph to display in the same way each time:

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

However, this would hamper how the layout works – it's using randomness to sort itself out into a legible diagram quickly. If your number of nodes is small, then it probably wouldn't be an issue, but a large number of nodes could just end up a tangle.

like image 109
Katharine Osborne Avatar answered Nov 12 '22 15:11

Katharine Osborne