d3-force update radius of forceCollide after initializing graph

后端 未结 1 567
伪装坚强ぢ
伪装坚强ぢ 2021-01-13 17:44

This question is a follow-up on a previous one titled \"D3-Force updating parameters after initializing graph\" (D3-Force updating parameters after initializing graph) and t

相关标签:
1条回答
  • 2021-01-13 18:16

    This will not actually update the radius. You are just re-setting the callback used to determine the radius, which doesn't even change compared to what it was before. Even if it did change, this would not trigger your update, because the radii won't be re-evaluated based on your updated data.

    When updating the distance callback of the link force, the force itself will get initialized. A look at the source shows a call to initializeDistance():

    force.distance = function(_) {
      return arguments.length ? (distance = typeof _ === "function" ? _ : constant(+_), initializeDistance(), force) : distance;
    };
    

    The same holds true for many other updates of other forces' parameters.

    Looking at the source of the collide force, however, one notices that there is no initialization invoked:

    force.radius = function(_) {
      return arguments.length ? (radius = typeof _ === "function" ? _ : constant(+_), force) : radius;
    };
    

    Since your callback doesn't change you won't need to call forceCollide.radius() again. Instead you need to call

    forceCollide.initialize(simulation.nodes());
    

    This will re-evaluate the radii based on your updated data.

    0 讨论(0)
提交回复
热议问题