反正有没有 plotly 制作甘特图?

Is there anyway for plotly to make a gantt chart?

求助。我正在尝试绘制甘特图,但我只是没有看到 一种方法来做到这一点。 甘特图类似于水平条形图,有一个 'starting' 点 为每个条指定的坐标。 所以它看起来像:

   XXXXX
      XXXXXX
   XXXXXXXXXXXXXXXXXXXXX
            XXXX

我找不到使条形图中的 'bars' 从特定位置开始的方法 X 坐标。 任何 tips/tricks/hints?

这是我的结果...不是完美的甘特图, (你不能改变栏的大小,没有依赖 条目之间)但足以满足我的目的:

显示我如何进行透明跟踪的代码片段:

        // 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;

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
)}  

是的! 将 plotly.figure_factory 导入为 ff

ff.create_gantt(df)

Plotly 内置了甘特图。您不需要从条形图创建它们。您可以为其提供字典列表,也可以为其提供数据框。如果您选择后者,请确保告诉图形哪一列是任务,以及您的开始和结束日期。我发现使用数据时间和标记列开始和完成要容易得多。这样甘特图会自动读取它们,文档在下面 link。

https://plot.ly/python/gantt/

如果你使用 plotly,如果你想在一行中有相似的键,请使用 group_tasks=True

R Package vistime 使用 Plotly 创建甘特图。还有一种智能图表中事件的垂直分布,这样图表就不会比需要的大。

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")

这是一个带有 plotly 的甘特图

"test.data"内容:
格式为:Task\tStart\tFinish\tResource

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)

输出: