d3-初始化图形后的forceCollide的力更新半径 [英] d3-force update radius of forceCollide after initializing graph

查看:2464
本文介绍了d3-初始化图形后的forceCollide的力更新半径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是上一个标题为D3 - 在初始化图形后强制更新参数的跟进( D3-在初始化图形后强制更新参数)和@altocumulus回答。

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 that @altocumulus answered.

我试图更新模拟力修改一些节点的半径后。但是,当我调用 forceCollide 来更改时,它不起作用。

I am trying to update the simulation forces after modifying the radius of some nodes. However, when I call on forceCollide to account for the changes it does not work.

图首先正确启动,使用 forceCollide 和一个函数使强制对应于半径:

The graph first initiates correctly, using forceCollide and a function to have the force correspond with the radius:

var forceCollide = d3.forceCollide()
.radius(function(d){return d.radius;})
.iterations(2)
.strength(0.95);

var simulation = d3.forceSimulation()
.velocityDecay(velocityDecay)
.force("collide", forceCollide);

然后修改 d.radius 对象并希望 forceCollide 来反映更改。然而,当我再次调用 forceCollide 它不工作:

I then modify the d.radius object and want forceCollide to reflect the changes. However, when I call on the forceCollide again it does not work:

forceCollide.radius(function(d){
d.radius;})

推荐答案

这不会实际更新半径。你只是重新设置回调用于确定半径,甚至不会改变比以前。即使 更改,也不会触发更新,因为根据更新的数据不会重新计算半径。

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.

更新链接力的距离回调时,力本身将被初始化。查看来源会显示调用 initializeDistance()

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.

查看 collide force,但是,注意到没有初始化被调用:

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;
};

由于您的回调不会改变,您不需要调用 forceCollide .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.

这篇关于d3-初始化图形后的forceCollide的力更新半径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆