Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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()
like image 875
RoQuOTriX Avatar asked Aug 28 '19 07:08

RoQuOTriX


1 Answers

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)

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.

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:

Interactive Gantt chart using Plotly

I believe this approach can definitely be improved to get a better workflow, but you could use it as a starting point.

like image 96
Axel Avatar answered Nov 01 '22 03:11

Axel