Hossein Kalbasi
Hossein Kalbasi

Reputation: 1861

Control the Bokeh xwheel_zoom tool using a Bokeh widget/callback

I am using xwheel_zoom (WheelZoomTool) for a Bokeh chart with datetime axis.

p = figure(x_axis_type="datetime", tools='xwheel_zoom, crosshair, undo, reset')

I provide pandas TimeStamp as the x value of this chart. For example:pd.Timestamp.now(tz='utc'). The x axis range of this chart is for the last 24 hours.

start = pd.Timestamp.now(tz='utc') - pd.Timedelta(hours=24)

Using xwheel_zoom, I could zoom in to see my chart better for a given time (e.g. last hour).

Is there any way in Bokeh I could achieve this zoom functionality by coding or connecting to the xwheel_zoom and controling it with a Bokeh widget (e.g. Bokeh dropdown)?

My objective is to have a button to click on and let it show me the zoomed in x_axis for the last hour, or show the chart between a datetime period I define. Idealy, I do not want to re-define/re-draw the chart again and just want to control the xwheel_zoom functionality.

Upvotes: 1

Views: 425

Answers (1)

Eugene Pakhomov
Eugene Pakhomov

Reputation: 10652

You don't need any tools to do that. Just change the desired range in a callback. Something like:

b = Button()

def update():
    p.x_range.update(start=0, end=1)

b.on_click(update)

The example will work only if used with bokeh serve. If you're not using that, you can rewrite the code to work with CustomJS and js_on_click.

Upvotes: 2

Related Questions