How to set d3.layout.force.charge once d3.layout.force has already been initialized?

StackOverflow https://stackoverflow.com/questions/22121682

  •  18-10-2022
  •  | 
  •  

سؤال

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);
});
هل كانت مفيدة؟

المحلول

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);
});
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top