different pie chart for types
This commit is contained in:
@@ -26,6 +26,7 @@ from bokeh.models import CustomJS,Slider, TextInput,BoxAnnotation
|
||||
from bokeh.resources import CDN,INLINE
|
||||
from bokeh.embed import components
|
||||
from bokeh.layouts import layout,widgetbox
|
||||
from bokeh.palettes import Category20c
|
||||
from bokeh.layouts import row as layoutrow
|
||||
from bokeh.layouts import column as layoutcolumn
|
||||
from bokeh.models import LinearAxis,LogAxis,Range1d,DatetimeTickFormatter,HoverTool
|
||||
@@ -37,6 +38,7 @@ from bokeh.models import (
|
||||
ResetTool, TapTool,CrosshairTool,BoxZoomTool,
|
||||
Span, Label
|
||||
)
|
||||
from bokeh.transform import cumsum
|
||||
from bokeh.models.glyphs import ImageURL
|
||||
from bokeh.models import OpenURL, TapTool
|
||||
from rowers.opaque import encoder
|
||||
@@ -251,65 +253,48 @@ def interactive_hr_piechart(df,rower,title):
|
||||
|
||||
return components(z)
|
||||
|
||||
def pretty_timedelta(secs):
|
||||
hours, remainder = divmod(secs,3600)
|
||||
minutes, seconds = divmod(remainder,60)
|
||||
|
||||
return '{}:{:02}:{:02}'.format(int(hours),int(minutes),int(seconds))
|
||||
|
||||
def interactive_workouttype_piechart(workouts):
|
||||
if len(workouts) == 0:
|
||||
return "","Not enough workouts to make a chart"
|
||||
|
||||
datadict = {}
|
||||
labels = []
|
||||
types = []
|
||||
|
||||
|
||||
for w in workouts:
|
||||
try:
|
||||
datadict[w.workouttype] += 3600*w.duration.hour+60*w.duration.minute+w.duration.second
|
||||
label = mytypes.workouttypes_ordered[w.workouttype]
|
||||
except KeyError:
|
||||
datadict[w.workouttype] = 3600*w.duration.hour+60*w.duration.minute+w.duration.second
|
||||
types += [w.workouttype]
|
||||
try:
|
||||
labels += [mytypes.workouttypes_ordered[w.workouttype]]
|
||||
except KeyError:
|
||||
labels += [w.workouttype]
|
||||
labels = w.workouttype
|
||||
try:
|
||||
datadict[label] += 60*(60*w.duration.hour+w.duration.minute)+w.duration.second
|
||||
except KeyError:
|
||||
datadict[label] = 60*(60*w.duration.hour+w.duration.minute)+w.duration.second
|
||||
|
||||
total = 0
|
||||
source_starts = [0]
|
||||
source_ends = []
|
||||
for type in types:
|
||||
total += datadict[type]
|
||||
source_starts.append(total)
|
||||
source_ends.append(total)
|
||||
data = pd.Series(datadict).reset_index(name='value').rename(columns={'index':'type'})
|
||||
data['angle'] = data['value']/data['value'].sum() * 2*pi
|
||||
data['color'] = Category20c[len(datadict)]
|
||||
data['totaltime'] = pd.Series([pretty_timedelta(v) for v in data['value']])
|
||||
|
||||
source_ends.append(total)
|
||||
p = figure(plot_height=350, title="Types", toolbar_location=None,
|
||||
tools="hover,save", tooltips="@type: @totaltime", x_range=(-0.5, 1.0))
|
||||
|
||||
source_starts = pd.Series(source_starts)*2*pi/total
|
||||
source_ends = pd.Series(source_ends)*2*pi/total
|
||||
p.wedge(x=0, y=1, radius=0.4,
|
||||
start_angle=cumsum('angle', include_zero=True), end_angle=cumsum('angle'),
|
||||
line_color="white", fill_color='color', source=data,legend='type', )
|
||||
|
||||
size = 350
|
||||
TOOLS = 'save'
|
||||
p.axis.axis_label=None
|
||||
p.axis.visible=False
|
||||
p.grid.grid_line_color = None
|
||||
p.outline_line_color = None
|
||||
p.toolbar_location = 'right'
|
||||
|
||||
z = figure(title="Workout Types", x_range=(-1,1), y_range=(-1,1), width=size, height=size,
|
||||
tools=TOOLS,
|
||||
)
|
||||
|
||||
colors = palette
|
||||
|
||||
print(source_ends)
|
||||
print(labels)
|
||||
|
||||
for start, end , legend, color in zip(source_starts, source_ends, labels,
|
||||
colors[0:len(source_starts)]):
|
||||
print(start,end,color,legend)
|
||||
z.wedge(x=0, y=0, radius=1, start_angle=start, end_angle=end, color=color, legend=legend)
|
||||
|
||||
|
||||
z.toolbar_location = 'right'
|
||||
z.legend.location = 'top_right'
|
||||
#z.legend.visible = False
|
||||
z.axis.visible = False
|
||||
z.xgrid.grid_line_color = None
|
||||
z.ygrid.grid_line_color = None
|
||||
z.outline_line_color = None
|
||||
|
||||
return components(z)
|
||||
return components(p)
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user