Reading the Force-Directed Graph example, I find it hard to understand, when the simulation starts.
According to the the API, "The simulator starts automatically". So that would be here:
var simulation = d3.forceSimulation()
.force("link", d3.forceLink().id(function(d) { return d.id; }))
.force("charge", d3.forceManyBody())
.force("center", d3.forceCenter());
Hmmm, without nodes, there wont be much to simulate. Continue reading the code, I assume, it might happen here:
simulation
.nodes(graph.nodes)
.on("tick", ticked);
Now it has nodes and can work. But it still has no links. Hmmm. So does it run considering the other forces? Well, Javascript is singlethreaded, so the time callback cannot execute anyway. So presumably it the timer fires, but the callback will be run after this function, or more exactly, after the XmlHttpRequest callback is handled. Lots of pondering. Continue reading
simulation.force("link")
.links(graph.links);
I assume that now the simulation has all arguments and can run considering the link forces. Which in fact is correct. Anyway, I find the API a bit confusing in this point (otherwise the new force API is absolutely ingenious!).
Debugging reveals, that force.initialize() is called 3 times, computing
nodeById = map$1(nodes, id),
twice, unnecessarily. So maybe it makes sense to have an API method alike
simulation.start(nodes, links)
or more abstract (or precise)
simulation.start(all parameters required to run the configured simulation)
? For me that would have been clearer. Most functions would fall into category configure while start, restart, stop would be category run.
thx so much for d3
urbanhop