Milestones on Gantt charts

Hi,

I am using the gantt chart in plotly. So far looks great. What I would like to know is whether I can add milestones to the chart, that would be either a shape (like a diamond) or text (like A)

I am using the create_gantt from the figure_factory.

This is what I have so far (basically from the example:

import plotly.plotly as py
import plotly.figure_factory as ff

df = [dict(Task=“SH 5”, Start=‘2017-01-01’, Finish=‘2017-02-02’, Resource=‘Complete’),
dict(Task=“SH 5”, Start=‘2017-02-15’, Finish=‘2017-03-15’, Resource=‘Incomplete’),
dict(Task=“SH 5”, Start=‘2017-03-15’, Finish=‘2017-04-15’, Resource=‘Not Started’),
dict(Task=“SH 5”, Start=‘2018-03-15’, Finish=‘2018-04-15’, Resource=‘Complete’),
dict(Task=“Job-2”, Start=‘2017-01-17’, Finish=‘2017-02-17’, Resource=‘Not Started’),
dict(Task=“Job-2”, Start=‘2017-01-17’, Finish=‘2017-02-17’, Resource=‘Complete’),
dict(Task=“Job-3”, Start=‘2017-03-10’, Finish=‘2017-03-20’, Resource=‘Not Started’),
dict(Task=“Job-3”, Start=‘2017-04-01’, Finish=‘2017-04-20’, Resource=‘Not Started’),
dict(Task=“Job-3”, Start=‘2017-05-18’, Finish=‘2017-06-18’, Resource=‘Not Started’),
dict(Task=“Job-4”, Start=‘2017-01-14’, Finish=‘2017-03-14’, Resource=‘Complete’)]

colors = {‘Not Started’: ‘rgb(220, 0, 0)’,
‘Incomplete’: (1, 0.9, 0.16),
‘Complete’: ‘rgb(0, 255, 100)’}

fig = ff.create_gantt(df, colors=colors, index_col=‘Resource’, show_colorbar=True, group_tasks=True)
py.iplot(fig, filename=‘gantt-group-tasks-together’, world_readable=True)

Thanks,

Ivan

@ivan7707 Just add shapes to fig after you’ve created the Gantt figure:

1 Like

Thanks Jack. Not sure if there is an answered question thing on here, but that did answer my question.

Hi @jack, I had the same issue as @ivan7707 and tried adding the shapes as you suggested.

I did it in the following way:

df = [dict(Task="Job A", Start='2009-01-01', Finish='2009-02-28'),
      dict(Task="Job B", Start='2009-03-05', Finish='2009-04-15'),
      dict(Task="Job C", Start='2009-02-20', Finish='2009-05-30')]

fig = ff.create_gantt(df)

fig['layout']['shapes'] = [
        {
            'type': 'line',
            'x0': '2009-04-15',
            'y0': 0,
            'x1': '2009-04-16',
            'y1': 3,
        }]

offline.plot(fig, filename='gantt-simple-gantt-chart')

However, now the tasks are not visible anymore. What is the correct way to add the shapes?

1 Like

Stumbled across your question when trying to get gantt charts and shapes working together. Your code replaces the shapes placed by create_gantt with the line shape. A correct snippet for this requirement looks as follows.

If there is a more elegant way to do this, please advise :slight_smile:

Hey Chrisk,

Your solution worked for me. But is there any way i can loop it , so that i can add multiple milestones without writing the repetitive code ?

Currently I am doing something like this:

fig[‘layout’][‘shapes’].append(
{
‘type’: ‘line’,
‘x0’: red[‘Date’][0],
‘y0’: 0,
‘x1’: red[‘Date’][0],
‘line’: {
‘color’: ‘rgb(128, 0, 128)’,
‘width’: 1,
‘dash’: ‘dot’,
},
}),

 fig['layout']['shapes'].append(
        {
        'type': 'line',
        'x0': red['Date'][1],
        'y0': 0,
        'x1': red['Date'][1],
        'line': {
            'color': 'rgb(128, 0, 128)',
            'width': 1,
            'dash': 'dot',
        },
        }),

Hi All, fig['layout']['shapes'].append( does not work with the latest plotly. I think 3.0+. All the values in a fig are tuples. What is the correct method to update a fig now?

Hi @nstoweAtGT,

You can preconstruct the shapes as a list and then assign them all together

shapes = [...]
fig['layout']['shapes'] = shapes

Of, you can use the += to append a new tuple of shapes to the existing tuple:

fig['layout']['shapes'] += ({...},)

Make sure the thing to the right of += is a tuple and not a list.

Hope that helps,
-Jon

Thanks @jmmease was not aware of that trick with tuples.

I ended up doing it the long way and assigning a tuple back via the properties like:

sl=list(fig.layout.shapes)
s=go.layout.Shape(
            type= 'rect',
            x0='2019-01-25',
            y0= 0,
            x1= '2019-02-16',
            y1= 3,)
sl.append(s)
fig.layout.shapes=sl

Also is there a way we can label these milestones/shapes with text in the chart?