简体   繁体   English

Plotly Express:尝试添加按钮以重新绘制甘特图/时间线

[英]Plotly Express: trying to add button to re-draw Gantt chart/timeline

I'm trying to produce a Gantt-type timeline chart with additional controls (buttons, etc) to re-draw the chart based on different formatting needs.我正在尝试生成带有附加控件(按钮等)的甘特型时间线图,以根据不同的格式需求重新绘制图表。 I'm working from the basic example shown in the Plotly documentation: https://plotly.com/python/gantt/我正在使用 Plotly 文档中显示的基本示例: https://plotly.com/python/gantt/

I'm trying to add a button that would allow for re-grouping events by task, or by resource (or other categorical variables).我正在尝试添加一个按钮,该按钮允许按任务或资源(或其他分类变量)对事件进行重新分组。 The following code is my first attempt.以下代码是我的第一次尝试。 It does allow me to re-draw the figure once, but subsequent clicks doesn't switch the format back:它确实允许我重新绘制一次图形,但随后的单击不会将格式切换回来:

import plotly.express as px
import pandas as pd

df = pd.DataFrame([
    dict(Task="Job A", Start='2009-01-01', Finish='2009-02-28', Resource="Alex"),
    dict(Task="Job B", Start='2009-03-05', Finish='2009-04-15', Resource="Alex"),
    dict(Task="Job C", Start='2009-02-20', Finish='2009-05-30', Resource="Max")
])

fig = px.timeline(df, x_start="Start", x_end="Finish", y="Task", color="Resource")

fig.update_layout(
    updatemenus=[
        dict(
            type = "buttons",
            direction = "left",
            buttons=list([
                dict(
                    args=[{"y" : "Resource"}],
                    label="By Resource",
                    method="restyle"
                ),
                dict(
                    args=[{"y": "Task"}],
                    label="By Task",
                    method="restyle"
                )
            ]),
            pad={"r": 10, "t": 10},
            showactive=True,
            x=0.11,
            xanchor="left",
            y=1.1,
            yanchor="top"
        ),
    ]
)

fig.show()

Am I misconfiguring the button/not using the update_layout and updatemenus methods correctly?我是否错误地配置了按钮/没有正确使用 update_layout 和 updatemenus 方法?

I don't think plotly.express allows switching with the custom button, so in this case I would use the data created in plotly.express to draw the graph.我不认为 plotly.express 允许使用自定义按钮进行切换,因此在这种情况下,我将使用 plotly.express 中创建的数据来绘制图形。 Display the initial graph with the task criteria and hide the resource criteria.显示带有任务条件的初始图表并隐藏资源条件。 Set the buttons to show/hide in the list according to each button.根据每个按钮设置按钮在列表中显示/隐藏。 As an adjustment to the graph, the x-axis is set to date type and the y-axis is set to reverse order.作为对图表的调整,x 轴设置为日期类型,y 轴设置为倒序。

import plotly.express as px
import plotly.graph_objects as go
import pandas as pd

df = pd.DataFrame([
    dict(Task="Job A", Start='2009-01-01', Finish='2009-02-28', Resource="Alex"),
    dict(Task="Job B", Start='2009-03-05', Finish='2009-04-15', Resource="Alex"),
    dict(Task="Job C", Start='2009-02-20', Finish='2009-05-30', Resource="Max")
])

task = px.timeline(df, x_start="Start", x_end="Finish", y="Task", color="Resource")
resource = px.timeline(df, x_start="Start", x_end="Finish", y="Resource", color="Resource")

fig = go.Figure()
fig.add_trace(go.Bar(task.data[0], visible=True))
fig.add_trace(go.Bar(task.data[1], visible=True))
fig.add_trace(go.Bar(resource.data[0], visible=False))
fig.add_trace(go.Bar(resource.data[1], visible=False))

fig.update_xaxes(type='date')
fig.update_yaxes(autorange="reversed")

fig.update_layout(
    updatemenus=[
        dict(
            type = "buttons",
            direction = "left",
            buttons=list([
                dict(
                    args=[{"visible": [True, True, False, False]}],
                    label="By Task",
                    method="update"
                ),                dict(
                    args=[{"visible" : [False, False, True, True]}],
                    label="By Resource",
                    method="update"
                ),
            ]),
            pad={"r": 10, "t": 10},
            showactive=True,
            x=0.01,
            xanchor="left",
            y=1.30,
            yanchor="top"
        ),
    ]
)

fig.show()

在此处输入图像描述

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM