帮助。我试图在剧情中制作甘特图,我只是没有看到 一种方法。 甘特图类似于水平条形图,其中有一个'起始'点 每个栏的坐标。 所以它看起来像:
XXXXX XXXXXX XXXXXXXXXXXXXXXXXXXXX XXXX
我无法找到一种方法来制作'在条形图中从特定的开始 X坐标。 任何提示/技巧/提示?
答案 0 :(得分:1)
以下是我的结果......不是完美的甘特图, (你不能改变条的大小,没有依赖 在条目之间)但足够我的目的:
一个代码片段,用于显示我如何处理透明跟踪:
// loop through all obj's to draw, for each one
// make a transparent offset to mimic gantt chart.
traces.push( {
x: [ obj.totalrunTime ],
y: [ key ],
name: key,
text: [ obj.totalrunTime+' '+key ],
orientation: 'h',
marker: { color: obj.color },
type: 'bar'
});
traces.push( {
x: [ offset ],
y: [ key ],
showlegend: false,
orientation: 'h',
marker: { color: 'rgba(255,255,255,0)' },
hoverinfo: "none",
name: key,
type: 'bar'
});
offset = offset + jobs[key].totalrunTime;
答案 1 :(得分:1)
Riddhiman在R中为此做出了很好的解决方案。 http://moderndata.plot.ly/gantt-charts-in-r-using-plotly/。 因为循环,我在开始时不情愿,但它给了很大的自由。
我为我的需求添加了一些额外的布局:
p <- plot_ly()
for(i in 1:(nrow(df) - 1)){
p <- add_trace(p,
x = c(df$Start[i], df$Start[i] + df$Duration[i]), # x0, x1
y = c(i, i), # y0, y1
mode = "lines+markers+text",
marker = list(color = df$color[i]
,symbol = "line-ns-open"
,size = 13), #markers ensures visability
text = c(df$text[i],"") # adds a text string
textposition = "middle left" #to the left of the bar
line = list(color = df$color[i]
, width = 20),
showlegend = F,
hoverinfo = "text",
# Create custom hover text
text = paste0("<b>Task:</b> ", df$Task[i], "<br>",
"<b>Duration:</b> ", df$Duration[i], " days<br>",
"<b>Resource:</b> ", df$Resource[i]),
evaluate = T # needed to avoid lazy loading
)}
答案 2 :(得分:1)
是的! 将plotly.figure_factory导入为ff
ff.create_gantt(DF)
Plotly内置了甘特图。您无需从条形图创建它们。您可以为其提供字典列表,也可以为其提供数据框。如果您执行后者,请确保告诉数字哪一列是任务,以及您的开始和结束日期。我发现使用datatime并标记Start和Finish列更容易。这样,甘特图将自动读取它们,文档在以下链接中。
答案 3 :(得分:1)
如果使用plotly,如果要在一行中包含相似的键,请使用group_tasks=True
。
答案 4 :(得分:0)
R包vistime
使用Plotly创建甘特图。图表中事件还有一种 intelligent 垂直分布,因此图表不会大于所需的大小。
install.packages("vistime")
library(vistime)
pres <- data.frame(Position = rep(c("President", "Vice"), each = 3),
Name = c("Washington", rep(c("Adams", "Jefferson"), 2), "Burr"),
start = c("1789-03-29", "1797-02-03", "1801-02-03"),
end = c("1797-02-03", "1801-02-03", "1809-02-03"),
color = c('#cbb69d', '#603913', '#c69c6e'),
fontcolor = c("black", "white", "black"))
vistime(pres, events="Position", groups="Name", title="Presidents of the USA")
答案 5 :(得分:0)
这是带有甘图的甘特图
“ test.data”的内容:
格式为:任务 \ t 开始 \ t 完成 \ t 资源
A 2017-04-26 10:12:04 2017-04-26 10:34:18 Done
B 2017-04-26 10:54:18 2017-04-26 11:07:41 Done
C 2017-04-26 11:47:42 2017-04-26 12:25:12 Done
A 2017-04-26 12:35:12 2017-04-26 13:28:29 Done
B 2017-04-26 13:48:29 2017-04-26 14:07:50 Done
A 2017-04-26 14:37:50 2017-04-26 15:12:08 Done
B 2017-04-26 15:32:09 2017-04-26 15:44:43 Done
C 2017-04-27 07:14:46 2017-04-27 07:29:48 Done
A 2017-04-27 08:49:48 2017-04-27 09:06:07 Done
A 2017-04-27 09:38:03 2017-04-27 09:59:03 Done
C 2017-04-27 10:09:03 2017-04-27 10:27:40 Done
B 2017-04-27 11:07:40 B2017-04-27 11:23:48 Done
以下是代码:
import plotly.offline as offline
import plotly.plotly as py
import plotly.figure_factory as ff
import plotly.graph_objs as go
import plotly.io as pio
import pandas as pd
import numpy as np
filePath="test.data"
df = pd.read_table(filePath,
header=None,
usecols=[0,1,2,3],
sep='\t',
converters={1:np.datetime64, 2:np.datetime64},
)
df.columns = ['Task', 'Start', 'Finish', 'Resource']
colors = {'Done': 'rgb(0, 240, 0)',}
fig = ff.create_gantt(df,
title='My Tasks',
bar_width=0.1,
showgrid_x=False,
showgrid_y=False,
colors=colors,
#colors='Viridis',
index_col='Resource',
show_colorbar = True,
group_tasks=True,
)
fig['layout'].update(plot_bgcolor = 'rgba(0,0,0,250)',
paper_bgcolor = 'rgba(0,0,0,0)',
showlegend = True,
violinmode='overlay',
colorway = ['rgb(0, 150, 0)'],
)
pio.write_image(fig, 'testdata.pdf', format='pdf', width=1000, height=1000, scale=1)
输出: