不能把甘特图画成破折号

Can not put gantt plot to dash

我在 plotly 上做了甘特图。但现在我想用下拉菜单把它放在破折号上。因为我是破折号的新手,所以我确实生成了下拉菜单,但我正在努力将我的甘特图放在破折号上,我的输入应该是下拉菜单中的值,输出应该是从中选择的值的线(图)下拉式菜单。

非常感谢您的帮助。谢谢。

我认为您在导入、变量名和格式化等几件事上不够小心。 先从plotly版本开始:

import plotly.offline as py
import plotly.figure_factory as ff
import pandas as pd

df = pd.DataFrame({'ObjectID': ['ITDM-1', 'ITDM-1', 'ITDM-1', 'ITDM-1',
                                'ITDM-10', 'ITDM-10', 'ITDM-10',
                                'ITDM-101', 'ITDM-101', 'ITDM-101'],
                   'Phase': ['phasezero', 'phaseone', 'phasetwo',
                             'phasethree', 'phasezero', 'phaseone',
                             'phasetwo', 'phasezero', 'phaseone', 'phasetwo'],
                   'StartDate': ['2016-12-1', '2017-3-22', '2017-8-21', '2017-9-21',
                                 '2016-12-1', '2016-12-5','2016-12-9', '2017-5-11',
                                 '2017-5-12', '2017-8-17'],
                   'EndDate': ['2017-5-22', '2017-8-21', '2017-9-21',  '2017-12-22',
                               '2017-2-5', '2017-4-9',  '2016-12-13', '2017-5-12',
                               '2017-8-17',  '2017-10-5']})


def gantt_fig(df):
    data = []

    for row in df.itertuples():
        data.append(dict(Task=str(row.Phase), Start=str(row.StartDate),     Finish=str(row.EndDate), Resource=str(row.ObjectID)))

    colors = ['rgb(0, 102, 204)', 'rgb(204, 0, 0)', 'rgb(0, 153, 0)']

    fig = ff.create_gantt(data, index_col='Resource', reverse_colors=True, show_colorbar=True, showgrid_x=True, title='Gantt Chart')
    fig['layout'].update( margin=dict(l=310))

    return fig


fig = gantt_fig(df)
py.iplot(fig)

从这里你应该尝试将它翻译成 dash(再次)注意你如何命名事物(与你的代码比较)

import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import plotly.figure_factory as ff

import pandas as pd


def gantt_fig(df):
    data = []

    for row in df.itertuples():
        data.append(dict(Task=str(row.Phase), Start=str(row.StartDate),
                         Finish=str(row.EndDate), Resource=str(row.ObjectID)))

    colors = ['rgb(0, 102, 204)', 'rgb(204, 0, 0)', 'rgb(0, 153, 0)']

    fig = ff.create_gantt(data, index_col='Resource',
                          reverse_colors=True, show_colorbar=True,
                          showgrid_x=True, title='Gantt Chart')
    fig['layout'].update( margin=dict(l=310))

    return fig


df = pd.DataFrame({'ObjectID': ['ITDM-1', 'ITDM-1', 'ITDM-1', 'ITDM-1',
                                'ITDM-10', 'ITDM-10', 'ITDM-10',
                                'ITDM-101', 'ITDM-101', 'ITDM-101'],
                   'Phase': ['phasezero', 'phaseone', 'phasetwo', 'phasethree',
                             'phasezero', 'phaseone', 'phasetwo', 'phasezero',
                             'phaseone', 'phasetwo'],
                   'StartDate': ['2016-12-1', '2017-3-22', '2017-8-21', '2017-9-21',
                                 '2016-12-1', '2016-12-5', '2016-12-9', '2017-5-11',
                                 '2017-5-12', '2017-8-17'],
                   'EndDate': ['2017-5-22', '2017-8-21', '2017-9-21',  '2017-12-22',
                               '2017-2-5', '2017-4-9',  '2016-12-13', '2017-5-12',
                               '2017-8-17',  '2017-10-5']})

options = df['ObjectID'].unique()


app = dash.Dash()

app.layout = html.Div([html.H1('Gantt table'),
                       dcc.Dropdown(id='my-dropdown',
                                    options=[{'label': n, 'value': n}
                                             for n in options],
                                    value=options[0]),
                       dcc.Graph(id='display-selected-value')
                      ]
                     )



@app.callback(
    dash.dependencies.Output('display-selected-value', 'figure'),
    [dash.dependencies.Input('my-dropdown', 'value')])
def update_gantt(value):
    df2plot = df[df['ObjectID']==value].reset_index(drop=True)
    fig = gantt_fig(df2plot)
    return fig


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

作为一般性建议,我将首先编写一个函数,return fig 你想用 plotly 绘制。然后移动到破折号,您可以检查 this 之后的下拉菜单是否正常工作,然后添加 fig.

的回调