I have a scatter plot that is generated using D3. Points (SVG circles) on the plot can be selected by clicking on them and regions can be selected using a D3 brush.
To ensure the circles get the click event I need to create the brush first so the circles are above it. Unfortunately this means I can’t drag to create the brush extent when my cursor is over a point in the plot.
Is there a way to pass hover and click events to the circles, but handle drag related events with the brush?
It can be accomplished but with use of the D3 brush API (See note below).
This is an example http://bl.ocks.org/4747894 where:
brushelement is behind the circlesmousedownevent. (Can respond to other events as well.)brushelement is well-behaved even if dragging starts from inside one of the circles.Some tracing and a look at the D3 source code suggests that the
extentis not being reset properly when amousemoveevent is fired from acircleelement atop the brush. That can be fixed by resetting theextentfor the brush in themousedownlistener for thecircleelements:Note: As per the API, the brush’s selection will not be refreshed automatically on calling
.extent(values). Merely clicking on a circle will reset theextentbut will not redraw the selection made. The selection will be discarded only when a different selection is started inside thecircle, or by clicking outside the circles and the current selection. This is the desired behavior, as I understand from the question. However, this might break code which is written with the assumption that whatever is theextentof the brush would be the selection visible on the graph.