9

I have read a lot about influencing the initial animation of the force layout but am afraid I have not understood it yet.

I have found out (and could implement) this example, about how to effectively "stop" it.

But my question is, is it possible to control it (i.e to influence how long it takes until "force stops"?).

From the documentation it seems that alpha is the parameter to change but it does not make a difference ( I tried negative values, zero and positive value without any noticeable difference).

Here is a jsdiddle of what I am trying to do : yrscc fiddle of what I am trying to do.

var force = d3.layout.force()
    .nodes(d3.values(datax.nodes))
    .links(datax.links)
    .size([xViewPortArea.Width, xViewPortArea.Height])
    .linkDistance(xGraphParameters.xLinkDistance)
    .charge(xGraphParameters.xCharge)
    .on("tick", tick)
     .alpha(-5)  // HERE 
    .start();

My questions:

  • which value of alpha would actually influence the number of iterations ? (I thought that it was is meant with " *If value is nonpositive, and the force layout is running, this method stops the force layout on the next tick and dispatches an "end" in the documentation
  • in this this posting a function is proposed by @JohnS which apparently can help . But I have not understood where one is supposed to call it.

P.S: another cool option is to have an image on the screen and then compute the optimal layout in the background like here. But I gave up trying to implement it

Community
  • 1
  • 1
user1043144
  • 2,680
  • 5
  • 29
  • 45

1 Answers1

10

One way to cool down the force layout is to force the tick events:

var k = 0;
while ((force.alpha() > 1e-2) && (k < 150)) {
    force.tick(),
    k = k + 1;
}

the alpha value measures the temperature of the force, lower values indicate that the layout is more stable. The number of ticks to consider it stable will depend on the number of nodes, I have had good results with 50-200. The friction coefficient will help to stabilize the force, but the layout will be less optimal.

Pablo Navarro
  • 8,244
  • 2
  • 43
  • 52
  • excellent. Thanks. I just copied the code at the end of my file and it works perfectly. P.S: I have also played around with friction but it is as you say: it slows down the animation but results in strange shape of the network. – user1043144 Jun 19 '13 at 11:19
  • This could be simplified a bit `while (force.alpha()) { force.tick(); }`, or `while (force.alpha() > 1e-5) { force.tick(); }` if we want to control the level at which the layout should stop calculating. – Pablo Navarro Feb 26 '16 at 19:17