Reputation: 1179
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)
Upvotes: 8
Views: 1225
Reputation: 2047
Right now there is no feature for hovering
on slider
for plotly
on dash
.
You can check documentation for Slider and RangeSlider.
I know this is not what you want, but it will be easier to use marks
for the slider to know the position.
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()],
marks={
955411200: {'label': '2000-04-11', 'style': {'color': '#77b0b1'}},
959644800: {'label': '2000-05-30'},
957484800: {'label': '2000-05-05'},
961804800: {'label': '2000-06-24', 'style': {'color': '#f50'}}
}
)
Change your dcc.RangeSlider
to this. It might be better than having no hover.
Else you can use Slider Widget
with just plotly, limited to jupyter
though.
Upvotes: 3