D3.js: combine the election?

Is there a way in D3 to concatenate choices?

Use case: I would like to add a mouseover event to both the update and enter a selection for a specific selection.

I can do it like this:

var s = d3.selectAll('.yellow').data(myData); s.on('mouseover'... s.enter().append('path').attr('class','yellow').on('mouseover'... 

But I would prefer to do this with a single line of code.

+6
source share
1 answer

In this particular case, you do not need to concatenate - the input selection merges with the update selection after it is called, so all you have to do is process .enter() before selecting the update.

In general, you cannot really combine choices per se, but in practice this is not necessary. You can either change the selection condition to select all the elements you need, or, if this is not possible, use .call() to run the function for all selected elements. This way you do not need to repeat the code to set attributes, etc.

+7
source

Source: https://habr.com/ru/post/955985/


All Articles