Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

D3: How to handle zooming and tooltips in a single chart?

I have a visualization that is essentially a series of stacked bar charts, each of which contains several panels. For example, here are three such bar charts, each with four panels.

I have managed to implement pan/zoom functionality that is coordinated across the charts. This is what it looks like if I zoom into the third panel from the last image, for example. The zoom behavior is called from an invisible rectangle that is placed over each chart.

My problem is that I want to enable tooltip functionality based on the location of the user's cursor within a plot. Since the zoom-rectangles are placed on top of the charts, however, no mouse events are registered for any SVG elements in the actual charts themselves.

Des anyone know a way around this?

like image 387
tddevlin Avatar asked Jul 01 '16 17:07

tddevlin


People also ask

Which method is used for adding tooltips in d3?

There are two possible methods of creating tooltips in D3. js. The first method is by creating the SVG <title> tags as a descendant of an interactable element. The second approach is to use mouseover , mosueleave , and mousemove events to dynamically move and change the visibility of a tooltip.


2 Answers

I was following Mike Bostock's example, and like you placing a rect across my whole chart and then calling the zoom behaviour on that, and like you found that it was consuming all the pointer events.

I found an example here that seemed to be achieving what I wanted, and I found that if I scrap the rect and just call the zoom behaviour on the svg element directly, I still get pointer events for the child elements.

I'm a noob here, I don't really understand why this works. I also guess this might have its own ramifications e.g. I guess this stops you limiting the area of your graphic in which mouse events cause a zoom. You may notice that the example I linked creates a sub-svg; I don't know, but perhaps this is to solve that problem.

like image 169
Brendan Avatar answered Oct 26 '22 00:10

Brendan


In your css put the style ponter-events:none for your zooming rectangles. That way the cursor events will be sensed by the elements blow.

like image 32
m fox Avatar answered Oct 26 '22 00:10

m fox