如何对甘特条进行聚类而不重叠?

2023-12-19

使用 create_gantt 我有重叠的开始日期和结束日期:

import plotly.plotly as py
import plotly.figure_factory as ff
import plotly

df = [dict(Task="Milestone A", Start='2017-01-01', Finish='2017-02-02', Resource='Jack'),
      dict(Task="Milestone B", Start='2018-01-01', Finish='2018-02-02', Resource='Jack'),
      dict(Task="Milestone A", Start='2017-01-17', Finish='2017-04-28', Resource='Joe'),
      dict(Task="Milestone B", Start='2017-03-17', Finish='2017-04-28', Resource='Joe'),
      dict(Task="Milestone A", Start='2017-01-14', Finish='2017-03-14', Resource='John'),
      dict(Task="Milestone B", Start='2018-01-14', Finish='2018-03-14', Resource='John')]

colors = {'Jack': 'rgb(220, 0, 0)',
          'Joe': (1, 0.9, 0.16),
          'John': 'rgb(0, 255, 100)'}

fig = ff.create_gantt(df, colors=colors, index_col='Resource', show_colorbar=True, group_tasks=True)

plotly.offline.plot(fig, filename='gantt-group-tasks-together')

Joe、Jack 和 John 的条形图与里程碑 A 和里程碑 B 重叠:

我想要 Joe、John 和 Jack 的里程碑 A 的 3 行聚集但不重叠:

如何实现这一目标?


怎么样altair-viz (docs https://altair-viz.github.io/)?

import pandas as pd
import numpy as np
import altair as alt
# alt.renderers.enable('notebook') # if in jupyter

df = pd.read_csv("tasks.csv")
df["Start date"] = pd.to_datetime(df["Start date"])
df["End date"] = pd.to_datetime(df["End date"])

chart = alt.Chart(df.drop("Resources", 1)).mark_bar().encode(
    x='Start date',
    x2='End date',
    y=alt.Y('Task Name', 
            sort=list(df.sort_values(["End date", "Start date"])
                                    ["Task Name"])), # Custom sorting
)

chart

示例 df:

-  -----------------------------------  -------------------  -------------------  ---------------------------------
0  Data type handling / Auto Inference  2019-07-01 00:00:00  2019-07-31 00:00:00  Backend
1  Sklearn & other models               2019-07-01 00:00:00  2019-07-31 00:00:00  Models
2  Maps / Geoplotting                   2019-07-01 00:00:00  2019-07-31 00:00:00  Backend, Graphical User Interface
3  Optimize Dockerfile                  2019-07-01 00:00:00  2019-07-31 00:00:00  CI/CD
4  Chapter 2: Compare competitors       2019-07-08 00:00:00  2019-10-21 00:00:00  Writing
-  -----------------------------------  -------------------  -------------------  ---------------------------------

编辑:我还找到了一种添加文本并使其看起来像有进度条的方法。它的工作原理是创建另一个系列,其条形高度等于original * progress并将其附加到原始数据框

# Use the progress to find how much of the bars should be filled
# (i.e. another end date)
df["progress date"] =  (df["End date"] - df["Start date"]) * df["Progress %"] / 100 + df["Start date"]

# Concatenate the two 
newdf = np.concatenate([df[["Task Name", "Start date", "End date", "Progress %"]].values,  
                        df[["Task Name", "Start date", "progress date", "Progress %"]].values])
newdf = pd.DataFrame(newdf, columns=["Task Name", "Start date", "End date", "Progress %"])

# Reconvert back to datetime
newdf["Start date"] = pd.to_datetime(newdf["Start date"])
newdf["End date"] = pd.to_datetime(newdf["End date"])

# This is the indicator variable (duration vs progress) where the grouping takes place
newdf["progress_"] = np.concatenate([np.ones(len(newdf)//2), np.zeros(len(newdf)//2), ])

# color for first half, color for second half
range_ = ['#1f77b4', '#5fa0d4',]

# The stacked bar chart will be our "gantt with progress"
chart = alt.Chart(newdf).mark_bar().encode(
    x=alt.X('Start date', stack=None),
    x2='End date',
    y=alt.Y('Task Name', sort=list(df.sort_values(["End date",
                                                      "Start date"])["Task Name"])*2),
    color=alt.Color('progress_', scale=alt.Scale(range=range_), legend=None)
) 

# Create appropriate labels
newdf["text%"] = newdf["Progress %"].astype(str) + " %"

# And now add those as text in the graph
text = alt.Chart(newdf).mark_text(align='left', baseline='middle', dx=5, color="white",  fontWeight="bold").encode(
    y=alt.Y('Task Name', sort=list(df.sort_values(["End date",
                                                      "Start date"])["Task Name"])*2),
    x=alt.X('Start date'),
    text='text%',
)

# Plot the graph
alt.layer(chart, text)

Result: enter image description here

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何对甘特条进行聚类而不重叠? 的相关文章

随机推荐