Is there a way in D3 to concatenate selections?
Use case: I'd like to add a mouseover event to both the update and enter selections of a particular selection.
I can do this as follows:
var s = d3.selectAll('.yellow').data(myData);
s.on('mouseover'...
s.enter().append('path').attr('class','yellow').on('mouseover'...
But I'd prefer to do it with one line of code.
In this particular case you don't need to concatenate -- the enter selection merges into the update selection after it's been called, so all you need to do is handle .enter()
before the update selection.
In general, you can't really concatenate selections as such, but in practice this isn't really necessary. You can either modify the selection condition to select all the elements you need, or, if this is not possible, use .call()
to run a function on all selected elements. This way you don't need to repeat the code to set attributes etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With