Dash:具有多个下拉选择条件的graph_update

问题描述 投票:0回答:1

你好,我是一个非常新的破折号,我试图渲染一个图表,其outpuut将取决于layput上的两个下拉选择我写了下面的graph_update逻辑,但不知何故它不是workig。

app.layout = html.Div([
    html.Div([
        html.H1('Stock Tickers'),
        dcc.Dropdown(
            id='my-dropdown',
            options=[
                {'label': 'A', 'value': 'A'},
                {'label': 'B', 'value': 'B'},
                {'label': 'C', 'value': 'C'}
            ],
            value='A'
        )
    ],
        style={'width': '20%', 'display': 'inline-block'}
    ),
    dcc.Dropdown(
        id='my-dropdown1',
        options=[
            {'label': '250,000', 'value': '250000'},
            {'label': '500,000', 'value': '500000'},
            {'label': '750,000', 'value': '750000'},
            {'label': '1,000,000', 'value': '1000000'}
        ],
        value='250000'
    ),
    dcc.Graph(id='my-graph')
], className="container")

@app.callback(Output('my-graph', 'figure'),
              [Input('my-dropdown', 'value'), Input('my-dropdown1', 'value')])

 def update_graph(selected_dropdown_value, selected_imp_value):
     dff = df[(df['Demo'] == selected_dropdown_value) & (df['Imp_cap'] == selected_impresession_value)]
     return {
         'data': [{
             'x': dff.Imp
             'y': dff.user,
             'line': {
                 'width': 3,
                 'shape': 'spline'
             }
         }],
         'layout': {
             'margin': {
                 'l': 30,
                 'r': 20,
                 'b': 30,
                 't': 20
             }
         }
     }

我希望有人可以帮我解决这个问题

非常感谢提前!!

python graph plotly plotly-dash
1个回答
1
投票

这就是我们设法做到的方式。这不是特别天才,只是写作的方式。

def update_graph(n_clicks, input1, input2, input3, input4):
    dff = df[df['Demo'] == input1]
    df1 = dff[dff['Month'] == input2]
    df2 = df1[df1['Imp'] == input3]
    df3 = df2[df2['imp_cap'] == input4]
    if n_clicks < 1:
        return []
    else:
        return {
            'data': [{
                'x': df3.Variable2,
                'y': df3.Variable1,
                'line': {
                    'width': 3,
                    'shape': 'spline'
                }
            }],
            'layout': dict(
                # 'margin': {
                #     'l': 30,
                #     'r': 20,
                #     'b': 30,
                #     't': 50
                # },
                title='title',
                xaxis=dict(title='x-title'),
                yaxis=dict(title='y-title'),
                annotations=[make_annotation_item(x=df3['Variable1'].iloc[-1], y=df3['Variable2'].iloc[-1])]
            )
        }
© www.soinside.com 2019 - 2024. All rights reserved.