Why ForceDirectedLayout Graph changes layout?


I am trying to create a ForceDirectedLayout graph and I see that every time that I refresh the page, the layout changes. I mean the nodes change position and it seams like rotating or so.
Is there a way to have a standard layout for this kind of graph?
Thanks in advance.

I suppose you could save the results of the initial layout and restore them when reloading.

But I suspect you are really asking how to do that without saving any state. For ForceDirectedLayout there is a random component to the calculations which is used when nodes are on top of each other, which is often the case initially. Alas, in GoJS we do not allow you to control that – right now it just calls Math.random. Perhaps we can customize that in the future.

@water, did you guys customized this?

Basically, our nodeDataArray is dynamic. And we wanted to make sure that the first node will be at the left-most position and the last node will be at the right-most position. Can you suggest a way to achieve this?

Thanks in advance!

Yes: http://gojs.net/latest/api/symbols/ForceDirectedLayout.html#randomNumberGenerator

ForceDirectedLayout doesn’t know about “first” and “last” nodes

But if you don’t want to use TreeLayout or LayeredDigraphLayout, you still have several possibilities with ForceDirectedLayout.

You could override http://gojs.net/latest/api/symbols/ForceDirectedLayout.html#isFixed to return true for the LayoutVertexes corresponding to the “first” and “last” Nodes. If you first assign Node.location for those two special nodes before the layout runs, they will remain motionless throughout.