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
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
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
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()
def test_datetime(): 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() assert list(map(lambda t: t.split(' ')[0], q(".axis.x text").map(texts))) == [ '2013-01-12T15:13:20', '2013-01-24T05:00:00', '2013-02-04T18:46:40', '2013-02-16T08:33:20' ]
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
def get(self, request): glucose = BloodGlucoseResults.objects.filter( user=request.user, time__lt=datetime.now(), time__gte=(datetime.now() - timedelta(hours=24))) custom_style = Style(background='transparent', plot_background='transparent', foreground_subtle='#FFD200', opacity='.6', opacity_hover='.9', transition='400ms ease-in', colors=('#FFD200', '#5B5B5B')) chart = DateTimeLine(height=600, width=1000, explicit_size=True, style=custom_style, show_legend=False, x_value_formatter=lambda dt: dt.strftime("%H:%M")) chart.add("Poziom cukru", [(item.time, item.glucose) for item in glucose]) rendered_chart = chart.render(unicode=True) ctx = {} print(rendered_chart) ctx['chart'] = rendered_chart.decode("utf-8") meals = Meals.objects.filter(user=request.user, time__lt=datetime.now(), time__gte=(datetime.now() - timedelta(hours=24))) insulin_inj = InsulinInjections.objects.filter( user=request.user, time__lt=datetime.now(), time__gte=(datetime.now() - timedelta(hours=24))) ctx['meals'] = meals ctx['insulin_inj'] = insulin_inj return render(request, 'smartdiabetes/stat.html', ctx)
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
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()
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()
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()
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()
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()
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()