How do I get a "Range Select Tool"?

@Bryan
I want to make the overlay reappear when I click on the figure (with the RangeTool on), without having to do it programmatically. I guess this can also be achieved with a click callback?

Then there is still the problem that when I retrieve the start and end values from the range programmatically, I want to have some obvious, unambigous values that let me know when that range was unset or not set. So just setting the alpha to zero or moving it out of view is not good enough.

NaN values are not accepted it seems. But I realized I can set the end value of the x_range of the Range1d smaller than the start value (e.g. start=0, end=-1). Is there any possibility that this would happen (i.e., x_range.end < x_range.start) if the range is set by the user with the mouse? If not, this could be a good way to signify “unset”.

@gmerritt123
Thanks for being interested to help. It seems that what I want to achieve may be possible, but my JS is not good enough (yet?). It’s difficult to give my code, as it involves a lot of other stuff. But what I want to get is basically the RangeTool, which is not in view/set from the start. When the RangeTool is turned on and I click on the figure, it should create a range. When I click outside of the figure again, the range should disappear/be unset again.

I also want that when I retrieve the values of the range programmatically, there are are some unambiguous values to tell me that the range is “unset”. start=0, end=-1 may be a good possibility (see above).

I guess an MRE would perhaps be:

import numpy as np
from bokeh.plotting import figure, show
from bokeh.models import RangeTool
from bokeh.models.ranges import Range1d

x = np.arange(0,20,0.001)
t = np.datetime64('2022-08-16T12:00:00','ms') + np.timedelta64(1000,'ms')*x

p = figure(x_axis_type='datetime')
p.line(t,np.sin(x))
p.add_tools(RangeTool(x_range=Range1d(start=0, end=-1)))
show(p)

Note that I set the start and end values to 0 and -1, which means that the range will be out of view, as well as when I do p.tools[-1].x_range.start and p.tools[-1].x_range.start I get 0 and -1 as output (of course only works when using a server and not with show). This lets me know the range is “unset”.

What I still miss is:

  • The range should be invisible at start.
  • When I click on the figure (or try to make a range) with the RangeTool on, the range should become visible and be set accordingly.
  • And when I click again outside of the figure, the range start and end should be set again to 0 and -1, and the overlay invisible again.