有没有办法从同一个函数设置多个属性?
d3.selectAll('.myshape')
.attr('y',function(d,i) { ... calculates something ... })
.attr('height',function(d,i) { ... calculates something very similar... })
Run Code Online (Sandbox Code Playgroud)
我想计算y1,并y2在同一时间,然后设置y = y1和height = y2-y1。但是在 d3 中这样做的标准方法似乎是每个属性都有单独的函数。有没有更好的办法?
如果我正确理解您的问题,您有一个密集的计算,您希望每个 element只计算一次,无论您设置的属性数量如何。
在这种情况下,如果需要,您可以使用 aneach传递当前元素 ( this) 以及数据、索引和组:
d3.selectAll(".myShape").each(function(d,i,n){
//Here you put the complex calculation
//which will assign the values of y1 and y2.
//This calculation runs only once per element
d3.select(this).attr("y", y1).attr("height", y2 - y1)
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
769 次 |
| 最近记录: |