Hi, I have been trying to create a web app to analyse a GPS trace. I need to get specific portions of the trace to compile relevant data from that section. I think what I need is to get the relayOut data from an event, for now selection-data fits well. With the min and max ‘x’ values redraw the figure within that range of values.
I have looked at the following docs and a similar question.
https://dash.plotly.com/clientside-callbacks
https://plotly.com/javascript/plotlyjs-function-reference/
https://community.plotly.com/t/simple-dash-app-yaxis-autoscale-on-xaxis-zoom-in-stock-market-plot/40463/2
I have pieced together some example code which I think is close but not quite there. Any help on this would be appreciated.
Thanks
import pandas as pd
import numpy as np
from dash import Dash, dcc, html, Input, Output, State
import plotly.graph_objects as go
import json
import dash
#create demo data
Bias=10
d = np.arange(0,650, 5).tolist()
s = np.abs(np.sin(d))
s = s + Bias
styles = {'pre': {'border': 'thin lightgrey solid','overflowX': 'scroll'}}
df = pd.DataFrame({'distance': d,'speed': s})
app = Dash(__name__)
#create dash graph
fig = go.Figure()
fig.add_trace(go.Scatter(x=df['distance'], y=df['speed'],
mode='lines+markers',
name='speed'))
app.layout = html.Div([
html.Div([
dcc.Dropdown(
id='distance-selection',
options=[
{'label': '250m', 'value': 250},
{'label': '500m', 'value': 500},
],
value=500,
clearable=False
)], style={'width': '15%', 'display': 'inline-block'}
),
html.Div([
dcc.Graph(
id='demo-graph',
figure=fig
), dash.html.Div(id="where")
]),
html.Div([
dcc.Graph(
id='demo-graph2',
figure=fig
),
]),
html.Div(className='row', children=[
html.Div([
dcc.Markdown("""
**Selection Data**
"""),
html.Pre(id='selection-data', style=styles['pre']),
], style={'width': '49%', 'float': 'right'}),
])
])
@app.callback(
Output('selection-data', 'children'),
Input('demo-graph', 'selectedData'))
def display_selected_data(selectedData):
return json.dumps(selectedData, indent=2)
@app.callback(
Output('demo-graph2', 'figure'),
Input('demo-graph', 'relayoutData'),
Input('demo-graph', 'figure'))
def update_graph(relOut, Fig):
xmin = df.loc[relOut["xaxis.range"]].min()
xmax = df.loc[relOut["xaxis.range"]].max()
dff = df[df['distance'].between(xmin, xmax)]
newLayout = go.Layout(
title="New Graph",
xaxis=dff['distance'],
yaxis=dff['speed']
)
Fig['layout'] = newLayout
return Fig
"""
app.clientside_callback(
function(relOut, Figure) {
fromS = relOut["xaxis.range"][0]
toS = relOut["xaxis.range"][1]
Figure.layout.xaxis = {
'range': [fromS,toS]
}
return {'layout': Figure.layout}:
}
,
Output('demo-graph2', 'figure'),
Input('demo-graph', 'relayoutData')
)
"""
if __name__ == '__main__':
app.run_server(debug=True)