簡體   English   中英

更新圖無效組件道具圖時出錯

[英]Error updating graph invalid component prop figure

只需從 excel 文件創建一個簡單的圖形,它就可以正確加載,但在更新圖形破折號時會返回此錯誤:

Failed component prop type: Invalid component prop figure key props supplied to Graph.

Bad object: {

“props”: {

“id”: “average_country”,

“figure”: {

“data”: [

{
“x”: [
“DE”,
“ES”,
“FR”,
“IT”,
“UK”
],

“y”: [
[
2365.56,
4528.33875,
4851.085,
4325.14,
2107.921428571429
]
],

“type”: “bar”
}
],

“layout”: {

“title”: “Hyperzone”
}
}
},

“type”: “Graph”,

“namespace”: “dash_core_components”
}

Valid keys: [
“data”,
“layout”,
“frames”
]

我只是使用 pivot 表來創建某些國家/地區的數據平均值,這是我的代碼,pivot 表是正確的,並輸出具有我想要的值的有效結果,就在我嘗試使用回調 ZC1C425268E68385D1ABZ507 更新圖表時它返回一條錯誤消息。

import pandas as pd
import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objs as go
from dash.dependencies import Input, Output

HP= pd.read_excel(r’C:\Users\salinejs\HP.xlsx’)
Columns = [‘Station’, ‘Week’, ‘Owner’, ‘Cost’,‘Country’]
HP_filtered = HP[Columns]

avaliable_weeks= HP_filtered[‘Week’].unique()

external_stylesheets = [‘https://codepen.io/chriddyp/pen/bWLwgP.css’]

app = dash.Dash(name, external_stylesheets=external_stylesheets)

app.layout = html.Div([
html.Div([
html.Div([

    html.Div([
        dcc.Dropdown(
            id= 'Week_filter',
            options = [{'label':j, 'value':j}for j in avaliable_weeks],
            value= 'Week 42'
        ),

    ],
    style= {'float':'left', 'display':'inline-block'}
    ),



    html.Div([
        dcc.Graph(
            id='average_country',
        )
        ],
    )
    ]
),
],

)

@app.callback(
Output(‘average_country’, ‘figure’),
[Input(‘Week_filter’,‘value’)]
)

def update_value(week):

HP_filtered_week = HP_filtered[ HP_filtered['Week'] == week ]

pv = pd.pivot_table(HP_filtered_week, values='Cost', columns='Country', index='Week')

print(pv)
print(pv.columns)

return dcc.Graph(
    id='average_country',
    figure={
        'data': [{'x': pv.columns , 'y' : pv.values,'type': 'bar'
                  }],

        'layout':{
            'title': 'Cost',

        }


    })

if name == ‘main’:
app.run_server(debug=True)

有點晚了-您現在可能已經對此進行了排序,但是我遇到了同樣的錯誤,這里的答案可以節省我的時間。

這是因為您將回調 output 發送到圖,但 function 正在返回整個 dcc.Graph() 而不僅僅是圖。

改變

return dcc.Graph(
    id='average_country',
    figure={
        'data': [{'x': pv.columns , 'y' : pv.values,'type': 'bar'
                  }],

        'layout':{
            'title': 'Cost',

        }


    })

return {'data': [{'x': pv.columns , 'y' : pv.values,'type': 'bar'
                  }],

        'layout':{
            'title': 'Cost',},}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM