注:本文在spyder中生成离线plotly甘特图,pycharm整体上差不多
1.使用plotly自带甘特图函数create_gantt
在此之前查阅了部分资料和博客,发现基本上是使用字典等字段导入数据,如:
df = [dict(Task = "机位1", Start = '2021-01-01', Finish = '2021-05-28'),
dict(Task = "机位2", Start = '2021-03-05', Finish = '2021-04-15'),
dict(Task = "机位3", Start = '2021-03-20', Finish = '2021-05-30')]
这在数据量大的程序中就十分不方便,这里可以使用pandas导入列表数据
import plotly as py
import plotly.figure_factory as ff
import pandas as pd
pyoff = py.offline.plot
df = pd.DataFrame({
"Task":["机位1","机位2","机位3"],
"Start":["2021-01-01","2021-02-25","2021-04-01"],
"Finish":["2021-01-28","2021-03-19","2021-06-30"]})
fig = ff.create_gantt(df)
pyoff(fig, filename='hehe.html')
2、多时间线甘特图
ployly自带的create_gantt方法在一个项目后只能产生一段时间线,这对多项目多时间线的管理而言十分不方便,即使在Task项中输入相同的的项目名也只是在y轴生成一个名字相同的项目,因此这里采用了timeline设置参数:
import plotly as py
import pandas as pd
import plotly_express as px
pyoff = py.offline.plot
data = pd.DataFrame({
"Task":["机位1","机位2","机位4","机位4"],
"Start":["2021-01-01","2021-02-25","2021-04-01","2021-08-01"],
"End":["2021-01-28","2021-03-19","2021-06-30","2021-12-01"],})
fig = px.timeline(data,x_start="Start",
x_end="End",
y="Task",
color="Task") #这里有些取巧,color参数并没用来设置颜色,当color参数与y参数一致时就会同时出现多条对应时间线,固直接把Task列表传给color参数
pyoff(fig, filename='hehe.html')