简体   繁体   English

D3 v4 - 访问选择数组并找到相应的元素

[英]D3 v4 - Accessing selection array and find corresponding element

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. 它基于这个示例https://jsfiddle.net/thudfactor/HdwTH/但是使用的放松方法似乎不再适用于v4。
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)
    }

Has it changed with D3 v4.x how you access a selections group array? 使用D3 v4.x更改了如何访问选择组数组?
How would you go about it now? 你现在怎么样?

In D3 4.0, selections are not arrays anymore. 在D3 4.0中,选择不再是数组。 According to the API: 根据API:

Selections no longer subclass Array using prototype chain injection; 选择不再使用原型链注入子类化Array; they are now plain objects, improving performance. 它们现在是普通物体,提高了性能。

So, if you console.log(textLabels) , you're gonna see something like this: 所以,如果你使用console.log(textLabels) ,你会看到类似这样的东西:

{_groups: Array[1], _parents: Array[1]}

Depending on exactly what are you selecting. 具体取决于您选择的是什么。 From there, you can access your array using textLabels._groups , for instance. 从那里,您可以使用textLabels._groups访问您的阵列。

For having an array, you have to use selection.nodes() , which, according to the API: 对于拥有数组,您必须使用selection.nodes() ,根据API:

Returns an array of all (non-null) elements in this selection. 返回此选择中所有(非null)元素的数组。

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

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