Displaying only one hint when using the HoverTool () tool

I draw a lot of dots in Bokeh, and I added HoverTool to the list of shape tools, so the mouse shows the x,y coordinates of the mouse when close to the glyph.

As the mouse approaches a set of glyphs that are closely related to each other, I get as many clues as glyphs. Instead, I want only one hint, one of the nearest glyphs. This is not just a detail of the presentation, because for so many points this result is:

  • in slow interaction with the chart, when the browser gets stuck, when tooltips pop up
  • in a very long tooltip, where the same information is repeated as many times as there are glyphs close to the cursor

The following is an example with code for replicating behavior: enter image description here

 import numpy.random from bokeh.plotting import figure, output_notebook, show from bokeh.models import HoverTool output_notebook() hover = HoverTool() hover.tooltips = [("(x,y)", "($x, $y)")] x = numpy.random.randn(500) y = numpy.random.randn(500) p = figure(tools=[hover]) p.circle(x,y, color='red', size=14, alpha=0.4) show(p) 
+2
source share
1 answer

I had a similar problem and came up with a solution using a special hint. I am inserting a style tag at the top that displays only the first child div in the .bk-tooltip class, which is the first hint.

Here is a working example:

 from bokeh.plotting import figure, show from bokeh.models import HoverTool, Range1d custom_hover = HoverTool() custom_hover.tooltips = """ <style> .bk-tooltip>div:not(:first-child) {display:none;} </style> <b>X: </b> @x <br> <b>Y: </b> @y """ p = figure(tools=[custom_hover]) #Custom behavior #p = figure(tools=['hover']) #Default behavior p.circle(x=[0.75,0.75,1.25,1.25], y=[0.75,1.25,0.75,1.25], size=230, color='red', fill_alpha=0.2) p.y_range = Range1d(0,2) p.x_range = Range1d(0,2) show(p) 

This is a kind of hacker solution, but it works in Safari, Firefox and Chrome. I think they will come out soon with a longer term.

+2
source

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


All Articles