在 plotly 中使用数值来创建甘特图 [英] Using numerical values in plotly for creating Gantt-Charts
问题描述
我想创建交互式甘特图(或序列图)来显示多个处理器上的任务调度.
I want to create interactive Gantt-Charts (or a sequence chart) for displaying the scheduling of tasks on multiple processors.
我巧妙地找到了这个库,它生成了非常好的交互式甘特图.不幸的是,plotly-Gantt 只适用于日期,而不适用于数值,就像我对日程表的运行时值一样.
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()
推荐答案
所以我试图让 Plotly 的 figure_factory
函数 create_gantt
处理数值.我唯一想到的是一个相当肮脏的解决方法,看起来像这样:
So I tried to get Plotly's figure_factory
function create_gantt
to work with numerical values. The only thing that I came up with is a rather dirty work-around that looks like this:
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-01
开始,对于 x=0
并增加x
每增加 1 天.此函数用于将您可能想在甘特图中使用的所有数值转换为日期字符串.在这里,我只是插入了从 0
到 10
的整数来展示这确实有效.
The function convert_to_datetime
takes an integer and converts it to datetime string starting at 1971-01-01
for x=0
and increases by one day per increment of x
. This function is used to convert all numerical values that you might want to use in your Gantt chart into date strings. Here I just inserted integers from 0
to 10
to showcase that this actually works.
然后对于刻度标签,最低 (0
) 和最大 (10
) 值用于创建均匀分布的刻度标签.然后这些整数也使用列表推导转换为日期字符串.
Then for the tick labels the lowest (0
) and largest (10
) values are used to create evenly distributed tick labels. These integers are then also converted to date strings using list comprehension.
最后,如果您执行整个操作,您将获得一个交互式甘特图,如下所示:
Finally if you execute the whole thing you will get an interactive Gantt chart that looks like this:
我相信这种方法肯定可以改进以获得更好的工作流程,但您可以将其用作起点.
I believe this approach can definitely be improved to get a better workflow, but you could use it as a starting point.
这篇关于在 plotly 中使用数值来创建甘特图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!