Esempio n. 1
0
    def _render_graph(
        self,
        identifiers,
        start_date,
        end_date,
        layout_extra=None,
        raise_404_if_empty=False,
        GraphClass=RainappGraph,
        **extra_params
    ):
        today_site_tz = self.tz.localize(datetime.datetime.now())
        start_date_utc, end_date_utc = self._to_utc(start_date, end_date)
        graph = GraphClass(start_date_utc,
                           end_date_utc,
                           today=today_site_tz,
                           tz=self.tz,
                           **extra_params)

        # Gets timeseries, draws the bars, sets  the legend
        for identifier in identifiers:
            location_name = self._get_location_name(identifier)
            cached_value_result = self._cached_values(identifier,
                                                      start_date_utc,
                                                      end_date_utc)
            dates_site_tz = [row['datetime'].astimezone(self.tz)
                             for row in cached_value_result]
            values = [row['value'] for row in cached_value_result]
            units = [row['unit'] for row in cached_value_result]
            unit = ''
            if len(units) > 0:
                unit = units[0]
            if values:
                unit_timedelta = UNIT_TO_TIMEDELTA.get(unit, None)
                if unit_timedelta:
                    # We can draw bars corresponding to period
                    bar_width = graph.get_bar_width(unit_timedelta)
                    offset = -1 * unit_timedelta
                    offset_dates = [d + offset for d in dates_site_tz]
                else:
                    # We can only draw spikes.
                    bar_width = 0
                    offset_dates = dates_site_tz
                graph.axes.bar(offset_dates,
                               values,
                               edgecolor='blue',
                               width=bar_width,
                               label=location_name)
            graph.set_ylabel(unit)
            # graph.legend()
            graph.suptitle(location_name)

            # Use first identifier and breaks the loop
            break

        graph.responseobject = HttpResponse(content_type='image/png')

        return graph.render()
    def _render_graph(
        self, identifiers, start_date, end_date, layout_extra=None,
        raise_404_if_empty=False, GraphClass=Graph, **extra_params):
        """
        Visualize timeseries in a graph.

        Legend is always drawn.

        New: this is now a more generalized version of image(), to
        support FlotGraph.
        """

        tz = pytz.timezone(settings.TIME_ZONE)
        today_site_tz = tz.localize(datetime.datetime.now())

        start_date_utc = dates.to_utc(start_date)
        end_date_utc = dates.to_utc(end_date)
        graph = GraphClass(start_date_utc,
                           end_date_utc,
                           today=today_site_tz,
                           tz=tz,
                           **extra_params)

        # Gets timeseries, draws the bars, sets  the legend
        for identifier in identifiers:
            location_name = self._grid_name(
                identifier['region_name'], identifier['identifier'])

            cached_value_result = self.values(identifier,
                                              start_date_utc,
                                              end_date_utc)

            dates_utc_timezoneaware = [row['datetime']
                                       for row in cached_value_result]
            values = [row['value'] for row in cached_value_result]
            units = [row['unit'] for row in cached_value_result]

            unit = ''
            if len(units) > 0:
                unit = units[0]
            if values:
                unit_timedelta = UNIT_TO_TIMEDELTA.get(unit, None)
                if unit_timedelta:
                    # We can draw bars corresponding to period
                    bar_width = graph.get_bar_width(unit_timedelta)
                    offset = -1 * unit_timedelta
                    offset_dates = [d + offset
                                    for d in dates_utc_timezoneaware]
                else:
                    # We can only draw spikes.
                    bar_width = 0
                    offset_dates = dates_utc_timezoneaware
                graph.axes.bar(offset_dates,
                               values,
                               edgecolor='blue',
                               width=bar_width,
                               label=location_name)
            graph.set_ylabel(unit)
            # graph.legend()
            graph.suptitle(location_name)

            # Use first identifier and breaks the loop
            break

        graph.responseobject = HttpResponse(content_type='image/png')

        return graph.render()
    def _render_graph(self,
                      identifiers,
                      start_date,
                      end_date,
                      layout_extra=None,
                      raise_404_if_empty=False,
                      GraphClass=Graph,
                      **extra_params):
        """
        Visualize timeseries in a graph.

        Legend is always drawn.

        New: this is now a more generalized version of image(), to
        support FlotGraph.
        """

        tz = pytz.timezone(settings.TIME_ZONE)
        today_site_tz = tz.localize(datetime.datetime.now())

        start_date_utc = dates.to_utc(start_date)
        end_date_utc = dates.to_utc(end_date)
        graph = GraphClass(start_date_utc,
                           end_date_utc,
                           today=today_site_tz,
                           tz=tz,
                           **extra_params)

        # Gets timeseries, draws the bars, sets  the legend
        for identifier in identifiers:
            location_name = self._grid_name(identifier['region_name'],
                                            identifier['identifier'])

            cached_value_result = self.values(identifier, start_date_utc,
                                              end_date_utc)

            dates_utc_timezoneaware = [
                row['datetime'] for row in cached_value_result
            ]
            values = [row['value'] for row in cached_value_result]
            units = [row['unit'] for row in cached_value_result]

            unit = ''
            if len(units) > 0:
                unit = units[0]
            if values:
                unit_timedelta = UNIT_TO_TIMEDELTA.get(unit, None)
                if unit_timedelta:
                    # We can draw bars corresponding to period
                    bar_width = graph.get_bar_width(unit_timedelta)
                    offset = -1 * unit_timedelta
                    offset_dates = [
                        d + offset for d in dates_utc_timezoneaware
                    ]
                else:
                    # We can only draw spikes.
                    bar_width = 0
                    offset_dates = dates_utc_timezoneaware
                graph.axes.bar(offset_dates,
                               values,
                               edgecolor='blue',
                               width=bar_width,
                               label=location_name)
            graph.set_ylabel(unit)
            # graph.legend()
            graph.suptitle(location_name)

            # Use first identifier and breaks the loop
            break

        graph.responseobject = HttpResponse(content_type='image/png')

        return graph.render()