Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A few things, in the order I approached the problem. Here's the <a href="http://jsfiddle.net/5dJaA/1/" rel="nofollow">final fiddle</a> if you want to follow along.</p> <p>First, your <code>update()</code> method does a lot more than just update the position of the nodes; it recalculates the whole tree. It's overkill for what you're trying to do, and while in the end it turned out to not be the source of the problem, I'd still recommend just using <a href="https://github.com/mbostock/d3/wiki/Force-Layout#wiki-resume" rel="nofollow"><code>force.resume()</code></a> to get the timer ticking and the nodes moving again.</p> <p>Second, you were on the right track with adding a <code>forEach</code> statement to turn off the former fixed nodes. But <code>forEach</code> works on arrays, and <code>root</code> is an object. So either reuse the <code>flatten</code> method (slow) or do as @Pablo suggested and use force.nodes() to get the nodes as an array (better).</p> <p>Third, and this took a bit to figure out: Once you set a node to be "fixed", the force layout ignores any new d.x and d.y values, resetting them back to d.px and d.py (the previous values). (I assume this is so that if the node is dragged, it will automatically jump back to its fixed position). So to move it to be fixed where you want it to be, you have to set the "previous" values, not the new values. The only problem is that this causes an immediate jump in position, instead of a smooth movement. You might want to add in a transition for that one node (to make it look like it's being dragged to the centre) before calling <code>force.resume()</code>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload