Browse Source

add charting services.

stable
Craig Oates 4 years ago
parent
commit
5e2c276d37
  1. 73
      app/services/charting_services.py

73
app/services/charting_services.py

@ -0,0 +1,73 @@
from bokeh.plotting import figure, output_file, show
from bokeh.embed import components
from bokeh.io import show, output_file
from bokeh.models import ColumnDataSource
from bokeh.palettes import Blues256, Category20c
from bokeh.plotting import figure
from bokeh.transform import factor_cmap, cumsum
def build_creation_date_chart(data, title):
units = list(data.keys())
totals = list(data.values())
if title == "By Year":
units.reverse()
totals.reverse()
source = ColumnDataSource(data=dict(units=units, totals=totals))
chart = figure(x_range=units, plot_height=350, plot_width=800,
toolbar_location=None, title=title)
chart.vbar(x='units', top='totals', width=0.9, source=source,
legend_field="units",line_color='white',
fill_color=factor_cmap('units', palette=Blues256, factors=units))
chart.title.text_font_size = '18pt'
chart.title.text_font = "intended, sans-serif"
chart.title.text_color = "#424242"
chart.outline_line_color = "#424242"
chart.xgrid.grid_line_color = None
chart.ygrid.grid_line_color = "#424242"
chart.y_range.start = 0
chart.y_range.end = (max(totals) + 10)
chart.background_fill_color = "#f5f5f6"
chart.border_fill_color = "#f5f5f6"
chart.legend.visible = False
script, div = components(chart)
return [script, div]
def build_sorted_chart(data, title):
d = sorted(data.items())
totals = list()
units = list()
for i in d:
units.append(i[0])
totals.append(i[1])
source = ColumnDataSource(data=dict(units=units, totals=totals))
chart = figure(x_range=units, plot_height=350, plot_width=800,
toolbar_location=None, title=title)
chart.vbar(x='units', top='totals', width=0.9, source=source,
legend_field="units",line_color='white',
fill_color=factor_cmap('units', palette=Blues256, factors=units))
chart.title.text_font_size = '18pt'
chart.title.text_font = "intended, sans-serif"
chart.title.text_color = "#424242"
chart.outline_line_color = "#424242"
chart.xgrid.grid_line_color = None
chart.ygrid.grid_line_color = "#424242"
chart.y_range.start = 0
chart.y_range.end = (max(totals) + 10)
chart.background_fill_color = "#f5f5f6"
chart.border_fill_color = "#f5f5f6"
chart.legend.visible = False
script, div = components(chart)
return [script, div]