def measure_graph(request, area_ident, filter='all'): """ visualizes scores or measures in a graph identifier_list: [{'waterbody_slug': ...}, ...] start_end_dates: 2-tuple dates each row is an area """ if filter == 'measure': measures = Measure.objects.filter( Q(pk=area_ident)|Q(parent__id=area_ident)).order_by('title') else: area = get_object_or_404(Area, ident=area_ident) if filter == 'focus': measures = [m for m in _sorted_measures(area) if m.is_indicator == True] else: measures = _sorted_measures(area) start_date = iso8601.parse_date(request.GET.get('dt_start', '2008-1-1T00:00:00')).date() end_date = iso8601.parse_date(request.GET.get('dt_end', '2013-1-1T00:00:00')).date() width = int(request.GET.get('width', 380)) height = int(request.GET.get('height', 170)) legend_location = int(request.GET.get('legend-location', -1)) wide_left_ticks = request.GET.get('wide_left_ticks', 'false') == 'true' format = request.GET.get('format', None) graph = DateGridGraph(width=width, height=height) _image_measures(graph, measures, start_date, end_date, legend_location=legend_location, wide_left_ticks=wide_left_ticks) graph.set_margins() if format == 'ps': return graph.render( response=HttpResponse(content_type='application/postscript'), format='ps') else: return graph.png_response( response=HttpResponse(content_type='image/png'))
def get(self, request, *args, **kwargs): """ Draw the EKR graph """ dt_start, dt_end = self._dt_from_request() graph_items, graph_settings = self._graph_items_from_request() graph = DateGridGraph( width=int(graph_settings['width']), height=int(graph_settings['height'])) # # Legend. Must do this before using graph location calculations # legend_handles = [ # Line2D([], [], color=value_to_html_color(0.8), lw=10), # Line2D([], [], color=value_to_html_color(0.6), lw=10), # Line2D([], [], color=value_to_html_color(0.4), lw=10), # Line2D([], [], color=value_to_html_color(0.2), lw=10), # Line2D([], [], color=value_to_html_color(0.0), lw=10), # ] # legend_labels = [ # 'Zeer goed', 'Goed', 'Matig', 'Ontoereikend', 'Slecht'] # graph.legend(legend_handles, legend_labels, legend_location=6) yticklabels = [] block_width = (date2num(dt_end) - date2num(dt_start)) / 50 # Legend #graph.margin_right_extra += 90 # Room for legend. See also nens_graph. legend_handles = [ Line2D([], [], color=COLOR_1, lw=10), Line2D([], [], color=COLOR_2, lw=10), Line2D([], [], color=COLOR_3, lw=10), Line2D([], [], color=COLOR_4, lw=10), Line2D([], [], color=COLOR_5, lw=10), ] legend_labels = [ 'slecht', 'ontoereikend', 'matig', 'goed', 'zeer goed', ] graph.legend(legend_handles, legend_labels, legend_location=7) for index, graph_item in enumerate(graph_items): if not graph_item.location: graph_item.location = graph_settings['location'] # Find the corresponding Score. score = Score.from_graph_item(graph_item) if score.id is None: graph_item.label = '(%s)' % graph_item.label yticklabels.append(graph_item.label) # We want to draw a shadow past the end of the last # event. That's why we ignore dt_start. try: ts = graph_item.time_series(dt_end=dt_end, with_comments=True) except: logger.exception( 'HorizontalBarView crashed on graph_item.time_series of %s' % graph_item) ts = {} if len(ts) != 1: logger.warn('Warning: drawing %d timeseries on a single bar ' 'HorizontalBarView', len(ts)) # We assume there is only one timeseries. for (loc, par, unit), single_ts in ts.items(): dates, values, comments, flag_dates, flag_values, flag_comments = ( dates_values_comments(single_ts)) if not dates: logger.warning('Tried to draw empty timeseries %s %s', loc, par) continue block_dates = [] block_dates_shadow = [] for date_index in range(len(dates) - 1): dist_to_next = (date2num(dates[date_index + 1]) - date2num(dates[date_index])) this_block_width = min(block_width, dist_to_next) block_dates.append( (date2num(dates[date_index]), this_block_width)) block_dates_shadow.append( (date2num(dates[date_index]), dist_to_next)) block_dates.append( (date2num(dates[-1]), block_width)) # Ignoring tzinfo, otherwise we can't compare. last_date = max(dt_start.replace(tzinfo=None), dates[-1]) block_dates_shadow.append( (date2num(last_date), (date2num(dt_end) - date2num(dt_start)))) a, b, c, d = score.borders block_colors = [comment_to_html_color(comment) for comment in comments] # Block shadow graph.axes.broken_barh( block_dates_shadow, (index - 0.2, 0.4), facecolors=block_colors, edgecolors=block_colors, alpha=0.2) # The 'real' block graph.axes.broken_barh( block_dates, (index - 0.4, 0.8), facecolors=block_colors, edgecolors='grey') # for goal in graph_item.goals.all(): # collected_goal_timestamps.update([goal.timestamp, ]) # For each unique bar goal timestamp, generate a mini # graph. The graphs are ordered by timestamp. goal_timestamps = [ datetime.datetime(2015, 1, 1, 0, 0), datetime.datetime(2027, 1, 1, 0, 0), ] subplot_numbers = [312, 313] for index, goal_timestamp in enumerate(goal_timestamps): axes_goal = graph.figure.add_subplot(subplot_numbers[index]) axes_goal.set_yticks(range(len(yticklabels))) axes_goal.set_yticklabels('') axes_goal.set_xticks([0, ]) axes_goal.set_xticklabels([goal_timestamp.year, ]) for graph_item_index, graph_item in enumerate(graph_items): # TODO: make more efficient; score is retrieved twice # in this function. score = Score.from_graph_item(graph_item) #print 'score: %s' % score #print 'doel scores: %s' % str(score.targets) #a, b, c, d = score.borders goal = score.targets[index] if goal is not None: axes_goal.broken_barh( [(-0.5, 1)], (graph_item_index - 0.4, 0.8), facecolors=comment_to_html_color(goal), edgecolors='grey') # # 0 or 1 items # goals = graph_item.goals.filter(timestamp=goal_timestamp) # for goal in goals: # axes_goal.broken_barh( # [(-0.5, 1)], (graph_item_index - 0.4, 0.8), # facecolors=value_to_html_color(goal.value), # edgecolors='grey') axes_goal.set_xlim((-0.5, 0.5)) axes_goal.set_ylim(-0.5, len(yticklabels) - 0.5) # Coordinates are related to the graph size - not graph 311 bar_width_px = 12 axes_x = float(graph.width - (graph.MARGIN_RIGHT + graph.margin_right_extra) + bar_width_px + 2 * bar_width_px * index ) / graph.width axes_y = float(graph.MARGIN_BOTTOM + graph.margin_bottom_extra) / graph.height axes_width = float(bar_width_px) / graph.width axes_height = float(graph.graph_height()) / graph.height axes_goal.set_position((axes_x, axes_y, axes_width, axes_height)) graph.axes.set_yticks(range(len(yticklabels))) graph.axes.set_yticklabels(yticklabels) graph.axes.set_xlim(date2num((dt_start, dt_end))) graph.axes.set_ylim(-0.5, len(yticklabels) - 0.5) # Set the margins, including legend. graph.set_margins() return graph.png_response( response=HttpResponse(content_type='image/png'))