D3 v4 - 访问选择数组并找到相应的元素 [英] D3 v4 - Accessing selection array and find corresponding element
问题描述
我试图得到一个约束放松为我的饼图工作。
它基于此示例 https://jsfiddle.net/thudfactor/HdwTH/ 但是使用的放松方法似乎不再适用于v4。
具体的问题是他们如何直接访问选择组数组:
I'm trying to get a constraint relaxing to work for my piechart.
It is based on this example https://jsfiddle.net/thudfactor/HdwTH/ but the relaxing method used seems not to work with v4 any more.
The concrete problem is how they access the selection group array directly:
textLabels = labelGroups.append("text").attr( ... );
if(again) {
labelElements = textLabels[0]; <------------- here
textLines.attr("y2",function(d,i) {
labelForLine = d3.select(labelElements[i]);
return labelForLine.attr("y");
});
setTimeout(relax,20)
}
.x如何访问选择组数组?
现在怎么办?
Has it changed with D3 v4.x how you access a selections group array?
How would you go about it now?
推荐答案
在D3 4.0中,选择不再是数组。根据API:
In D3 4.0, selections are not arrays anymore. According to the API:
选择不再使用原型链注入的子类Array;它们现在是平面对象,提高性能。
Selections no longer subclass Array using prototype chain injection; they are now plain objects, improving performance.
因此,如果您 console.log(textLabels)
看到这样的东西:
So, if you console.log(textLabels)
, you're gonna see something like this:
{_groups: Array[1], _parents: Array[1]}
根据您所选择的内容。从这里,你可以使用 textLabels._groups
访问你的数组。
Depending on exactly what are you selecting. From there, you can access your array using textLabels._groups
, for instance.
必须使用 selection.nodes()
,根据API:
For having an array, you have to use selection.nodes()
, which, according to the API:
返回此选择中所有(非空)元素的数组。
Returns an array of all (non-null) elements in this selection.
这篇关于D3 v4 - 访问选择数组并找到相应的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!