Пример #1
0
    def export(self, title, data):
        """Generate graph from the data.

        Example for the mem plugin:
        {'percent': [
            (datetime.datetime(2018, 3, 24, 16, 27, 47, 282070), 51.8),
            (datetime.datetime(2018, 3, 24, 16, 27, 47, 540999), 51.9),
            (datetime.datetime(2018, 3, 24, 16, 27, 50, 653390), 52.0),
            (datetime.datetime(2018, 3, 24, 16, 27, 53, 749702), 52.0),
            (datetime.datetime(2018, 3, 24, 16, 27, 56, 825660), 52.0),
            ...
            ]
        }

        Return:
        * True if the graph have been generated
        * False if the graph have not been generated
        """
        if data == {}:
            return False

        chart = DateTimeLine(title=title.capitalize(),
                             width=self.width,
                             height=self.height,
                             style=self.style,
                             show_dots=False,
                             legend_at_bottom=True,
                             x_label_rotation=20,
                             x_value_formatter=lambda dt: dt.strftime('%Y/%m/%d %H:%M:%S'))
        for k, v in iteritems(time_serie_subsample(data, self.width)):
            chart.add(k, v)
        chart.render_to_file(os.path.join(self.path,
                                          title + '.svg'))
        return True
Пример #2
0
 def _plot(self, x_title):
     plot = DateTimeLine(
         legend_at_bottom=True,
         title=self.hostname,
         x_label_rotation=35,
         x_value_formatter=lambda dt: dt.strftime('%Y.%m.%d %H:%M:%S')
     )
     plot.x_title = x_title
     return plot
Пример #3
0
    def test_datetimeline():
        line = DateTimeLine()
        from datetime import timezone, timedelta
        tz7 = timezone(timedelta(hours=7), 'GMT +7')
        tzn4 = timezone(timedelta(hours=-4), 'GMT -4')

        line.add(
            'dt', [(datetime(2013, 1, 12, 8, tzinfo=tz7), 300),
                   (datetime(2013, 1, 12, 8), 412),
                   (datetime(2013, 1, 12, 8, tzinfo=tzn4), 823)]
        )
        line.x_label_rotation = 45
        return line.render_response()
Пример #4
0
def test_datetime():
    """Test a simple datetimeline"""
    datetime_chart = DateTimeLine(truncate_label=1000)
    datetime_chart.add(
        'datetimes',
        [(datetime(2013, 1, 2, 1, 12, 29), 300),
         (datetime(2013, 1, 12, 21, 2, 29), 412),
         (datetime(2013, 2, 2, 12, 30, 59), 823), (datetime(2013, 2, 22), 672)]
    )

    q = datetime_chart.render_pyquery()
    dates = list(map(lambda t: t.split(' ')[0], q(".axis.x text").map(texts)))
    assert dates == [
        '2013-01-12T14:13:20', '2013-01-24T04:00:00', '2013-02-04T17:46:40',
        '2013-02-16T07:33:20'
    ]
    def create_graph(self, data, args):
        """Creates the graph based on rendering data"""
        style_arguments = {}
        for arg in self.allowed_style_url_args.keys():
            if arg in args:
                style_arguments.update({arg: args[arg]})

        line_chart_arguments = {
            'style': RedBlueStyle(**style_arguments),
            'x_label_rotation': 25,
            'x_value_formatter': lambda dt: dt.strftime('%y-%m-%d %H:%M'),
        }
        for arg in self.allowed_chart_url_args.keys():
            if arg in args:
                line_chart_arguments.update({arg: args[arg]})

        line_chart = DateTimeLine(**line_chart_arguments)
        if data:
            values = [(dateutil.parser.parse(price['date_seen']), price['value']) for price in data]
            line_chart.add(data[0]['currency'], values)
        return line_chart
Пример #6
0
 def test_datetimeline():
     line = DateTimeLine()
     line.add('dt', [
         (datetime(2013, 1, 12, 8, 0), 300),
         (datetime(2013, 1, 12, 12), 412),
         (datetime(2013, 2, 22, 12), 823),
         (datetime(2013, 2, 22, 20), 672)
     ])
     line.x_value_formatter = lambda x: x.strftime("%Y-%m-%d")
     line.x_label_rotation = 45
     return line.render_response()
Пример #7
0
    def test_datetimeline():
        line = DateTimeLine()
        from datetime import timezone, timedelta
        tz7 = timezone(timedelta(hours=7), 'GMT +7')
        tzn4 = timezone(timedelta(hours=-4), 'GMT -4')

        line.add('dt', [
            (datetime(2013, 1, 12, 8, tzinfo=tz7), 300),
            (datetime(2013, 1, 12, 8), 412),
            (datetime(2013, 1, 12, 8, tzinfo=tzn4), 823)
        ])
        line.x_value_formatter = lambda x: x.isoformat()  # strftime("%Y-%m-%d")
        line.x_label_rotation = 45
        return line.render_response()
Пример #8
0
    def test_datetimeline_with_pytz():
        import pytz
        tz = pytz.timezone('US/Eastern')

        line = DateTimeLine()
        line.add(
            'dt', [(tz.localize(datetime(2013, 1, 12, 8)), 300),
                   (tz.localize(datetime(2013, 1, 12, 10)), 600),
                   (tz.localize(datetime(2013, 1, 12, 14)), 30),
                   (tz.localize(datetime(2013, 1, 12, 16)), 200)]
        )
        from datetime import timezone
        line.x_value_formatter = lambda x: (
            x.replace(tzinfo=timezone.utc).astimezone(tz)).isoformat()
        # line.x_value_formatter = lambda x: tz.normalize(
        #     x.replace(tzinfo=pytz.utc)).isoformat()
        line.x_label_rotation = 45
        return line.render_response()