Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
348 views
in Technique[技术] by (71.8m points)

javascript - 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.
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?
How would you go about it now?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In D3 4.0, selections are not arrays anymore. According to the API:

Selections no longer subclass Array using prototype chain injection; they are now plain objects, improving performance.

So, if you console.log(textLabels), you're gonna see something like this:

{_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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...