我正在尝试放宽约束,以便为我的饼图工作。
它基于此示例 https://jsfiddle.net/thudfactor/HdwTH/但是使用的放松方法似乎不再适用于 v4。
具体问题是他们如何直接访问选择组数组:
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)
}
D3 v4.x 访问选择组数组的方式是否发生了变化?
你现在会怎么做?
最佳答案
在 D3 4.0 中,选择不再是数组。根据 API:
Selections no longer subclass Array using prototype chain injection; they are now plain objects, improving performance.
因此,如果您console.log(textLabels)
,您将看到如下内容:
{_groups: Array[1], _parents: Array[1]}
具体取决于您的选择。例如,您可以从那里使用 textLabels._groups
访问您的数组。
要获得数组,您必须使用 selection.nodes()
,根据 API:
Returns an array of all (non-null) elements in this selection.
关于javascript - D3 v4 - 访问选择数组并找到相应的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38702831/