如何让破折号应用程序按年份显示数据

How to get dash app plotly to display data by year

我是 dash 的新手,我试图让它显示一个基于审计年度的饼图,该饼图将显示金额 'Collected'、'Uncollected'、'Decreased', 'Written off'。这是我使用 plotly 而不是破折号功能对饼图进行编码时的饼图示例。. So here is how it looks now via the dash app

下面是我用来制作仪表板的代码

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

df1=pd.read_csv('Data 4.0.csv')

app=dash.Dash(__name__)

app.layout=html.Div([
    html.Div([
    html.Label(['Audit Yield']),
    dcc.Dropdown(
        id='my_dropdown',
        options=[
            {'label':'Year','value':'Yield Year'},
            {'label':'Status','value':'Status'},
            {'label':'Amount','value':'Amount'}
        ],
        value='Amount',
        multi=False,
        clearable=False,
        style={"width":"50%"}
    )
]),

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

])

@app.callback(
    Output(component_id='the_graph', component_property='figure'),
    [Input(component_id='my_dropdown', component_property='value')]

)

def update_graph(my_dropdown):
    dff=df1
    
    piechart=px.pie(
            data_frame=dff,
            names=my_dropdown,
            hole=.3,
            )
    return(piechart)

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

所以基本上我希望按年份调用仪表板并显示当年收集的金额和钱的去向。就像 7 个饼图每年显示该数据一样。任何帮助或指导将不胜感激 it.Thanks!

这是数据集的 link:Dataset for this code

所以如果我没理解错的话,你想select下拉菜单中的年份并显示相应的饼图?

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

df1 = pd.read_csv('Data 4.0.csv')
years = df1['Yield Year'].unique()

app = dash.Dash(__name__)

app.layout = html.Div([
    html.Div([
        html.Label(['Audit Yield']),
        dcc.Dropdown(
            id='my_dropdown',
            options=[{'label': y, 'value': y} for y in years],
            value=years[0],
            multi=False,
            clearable=False,
            style={"width": "50%"}
        )
    ]),

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

])


@app.callback(
    Output(component_id='the_graph', component_property='figure'),
    [Input(component_id='my_dropdown', component_property='value')]
)
def update_graph(year):
    piechart = px.pie(
        df1[df1['Yield Year'] == int(year)],
        values='Amount',
        names='Status',
        hole=.3,
    )
    return piechart


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

输出: