Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

D3.js: concatenate selections?

Tags:

d3.js

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.

like image 653
Richard Avatar asked Oct 15 '13 13:10

Richard


1 Answers

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.

like image 122
Lars Kotthoff Avatar answered Sep 28 '22 01:09

Lars Kotthoff