Skip to content Skip to sidebar Skip to footer

D3.js - What Is Selection.call() Returning?

I'm currently working on a force simulation. Here's the fiddle if you need it. This particular section is giving me a bit of trouble - I'm trying it for links and not nodes, where

Solution 1:

Your statements are not equivalent and are, therefore, not comparable. The documentation on selection.call() tells us:

Invokes the specified function exactly once, passing in this selection along with any optional arguments. Returns this selection.

This is to be understood literally: returning this selection means exactly the selection the specified function is called with. Because there are no lines prior to that call your selection will be empty when invoking .call() on it. Although you may successfully use this empty selection within that function to bind data and append the lines, this will not alter the original selection.

Because

Selections are immutable.

the .call() method will nonetheless return the original, i.e. empty, selection, which will then be assigned to link. It is easy to see, that this differs from your other solution when selecting the newly added lines afterwards or when returning the enter selection from your modified function.

It is actually working for the nodes, because in byFrame() you do not access them via node but by doing a svg.selectAll("circle") instead.

I think for this application using

letlink = linkRender(svg.selectAll("line"));

will be the way to go. As you have already mentioned, this will require linkRender() to return the selection properly.

Post a Comment for "D3.js - What Is Selection.call() Returning?"