简体   繁体   English

如何使用d3.js,force和network仅使用一个函数设置cx和cy?

[英]How to set cx and cy with only one function using d3.js, force and network?

All I found say something like this to change cx,cy : 我发现的所有内容都是说要更改cx,cy

  .attr("cx", function(d) {var x... return x; })
  .attr("cy", function(d) {var y... return y; })

But I have a function F(x,y) that returns {X:value, Y:value} . 但是我有一个函数F(x,y)返回{X:value, Y:value}

As this F function is computationally expensive, I can't call it twice. 由于此F函数在计算上很昂贵,因此我不能两次调用它。

I need something like this: 我需要这样的东西:

 node.attr("[cx,cy]", function(d){ return F(d.x,d.y) }

Just set cx and cy calling function F just one time for each node. 对于每个节点,只需将cxcy调用函数F设置一次即可。 It is in function tick() . 它在函数tick() And I think node.attr(...) calls the function for each node automatically, that's why I don't execute the function F before the node.attr(...) line. 而且我认为node.attr(...)自动为每个节点调用该函数,这就是为什么我不在node.attr(...)行之前执行函数Fnode.attr(...)

Is there any way to do that? 有什么办法吗?

You could pre-compute the values once and store them in your data objects: 您可以预先计算一次值,并将它们存储在数据对象中:

data.forEach(function (d) {
    var result = F(d.x, d.y);
    d.computedX = result[0];
    d.computedY = result[1];
});

And then later: 然后再:

node.attr("cx", function(d) { return d.computedX; })
    .attr("cy", function(d) { return d.computedY; })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM