Question

What I want to do is onClick, change the charge so that nodes are repelled by a higher force, but this is not working:

var force = d3.layout.force()
    .gravity(.05)
    .charge(function(d, i) { return i ? 0 : -2000; })
    .nodes(nodes)
    .size([w, h]);

svg.on("click", function() {
  force = force.charge(function(d, i) { return i ? 0 : -10000; })
  window.setTimeout(function() {
  force = force.charge(function(d, i) { return i ? 0 : -2000; })
  },3000);
});
Was it helpful?

Solution

all you have to do is call force.start which runs through the declared nodes again and changes the charges and other properties on them.

svg.on("click", function() {
  force = force.charge(function(d, i) { return i ? 0 : -50000; })
  force.start()
  window.setTimeout(function() {
  force = force.charge(function(d, i) { return i ? 0 : -2000; })
  force.start()
  },3000);
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top