7

I want to create interactive Gantt-Charts (or a sequence chart) for displaying the scheduling of tasks on multiple processors.

I found the library plotly, which produced very good and interactive Gantt-charts. Unfortunately, plotly-Gantt only works with dates and not numerical values, like I have with the runtime values of the schedule.

Is there a possibility to create Gantt-charts in plotly with numerical values?

Code Example: (I would like to use something like this)

import plotly.figure_factory as ff

df = [dict(Task="Job A on Core 0", Start=0, Finish=10),
      dict(Task="Job B on Core 1", Start=2, Finish=8),
      dict(Task="Job C on Core 0", Start=11, Finish=12)]

fig = ff.create_gantt(df)
fig.show()
4

1 回答 1

2

所以我试图让 Plotly 的figure_factory函数create_gantt与数值一起工作。我唯一想到的是一个相当肮脏的解决方法,看起来像这样:

import plotly.figure_factory as ff
from datetime import datetime
import numpy as np

def convert_to_datetime(x):
  return datetime.fromtimestamp(31536000+x*24*3600).strftime("%Y-%d-%m")

df = [dict(Task="Job A", Start=convert_to_datetime(0), Finish=convert_to_datetime(4)),
      dict(Task="Job B", Start=convert_to_datetime(3), Finish=convert_to_datetime(6)),
      dict(Task="Job C", Start=convert_to_datetime(6), Finish=convert_to_datetime(10))]

num_tick_labels = np.linspace(start = 0, stop = 10, num = 11, dtype = int)
date_ticks = [convert_to_datetime(x) for x in num_tick_labels]

fig = ff.create_gantt(df)
fig.layout.xaxis.update({
        'tickvals' : date_ticks,
        'ticktext' : num_tick_labels
        })
fig.write_html('first_figure.html', auto_open=True)

该函数convert_to_datetime接受一个整数并将其转换为从1971-01-01for开始的日期时间字符串x=0,每增加 1 天就增加一天x。此函数用于将您可能希望在甘特图中使用的所有数值转换为日期字符串。在这里,我只是插入了从0to的整数10来展示这确实有效。

然后对于刻度标签,最低 ( 0) 和最大 ( 10) 值用于创建均匀分布的刻度标签。然后这些整数也使用列表推导转换为日期字符串。

最后,如果您执行整个操作,您将获得一个交互式甘特图,如下所示:

使用 Plotly 的交互式甘特图

我相信这种方法绝对可以改进以获得更好的工作流程,但您可以将其作为起点。

于 2019-08-28T18:24:39.363 回答