Exemplo n.º 1
0
 def test_graph(self):
     start_date = datetime.datetime(2010, 7, 1)
     end_date = datetime.datetime(2010, 10, 1)
     today = datetime.datetime(2010, 9, 8)
     graph = Graph(start_date, end_date, today=today)
     graph.add_today()
     graph.suptitle('hallo')
     graph.legend()
     self.assertTrue(graph.http_png())
Exemplo n.º 2
0
    def image(self, identifiers=None, start_date=None, end_date=None,
              width=None, height=None, layout_extra=None):
        """
        Create graph of given parameters
        for first identifier in identifiers object.
        Plots lines and bar charts depends on layout_extra['type'].
        Draws bar charts only for equidistant timeseries.
        """
        if layout_extra == None or len(layout_extra) <= 0:
            layout_extra = {
                "lines": [{"style": "-", "y-position": 1.97,
                           "color": "red", "width": 3, "name": "L1"},
                          {"style": "--", "y-position": 2.12,
                           "color": "green", "width": 3, "name": "L2"}],
                "type": "line",
                "style": '-',
                "width": 1}

        today = datetime.datetime.now()
        graph = Graph(start_date, end_date,
                      width=width, height=height, today=today)
        graph.axes.grid(True)
        graph.add_today()
        # Draw graph lines with extra's
        #title = None
        y_min, y_max = None, None
        #legend = None
        for identifier in identifiers:
            fewsnorm_source = self._fewsnorm_source(identifier['ident'])
            events = self.values(
                identifier, start_date, end_date,
                fewsnorm_source=fewsnorm_source)

            dates = []
            values = []
            for event in events:
                dates.append(event['datetime'])
                values.append(event['value'])
            if layout_extra.get('type') == "line":
                graph.axes.plot(dates,
                                values,
                                ls=layout_extra.get('style'),
                                lw=layout_extra.get('width'),
                                label=identifier['ident'])
            elif layout_extra.get('type') == "bar":
                time_delta = (end_date - start_date).days
                timestep = self._timestep(identifier, fewsnorm_source)
                if time_delta > 0 and timestep != None:
                    bar_width = (float(timestep) / 60 / 24) / float(time_delta)
                    graph.axes.bar(dates, values,
                                   edgecolor='Red',
                                   width=bar_width, label=identifier['ident'])
                for line in layout_extra.get('lines'):
                    graph.axes.axhline(y=line.get('y-position'),
                                       ls=line.get('style'),
                                       color=line.get('color'),
                                       lw=line.get('width'),
                                       label=line.get('name'))
            break

        graph.axes.set_ylabel(
            AdapterFewsNorm._unit(fewsnorm_source, self.parameter_id))

        graph.legend()
        graph.axes.legend_.draw_frame(False)

        return graph.http_png()