I'm trying to produce a Gantt chart style plot using stacked horizontal bars, however when I show() the plot, the page is blank, and the error I get in the js console is SCRIPT5007: Unable to get property 'length' of undefined or null reference
. I am able to view other Bokeh plots, such as the stacked vertical bars example in the Bokeh documentation.
This is the code I am using to create the plot
from bokeh.plotting import figure, show, output_notebook, output_file
from bokeh.models import ColumnDataSource, Range1d
grouped=df.groupby('Org')
source=ColumnDataSource(grouped)
p=figure(title='Project Schedule', x_axis_type='datetime', y_range=grouped['Org'],
x_range=Range1d(df.Start.min(),df.Finish.max()))
p.hbar_stack('Task', y='Org', height=0.9, source=source)
show(p)
And a sample of the data I use, in a pandas dataframe
Org Task Start Finish
0 o1 t1 2019-11-19 2019-11-23
1 o1 t2 2019-11-23 2019-11-27
2 o1 t3 2019-11-27 2019-12-01
3 o1 t4 2019-12-02 2019-12-06
4 o2 t1 2019-11-20 2019-11-24
5 o2 t2 2019-11-24 2019-11-28
6 o2 t3 2019-12-01 2019-12-05
7 o2 t4 2019-12-05 2019-12-09
8 o3 t1 2019-11-20 2019-11-24
9 o3 t2 2019-11-24 2019-11-28
10 o3 t3 2019-12-01 2019-12-05
11 o3 t4 2019-12-05 2019-12-09
You could do smth like this (Bokeh v1.3.0):
from bokeh.plotting import figure, show, output_notebook, output_file
from bokeh.models import ColumnDataSource, Range1d
from bokeh.core.properties import value
import pandas as pd
df = pd.DataFrame({'Org': ['o1', 'o1','o1','o1', 'o2','o2','o2','o2','o3','o3','o3','o3'],
'Task': ['t1', 't2','t3','t4', 't1', 't2','t3','t4','t1', 't2','t3','t4'],
'Start': pd.date_range('2019-02-24', periods=4*3, freq='D'),
'Finish': pd.date_range('2019-03-24', periods=4*3, freq='D')})
df['Diff'] = df['Finish'] - df['Start']
x = df['Task'].unique().tolist()
y = df['Org'].unique().tolist()
data1 = {'y': y}
data2 = {t: [df['Diff'][j + i % 4] for j in range(len(y))] for i, t in enumerate(x)}
data = {**data1, **data2}
p = figure(title='Project Schedule', y_range = y, x_axis_type='datetime')
p.hbar_stack(x, y='y', height=0.9, color=['red', 'blue', 'orange', 'green'], legend=[value(k) for k in x], source=data)
show(p)
But it is not working quite well, needs some more logic for calculating the time differences in ms, I guess...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.