def MonthGraph(request): if request.method =='GET' : if request.user.is_authenticated(): now = datetime.now() now = formats.date_format(now,"SHORT_DATETIME_FORMAT") #data = DeviceUsage.objects.values('date').annotate(sumusage=Sum('usage'))# Group by date y = [0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0] print "Don't select data" head="" plot = Bar(y,title=head, xlabel='date', ylabel='Unit', width=800, height=400) plot.toolbar_location = None plot.outline_line_color = None script, div = components(plot, CDN) return render(request, "monthgraph.html", {"the_script": script, "the_div": div,'devices': DeviceProfile.objects.filter(owner=request.user),"date":str(now)}) else: return redirect('/authen/') elif request.method == 'POST': now = datetime.now() now = formats.date_format(now,"SHORT_DATETIME_FORMAT") #data = DeviceUsage.objects.values('date').annotate(sumusage=Sum('usage'))# Group by date y = [0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0] head="" if request.POST.get('deviceid') is not None: id = request.POST.get('deviceid') if request.POST.get('month') is not None: month = request.POST.get('month') do = DeviceProfile.objects.get(device_id=id) data = DeviceUsage.objects.filter(device_id=do,date__month=month).values('date').annotate(sumusage=Sum('usage'),sumtime=Sum('time')) #print data # data[0].get('date').strftime("%d") get only date from year-month-date for d in data: hr = (float(d.get('sumtime'))/3600) kw = float(d.get('sumusage')) y[int(d.get('date').strftime("%d"))] = kw * hr head = "usage of device name: "+ do.device_name +" at "+request.POST.get('month')+"th Month" else: print "Don't select data" head="" plot = Bar(y,title=head, xlabel='date', ylabel='Unit', width=800, height=400) plot.toolbar_location = None plot.outline_line_color = None script, div = components(plot, CDN) return render(request, "monthgraph.html", {"the_script": script, "the_div": div,'devices': DeviceProfile.objects.filter(owner=request.user),"date":str(now)})
def generate_chart(year): data = get_data(year) barchart = Bar(data, values=blend(*SCORED_EVENTS, labels_name='event'), label=cat(columns='Team', sort=False), stack=cat(columns='event', sort=False), color=color(columns='event', palette=Spectral9, sort=False), xgrid=False, ygrid=False, legend='top_right', plot_width=1000, plot_height=625, tools="pan,wheel_zoom,box_zoom,reset,resize") barchart.title = "Formula SAE Michigan " + str(year) + " Total Scores by Place" barchart._xaxis.axis_label = "Teams" barchart._xaxis.axis_line_color = None barchart._xaxis.major_tick_line_color = None barchart._xaxis.minor_tick_line_color = None barchart._xaxis.major_label_text_font_size = '0.6em' barchart._yaxis.axis_label = "Total Score" barchart._yaxis.axis_line_color = None barchart._yaxis.major_tick_line_color = None barchart._yaxis.minor_tick_line_color = None barchart.outline_line_color = None barchart.toolbar_location = 'right' barchart.logo = None return barchart
def generate_chart(event): data = get_data(TIMED_EVENTS[event]) # Bokeh doesn't let me control the order of the grouping! This is # frustrating since it will be different on every server launch barchart = Bar(data, values='percentage_dnf', label='year', color="FireBrick", xgrid=False, ygrid=False, plot_width=800, plot_height=500, tools="pan,wheel_zoom,box_zoom,reset,resize") barchart.title = "Formula SAE Michigan DNFs - " + event barchart._xaxis.axis_label = "Year" barchart._xaxis.axis_line_color = None barchart._xaxis.major_tick_line_color = None barchart._xaxis.minor_tick_line_color = None barchart._yaxis.axis_label = "Percentage DNF" barchart._yaxis.axis_line_color = None barchart._yaxis.major_tick_line_color = None barchart._yaxis.minor_tick_line_color = None barchart._yaxis.formatter = NumeralTickFormatter(format="0%") barchart.y_range = Range1d(0, 1) barchart.outline_line_color = None barchart.toolbar_location = 'right' barchart.logo = None for renderer in barchart.select(GlyphRenderer): if renderer.data_source.data['height'] != [0]: year = renderer.data_source.data['year'] num_dnf = data['dnfs'].loc[data['year'] == year] num_entries = data['entries'].loc[data['year'] == year] percent_dnf = data['percentage_dnf'].loc[data['year'] == year] hover = HoverTool(renderers=[renderer], tooltips=[ ("# DNFs", '%d' % num_dnf.values[0]), ("# Entries", '%d' % num_entries.values[0]), ("% DNF", '%.2f%%' % (100 * percent_dnf.values[0])) ]) barchart.add_tools(hover) return barchart
def generate_chart(team): data = generate_data(team) selectable_years = list(map(str, data.index.unique())) # Generate the chart UNFORTUNATELY using the high level plots. Streaming # a bunch of quads resulted in lots of graphics corruption when switching # teams. I would rather have the plots work all the time but really slow # then have the plot show the wrong information. barchart = Bar(data, values=blend(*SCORED_EVENTS, labels_name='event'), label=cat(columns='Year', sort=False), stack=cat(columns='event', sort=False), color=color(columns='event', palette=Spectral9, sort=False), xgrid=False, ygrid=False, plot_width=1000, plot_height=625, tools="pan,wheel_zoom,box_zoom,reset,resize") barchart.title = "Formula SAE Michigan - " + team barchart._xaxis.axis_label = "Teams" barchart._xaxis.axis_line_color = None barchart._xaxis.major_tick_line_color = None barchart._xaxis.minor_tick_line_color = None barchart._yaxis.axis_label = "Total Score" barchart._yaxis.axis_line_color = None barchart._yaxis.major_tick_line_color = None barchart._yaxis.minor_tick_line_color = None barchart.outline_line_color = None barchart.toolbar_location = 'right' barchart.logo = None # Hacky tooltips for renderer in barchart.select(GlyphRenderer): if renderer.data_source.data['height'] != [0]: year = renderer.data_source.data['Year'] place = data['Place'].loc[data['Year'] == year] score = data['Total Score'].loc[data['Year'] == year] hover = HoverTool(renderers=[renderer], tooltips=[("Year", '@Year'), ("Selection", '@event'), ("Event Score", '@height'), ("Total Score", '%.2f' % score.values[0]), ("Overall Place", '%d' % place.values[0])]) barchart.add_tools(hover) return barchart
def generate_chart(event): data = get_data(TIMED_EVENTS[event]) # Bokeh doesn't let me control the order of the grouping! This is # frustrating since it will be different on every server launch barchart = Bar(data, values='percentage_dnf', label='year', color="FireBrick", xgrid=False, ygrid=False, plot_width=800, plot_height=500, tools="pan,wheel_zoom,box_zoom,reset,resize") barchart.title = "Formula SAE Michigan DNFs - " + event barchart._xaxis.axis_label = "Year" barchart._xaxis.axis_line_color = None barchart._xaxis.major_tick_line_color = None barchart._xaxis.minor_tick_line_color = None barchart._yaxis.axis_label = "Percentage DNF" barchart._yaxis.axis_line_color = None barchart._yaxis.major_tick_line_color = None barchart._yaxis.minor_tick_line_color = None barchart._yaxis.formatter = NumeralTickFormatter(format="0%") barchart.y_range = Range1d(0, 1) barchart.outline_line_color = None barchart.toolbar_location = 'right' barchart.logo = None for renderer in barchart.select(GlyphRenderer): if renderer.data_source.data['height'] != [0]: year = renderer.data_source.data['year'] num_dnf = data['dnfs'].loc[data['year'] == year] num_entries = data['entries'].loc[data['year'] == year] percent_dnf = data['percentage_dnf'].loc[data['year'] == year] hover = HoverTool(renderers=[renderer], tooltips=[("# DNFs", '%d' % num_dnf.values[0]), ("# Entries", '%d' % num_entries.values[0]), ("% DNF", '%.2f%%' % (100 * percent_dnf.values[0]))]) barchart.add_tools(hover) return barchart
def generate_chart(year): data = get_data(year) plot_data = { 'country': data.index.tolist(), 'count': [float(i) for i in data.values.tolist()] } barchart = Bar(plot_data, label='country', values='count', color="red", xgrid=False, ygrid=False, plot_width=800, plot_height=500, tools="pan,wheel_zoom,box_zoom,reset,resize") barchart.title = "Formula SAE Michigan " + year + " Countries" barchart.x_range = FactorRange(factors=data.index.tolist()) barchart._xaxis.axis_label = "Country" barchart._xaxis.axis_line_color = None barchart._xaxis.major_tick_line_color = None barchart._xaxis.minor_tick_line_color = None barchart._yaxis.axis_label = "Number of Teams" barchart._yaxis.axis_line_color = None barchart._yaxis.major_tick_line_color = None barchart._yaxis.minor_tick_line_color = None barchart.outline_line_color = None barchart.toolbar_location = 'right' barchart.logo = None hover = HoverTool(tooltips=[("Country", '@x'), ("# Teams", '@height')]) barchart.add_tools(hover) return barchart
def generate_chart(year): data = get_data(year) plot_data = {'country': data.index.tolist(), 'count': [float(i) for i in data.values.tolist()]} barchart = Bar(plot_data, label='country', values='count', color="red", xgrid=False, ygrid=False, plot_width=800, plot_height=500, tools="pan,wheel_zoom,box_zoom,reset,resize") barchart.title = "Formula SAE Michigan " + year + " Countries" barchart.x_range = FactorRange(factors=data.index.tolist()) barchart._xaxis.axis_label = "Country" barchart._xaxis.axis_line_color = None barchart._xaxis.major_tick_line_color = None barchart._xaxis.minor_tick_line_color = None barchart._yaxis.axis_label = "Number of Teams" barchart._yaxis.axis_line_color = None barchart._yaxis.major_tick_line_color = None barchart._yaxis.minor_tick_line_color = None barchart.outline_line_color = None barchart.toolbar_location = 'right' barchart.logo = None hover = HoverTool(tooltips=[("Country", '@x'), ("# Teams", '@height')]) barchart.add_tools(hover) return barchart
def generate_chart(): data = get_data() # Bokeh doesn't let me control the order of the grouping! This is # frustrating since it will be different on every server launch barchart = Bar(data, label='year', values='count', group=cat(columns='size', ascending=True, sort=True), color=color(columns='size', palette=Spectral4), legend='top_left', xgrid=False, ygrid=False, plot_width=800, plot_height=500, tools="pan,wheel_zoom,box_zoom,reset,resize") barchart.title = "Formula SAE Michigan Engine Cylinders" barchart._xaxis.axis_label = "Year" barchart._xaxis.axis_line_color = None barchart._xaxis.major_tick_line_color = None barchart._xaxis.minor_tick_line_color = None barchart._yaxis.axis_label = "Frequency" barchart._yaxis.axis_line_color = None barchart._yaxis.major_tick_line_color = None barchart._yaxis.minor_tick_line_color = None barchart.outline_line_color = None barchart.toolbar_location = 'right' barchart.logo = None hover = HoverTool(tooltips=[("Engine", '@size'), ("# Teams", '@height')]) barchart.add_tools(hover) return barchart
def generate_single_bar(df, time_str, color_dict, colors): df = df[df.percent_total >= 3] title = "Failure Rate " + time_str fill = [color_dict[model_key] if model_key in color_dict else 'grey' for model_key in df.model] source = ColumnDataSource(dict(color=[c for c in df['color']], model=[m for m in df['model']], failure_rate=[f for f in df['failure_rate']], count=[co for co in df['count']])) plot = Bar(df, 'model', values='failure_rate', title=title, source=source, tools=['hover'],color='color', legend=None) # outline_line_color="color", border_fill_color='color', hover = plot.select(dict(type=HoverTool)) hover.tooltips = [ ("Model ", "@model"), ("Failure rate ", "@y"), ("Number of drives", "@count") #("Time ", "@timeline"), ] hover.mode = 'mouse' plot.xaxis.axis_label = 'Model Serial Number' plot.yaxis.axis_label = 'Naive Failure Rate' plot.title_text_font_size="18px" plot.grid.grid_line_alpha = 0 plot.ygrid.grid_line_color = None plot.toolbar.logo = None plot.outline_line_width = 0 plot.outline_line_color = "white" plot.plot_height = 600 plot.plot_width = 800 plot.xaxis.major_tick_line_color = None plot.yaxis.major_tick_line_color = None plot.xaxis.axis_line_width = 2 plot.yaxis.axis_line_width = 2 plot.title.text_font_size = '16pt' plot.xaxis.axis_label_text_font_size = "14pt" plot.xaxis.major_label_text_font_size = "14pt" plot.yaxis.axis_label_text_font_size = "14pt" plot.yaxis.major_label_text_font_size = "14pt" return(plot)
def plot_read_dist(rinfo): df = pd.read_table(rinfo) data = df[['length', 'mapper', 'count']].groupby(['length', 'mapper']).count() data = data.apply(lambda s: s / data.sum() * 100, axis=1).reset_index() p = Bar(data, plot_width=500, plot_height=400, label='length', values='count', agg='sum', stack=cat(columns='mapper', sort=False), legend='top_right', color=color(columns='mapper', palette=["#f98283", "#a4a4a4"], sort=False), xlabel='read length (nt)', ylabel='proportion (%)', ygrid=False, tooltips=[('length', '@length'), ('mapper', '@mapper'), ('percent', '@height')]) p.toolbar.logo = None p.outline_line_color = None return p
def generate_chart(year): data = get_data(year) barchart = Bar(data, values=blend(*SCORED_EVENTS, labels_name='event'), label=cat(columns='Team', sort=False), stack=cat(columns='event', sort=False), color=color(columns='event', palette=Spectral9, sort=False), xgrid=False, ygrid=False, legend='top_right', plot_width=1000, plot_height=625, tools="pan,wheel_zoom,box_zoom,reset,resize") barchart.title = "Formula SAE Michigan " + str( year) + " Total Scores by Place" barchart._xaxis.axis_label = "Teams" barchart._xaxis.axis_line_color = None barchart._xaxis.major_tick_line_color = None barchart._xaxis.minor_tick_line_color = None barchart._xaxis.major_label_text_font_size = '0.6em' barchart._yaxis.axis_label = "Total Score" barchart._yaxis.axis_line_color = None barchart._yaxis.major_tick_line_color = None barchart._yaxis.minor_tick_line_color = None barchart.outline_line_color = None barchart.toolbar_location = 'right' barchart.logo = None return barchart
def MonthGraph(request): if request.method == 'GET': if request.user.is_authenticated(): now = datetime.now() now = formats.date_format(now, "SHORT_DATETIME_FORMAT") #data = DeviceUsage.objects.values('date').annotate(sumusage=Sum('usage'))# Group by date y = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] print "Don't select data" head = "" plot = Bar(y, title=head, xlabel='date', ylabel='Unit', width=800, height=400) plot.toolbar_location = None plot.outline_line_color = None script, div = components(plot, CDN) return render( request, "monthgraph.html", { "the_script": script, "the_div": div, 'devices': DeviceProfile.objects.filter(owner=request.user), "date": str(now) }) else: return redirect('/authen/') elif request.method == 'POST': now = datetime.now() now = formats.date_format(now, "SHORT_DATETIME_FORMAT") #data = DeviceUsage.objects.values('date').annotate(sumusage=Sum('usage'))# Group by date y = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] head = "" if request.POST.get('deviceid') is not None: id = request.POST.get('deviceid') if request.POST.get('month') is not None: month = request.POST.get('month') do = DeviceProfile.objects.get(device_id=id) data = DeviceUsage.objects.filter( device_id=do, date__month=month).values('date').annotate( sumusage=Sum('usage'), sumtime=Sum('time')) #print data # data[0].get('date').strftime("%d") get only date from year-month-date for d in data: hr = (float(d.get('sumtime')) / 3600) kw = float(d.get('sumusage')) y[int(d.get('date').strftime("%d"))] = kw * hr head = "usage of device name: " + do.device_name + " at " + request.POST.get( 'month') + "th Month" else: print "Don't select data" head = "" plot = Bar(y, title=head, xlabel='date', ylabel='Unit', width=800, height=400) plot.toolbar_location = None plot.outline_line_color = None script, div = components(plot, CDN) return render( request, "monthgraph.html", { "the_script": script, "the_div": div, 'devices': DeviceProfile.objects.filter(owner=request.user), "date": str(now) })
def output_components(prefix, order, maxaln): rinfo = "{0}.rinfo".format(prefix) comp = "{0}.comp".format(prefix) def plot_read_dist(rinfo): df = pd.read_table(rinfo) data = df[['length', 'mapper', 'count']].groupby(['length', 'mapper']).count() data = data.apply(lambda s: s / data.sum() * 100, axis=1).reset_index() p = Bar(data, plot_width=500, plot_height=400, label='length', values='count', agg='sum', stack=cat(columns='mapper', sort=False), legend='top_right', color=color(columns='mapper', palette=["#f98283", "#a4a4a4"], sort=False), xlabel='read length (nt)', ylabel='proportion (%)', ygrid=False, tooltips=[('length', '@length'), ('mapper', '@mapper'), ('percent', '@height')]) p.toolbar.logo = None p.outline_line_color = None return p rdist = plot_read_dist(rinfo) df = pd.read_table(comp, index_col=0) total = df.sum() total = total * 100 / total.sum() df = df.apply(lambda s: s * 100 / s.sum(), axis=1) df = df.reset_index() #ftypes = df.columns[1:].tolist() ftypes = order colors = sns.color_palette("hls", len(ftypes)).as_hex() bar = Bar(df, values=blend(*ftypes, name="ftypes", labels_name="ftype"), x_range=rdist.x_range, y_range=(0, 100), label=cat(columns='rlen', sort=False), stack=cat(columns='ftype', sort=False), xlabel='read length (nt)', ylabel='proportion (%)', legend="top_right", ygrid=False, width=500, height=400, color=color(columns='ftype', palette=colors, sort=False), fill_alpha=1, tooltips=[("length", "@rlen"), ("feature", "@ftype"), ("percent", "@height")]) bar.toolbar.logo = None bar.outline_line_color = None start_angles = {} end_angles = {} start = 0 for ftype in ftypes: end = 2 * pi * total[ftype] / 100 start_angles[ftype] = start end_angles[ftype] = start + end start += end colors = dict(zip(ftypes, colors)) df = pd.DataFrame(total).reset_index() df.columns = ["ftype", "percent"] df["start"] = df.apply(lambda s: start_angles[s["ftype"]], axis=1) df["end"] = df.apply(lambda s: end_angles[s["ftype"]], axis=1) df["color"] = df.apply(lambda s: colors[s["ftype"]], axis=1) df["x"] = df.apply(lambda s: 1.2 * cos( (start_angles[s["ftype"]] + end_angles[s["ftype"]]) / 2), axis=1) df["y"] = df.apply(lambda s: 1.2 * sin( (start_angles[s["ftype"]] + end_angles[s["ftype"]]) / 2), axis=1) df["text"] = df.apply(lambda s: "{0:.3f}%".format(s["percent"]), axis=1) source = ColumnDataSource(data=df) pie = figure(width=400, height=400, x_range=(-1.4, 1.4), y_range=(-1.4, 1.4)) pie.toolbar.logo = None wr = pie.annular_wedge(x=0, y=0, inner_radius=0.5, outer_radius=1, start_angle="start", end_angle="end", fill_color="color", line_color="#ffffff", line_width=0.5, source=source) pie.axis.visible = False pie.grid.grid_line_color = None pie.outline_line_color = None hover = HoverTool(tooltips=[("feature", "@ftype"), ("percent", "@percent")], renderers=[wr]) pie.add_tools(hover) text_props = { "source": source, "angle": 0, "color": "black", "text_align": "center", "text_baseline": "middle", "text_font_size": "8pt", "text_font_style": "normal" } pie.text(x="x", y="y", text="text", **text_props) empty = figure(width=400, height=400, x_range=(-1.1, 1.1), y_range=(-1.1, 1.1)) empty.toolbar.logo = None empty.axis.visible = False empty.grid.grid_line_color = None empty.outline_line_color = None empty.toolbar_location = None stat = get_stat(prefix, maxaln) source = ColumnDataSource(data=stat) columns = [ TableColumn(field="Statistics", title="Statistics", width=200), TableColumn(field="Number of reads", title="Number of reads", formatter=NumberFormatter(format="0,0"), width=150), TableColumn(field="Proportion", title="Proportion", formatter=NumberFormatter(format="0.000%"), width=100), ] data_table = DataTable(source=source, columns=columns, width=450, row_headers=False) script, div = components( layout([[data_table, rdist], [pie, bar]], sizing_mode="scale_width")) return script, div