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

风格不统一 提交于 2020-04-13 18:04:48

问题


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');

回答1:


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.



来源:https://stackoverflow.com/questions/29825508/how-to-get-the-same-node-positions-in-d3s-force-layout-graph

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!