How can I show a graph using d3 force layout with initial positions set and no movement at all?

陌路散爱 提交于 2019-12-10 03:02:38

问题


I use d3 force layout to construct a graph, and then store the positions of the nodes in a database.

The next time I want to show the graph, I read the positions from the database into the nodes and then I start the force layout.

That causes the layout to move slightly.

Is there I way to show the graph with the stored node positions without calling force.start() at all?

What I want is a "read only" mode to show a stable graph created by someone else earlier.

/Hans


回答1:


I recommend you to read the documentation of the force layout. It is really good.

As you mentioned, you have to set the px and py attributes for each node. Then, you can set the attribute fixed to true in each node.

This will imply that computing force.start() won't have any effect.


Another thing you can try is to set the alpha value to 0, and then start the simulation:

force.alpha(0)
    .start()

I am not sure whether this method works. Post a jsFiddle if it does not.



来源:https://stackoverflow.com/questions/17286138/how-can-i-show-a-graph-using-d3-force-layout-with-initial-positions-set-and-no-m

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