3

I'd like to make a force layout with two types of nodes : type 1 stay in the center, and type 2 move in the periphery. There are links between type 1 and type 2, so the graph should stay together.

I imagine that i can do that with fixing the gravity positive for type 1 node and negative for type two :

  force.gravity(function(d){return (d.type=='pers')?10:-15})

but gravity seems to be a one for all parameter. Is there other way or can i change that to make the gravity parameter node dependent ? Or a completely different way to achieve this ?

4

2 回答 2

3

我尝试了一段时间,结果它也没有任何效果。如果您查看代码,似乎重力无法发挥作用:

比较 .charge() 和 linkStrength():

force.linkStrength = function(x) {
  if (!arguments.length) return linkStrength;
  linkStrength = typeof x === "function" ? x : +x;
  return force;
};
force.charge = function(x) {
  if (!arguments.length) return charge;
  charge = typeof x === "function" ? x : +x;
  return force;
};

到.gravity():

force.gravity = function(x) {
  if (!arguments.length) return gravity;
  gravity = +x;
  return force;
};

我不确定其他地方是否有进一步的限制,但如果你将重力传递给一个函数,它将不知道如何处理它。

于 2013-05-25T13:48:00.857 回答
2

电荷是力的属性,而不是节点的属性。您可以对每个节点收取不同的费用。节点带有负电荷,因此它们被“重力”吸引并被其他节点排斥。如果您为类型 1 的节点设置较大(幅度)的电荷,而为类型 2 的节点设置较小的电荷,您可能会达到预期的效果。

Mike Bostock 的这次演讲解释并展示了不同的力配置:

于 2013-05-25T16:39:21.413 回答