Can ForceDirectedLayout layout around a specific node, and can it remember state?

Let’s take the example in ForceDirectedLayout

I have two questions I have been trying to figure out for a while now:

  1. Can I make this example “center” around a specific node, similar to Recentering Radial Layout?

  2. Every time a ForceDirectedLayout renders, it seems random in positioning. Is there a way to get it to render the same way every time?


  1. Try overriding to return true for the ForceDirectedVertex whose .node is the Node that you don’t want to be moved.

  2. Try setting to null.

@walter Thanks for some suggestoins. Will give them a shot and post back if I have any luck!

@walter Thanks for #2. It seems to work and stops generating random graphs.

However I still cannot seem to resolve #1. The node I have set to fixed… it still positions itself in odd places. For example, it might position itself in the bottom right of the canvas. I am trying to get it to position directly in the middle. Is that even possible with ForceLayout?

Maybe I misunderstood what you wanted to do. If you don’t care about where your “special” node is relative to other nodes in your graph, but you do care about where that node appears in your viewport (a.k.a. canvas) when the layout is done, then maybe you just want to call Diagram.centerRect at the appropriate time.

myDiagram = $(go.Diagram, ...,
    { ...
      "initialLayoutCompleted": function(e) {
          var node = e.diagram.findNodeForKey(...);
          // ... or however you want to find your special node
          if (node !== null) e.diagram.centerRect(node.actualBounds);

Remember that by default the diagram cannot scroll beyond the edges of its document bounds, so if the desired node is near the edge of the document, the node might still appear near the corresponding edge of the viewport, not precisely at the center.

There is no way to force ForceDirectedLayout to make sure that a particular vertex/node is precisely at the center of the resulting graph.

Read more about this at:

@walter I am running into an issue with randomNumberGenerator.

I thought I got it working. However it does not seem to work.

Basically I am building off this example:

I simply added:

DemoForceDirectedLayout.prototype.randomNumberGenerator = null

but this does not seem to work. Any thoughts? I already confirmed I am using version: "1.5.18".

EDIT: Solved this issue by initializing the layout first then setting it to null. eg.

    a = new go.ForceDirectedLayout()
    a.randomNumberGenerator = null