I’m
trying to create a pie chart for a simple web application. So far I have used
figure.wedge() to make a complete pie chart. My code example in this post is
modified, but shows the same behaviour as the original.
First. Is
it a way I can make the wedges always be scaled (change radius) in relation to
the canvas\figure, so that the whole pie always is visible inside the figure.
It does not do this when scaling down the window.
Second.
How can I make the hovertool show values from source data?
In
general, any comment on how to make the code better is much appreciated.
Code example:
from bokeh.models import HoverTool
from bokeh.plotting import figure,show
from bokeh.models.sources import ColumnDataSource
from bokeh.palettes import viridis
import pandas as pd
data = {‘category’: {0: ‘A’, 1: ‘B’, 2: ‘C’},
'value': {0: 832, 1: 2734, 2: 1488},
'percent': {0: 16.462208151958844, 1: 54.095765730114763, 2: 29.442026117926396},
'degrees': {0: 59.263949347051842, 1: 194.74475662841314, 2: 105.99129402453502}}
df = pd.DataFrame(data)
cds = ColumnDataSource(data=df)
total = sum(df[‘value’])
radius = 0.4
piecolors = viridis(len(df[‘category’]))
plot = figure(title=‘test pie chart’,v_symmetry=True,h_symmetry=True,
responsive=True,toolbar_location="above",tools="pan,wheel_zoom,box_zoom,reset,save",
outline_line_color="#666666")
plot.height = 300
hover_html = “”"
<div>
<span class="hover-tooltip"><b>@category</b></span>
</div>
<div>
<span class="hover-tooltip">@percent"</span>
</div>
"""
hover = HoverTool(tooltips=hover_html)
plot.add_tools(hover)
for index,degree in enumerate(cds.data[‘degrees’]):
if index == 0:
start_angle = 0
end_angle = degree
else:
start_angle = end_angle
end_angle = start_angle + degree
plot.wedge(x=2, y=2,legend=cds.data['category'][index], radius=radius,radius_units="data", start_angle=start_angle,
end_angle=end_angle,line_color='black', direction="anticlock",start_angle_units='deg',
end_angle_units='deg',fill_color=piecolors[index])
plot.xaxis.axis_label= ‘n={}’.format(total)
plot.legend.background_fill_alpha = 0.8
plot.min_border_left = 80
plot.min_border_bottom = 80
plot.min_border_right = 80
plot.min_border_top = 80
show(plot)