How to change Gantt charts date format


#1

Hey, I’m trying to change the Gantt charts date format from complete dates, to ranges of days, i.e. “add a task that has to be started on day 5 of the project and finished on day 20”.


#2

Hi @anonimeishon,

You can customize the D3 date formatting for the axis ticks using the fig.layout.xaxis.tickformat property. Here’s the docstring

Sets the tick label formatting rule using d3 formatting mini-
languages which are very similar to those in Python. For
numbers, see: https://github.com/d3/d3-format/blob/master/READM
E.md#locale_format And for dates see:
https://github.com/d3/d3-time-
format/blob/master/README.md#locale_format We add one item to
d3's date formatter: "%{n}f" for fractional seconds with n
digits. For example, *2016-10-13 09:15:23.456* with tickformat
"%H~%M~%S.%2f" would display "09~15~23.46"

See https://github.com/d3/d3-time-format/blob/master/README.md#locale_format for options

import plotly.graph_objs as go
import plotly.figure_factory as ff

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.xaxis.tickformat = '%j'
go.FigureWidget(fig)

Hope that helps!
-Jon


#3

Also, is there an easy way to put custom tags in every task?


#5

It works! but there’s a problem, it just shows up to 365 :confused:


#6

Yeah, that’s how the date formatting is going to work. You could override the tick text altogether by setting layout.xaxis.tickvals and layout.xaxis.ticktext

import plotly.graph_objs as go
import plotly.figure_factory as ff

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

fig = ff.create_gantt(df)

import pandas as pd
fig.layout.xaxis.tickvals = pd.date_range('2009-01-01', '2009-03-01', freq='d')
fig.layout.xaxis.ticktext = list(range(len(fig.layout.xaxis.tickvals)))

fig = go.FigureWidget(fig)
fig

-Jon