Wednesday, 5 June 2019

Hover tool for plotly slider widget (python)

I am using python 3.6.5 and plotly 3.9.0 to create an interactive line graph that the user can change the range using a ranger slide.

I would like to add a hover tool to the range slider so that when the user moves the slider, a hover icon says the new date range before the user releases the mouse.

I think this is the default on bokeh, but I have given up on this and moved to plotly-dash.

Can this be done?

A minimum working example of what I am trying to do is below.

import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objs as go
import plotly.plotly as py

from datetime import datetime
import pandas as pd
import numpy as np

np.random.seed(10)
df = pd.DataFrame({
    'date':pd.date_range(start='1/1/2000', periods=200),
    'x': np.random.choice(range(100),200)
})

app = dash.Dash()
app.layout = html.Div([
    dcc.Graph(
        id='graph',
    ),

    dcc.RangeSlider(
        id='slider',
        min = df['date'].min().timestamp(),
        max = df['date'].max().timestamp(),
        value=[df.iloc[-101]['date'].timestamp(), df.iloc[-1]['date'].timestamp()]
    )

])


@app.callback(
    dash.dependencies.Output('graph','figure'),
    [dash.dependencies.Input('slider','value')])

def update_figure(value):
    lBound = pd.to_datetime(value[0], unit='s')
    uBound = pd.to_datetime(value[1], unit='s')
    filteredData = df.loc[(df['date']>=lBound) & (df['date']<=uBound)]
    fig = [
        go.Scatter(
            x=filteredData['date'],
            y=filteredData['x'],
            mode='lines',
            name='xxxx'
        )
    ]

    layout = go.Layout(
                xaxis={'title': ' '},
                yaxis={'title': 'per cent'},
                hovermode='closest')

    return {'data':fig,'layout':layout}


if __name__ == '__main__':
    app.run_server(debug=True)



from Hover tool for plotly slider widget (python)

No comments:

Post a Comment