D3 v4 - 访问选择数组并找到相应的元素 [英] D3 v4 - Accessing selection array and find corresponding element

查看:14
本文介绍了D3 v4 - 访问选择数组并找到相应的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试放松约束来为我的饼图工作.它基于此示例 https://jsfiddle.net/thudfactor/HdwTH/ 但放松方法used 似乎不再适用于 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)
    }

它在 D3 v4.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆