From cc7d2620e8585e7ab01a02ec2288c2bf760535be Mon Sep 17 00:00:00 2001 From: Joachim Lusiardi Date: Mon, 2 May 2016 06:42:43 +0200 Subject: [PATCH] Worked on charts --- app/rollerverbrauch/filters.py | 13 +++++ app/rollerverbrauch/static/main.js | 8 +-- app/rollerverbrauch/templates/layout.html | 28 ++++++++++ app/rollerverbrauch/templates/statistics.html | 52 ++++++------------- app/rollerverbrauch/tools.py | 12 ++++- 5 files changed, 73 insertions(+), 40 deletions(-) diff --git a/app/rollerverbrauch/filters.py b/app/rollerverbrauch/filters.py index 00a0e59..f3ce080 100644 --- a/app/rollerverbrauch/filters.py +++ b/app/rollerverbrauch/filters.py @@ -1,4 +1,5 @@ from rollerverbrauch import app +import hashlib @app.template_filter('none_filter') @@ -7,3 +8,15 @@ def none_filter(value): return '' else: return value + + +@app.template_filter('md5') +def md5_filter(value): + m = hashlib.md5() + m.update(str(value).encode('UTF-8')) + return m.hexdigest() + + +@app.template_filter('str') +def str_filter(value): + return str(value) diff --git a/app/rollerverbrauch/static/main.js b/app/rollerverbrauch/static/main.js index dc9ca89..0724e5d 100644 --- a/app/rollerverbrauch/static/main.js +++ b/app/rollerverbrauch/static/main.js @@ -12,18 +12,18 @@ function createChart(id, data, unit) { "axisAlpha": 0, "position": "left", "ignoreAxisWidth":true, - "title": "Fuelled litres" +// "title": unit }], "balloon": { "borderThickness": 1, - "shadowAlpha": 10 + "shadowAlpha": 10, }, "graphs": [{ "id": "g1", "balloon":{ "drop":true, "adjustBorderColor":false, - "color":"#ffffff" + "color":"#ffffff", }, "bullet": "round", "bulletBorderAlpha": 1, @@ -31,7 +31,7 @@ function createChart(id, data, unit) { "bulletSize": 5, "hideBulletsCount": 50, "lineThickness": 2, - "title": "Fuelled litres", + // "title": unit, //"useLineColorForBulletBorder": true, "valueField": "value", "balloonText": "[[value]] "+unit+"" diff --git a/app/rollerverbrauch/templates/layout.html b/app/rollerverbrauch/templates/layout.html index 2f4f726..8b7e982 100644 --- a/app/rollerverbrauch/templates/layout.html +++ b/app/rollerverbrauch/templates/layout.html @@ -59,6 +59,31 @@ {% endmacro %} +{% macro chartScript(divId, data, unit)%} + {% set hash = divId | md5 %} + + data_{{ hash }} = [{% for stop in data %}{ + "date": "{{stop.date}}", + "value": {{stop.value}} + }{% if not loop.last %},{%endif%} + {% endfor%} + ] + var chart_{{ hash }} = createChart('{{divId}}', data_{{ hash }}, '{{unit}}'); + + function zoom_chart_{{ hash }}() { + chart_{{ hash }}.zoomToIndexes( + chart_{{ hash }}.dataProvider.length - 40, + chart_{{ hash }}.dataProvider.length - 1 + ); + } + + chart_{{ hash }}.addListener("rendered", zoom_chart_{{ hash }}); + + zoom_chart_{{ hash }}() + +{% endmacro %} + + @@ -94,6 +119,9 @@ + + + diff --git a/app/rollerverbrauch/templates/statistics.html b/app/rollerverbrauch/templates/statistics.html index b40700e..24fa010 100644 --- a/app/rollerverbrauch/templates/statistics.html +++ b/app/rollerverbrauch/templates/statistics.html @@ -57,13 +57,27 @@
-
+ {% if vehicle.pitstop_count > 0 %} +
+ + {% else %} + not enough data. + {% endif %}
-
+ {% if vehicle.pitstop_count > 0 %} +
+ + {% else %} + not enough data. + {% endif %}
- + {% endfor %} @@ -78,39 +92,7 @@ }); - - - - {% endblock %} diff --git a/app/rollerverbrauch/tools.py b/app/rollerverbrauch/tools.py index ff79704..fca48c8 100644 --- a/app/rollerverbrauch/tools.py +++ b/app/rollerverbrauch/tools.py @@ -11,10 +11,14 @@ class VehicleStats: self.overall_litres = 0 self.average_litres_fuelled = 0 self.average_litres_used = 0 - self.pitstops = vehicle.pitstops + self.litres = [] + self.odometers = [] + if self.pitstop_count > 0: for pitstop in vehicle.pitstops: self.overall_litres += pitstop.litres + self.litres.append(StatsEvent(pitstop.date, pitstop.litres)) + self.odometers.append(StatsEvent(pitstop.date, pitstop.odometer)) self.average_litres_fuelled = self.overall_litres / self.pitstop_count if self.pitstop_count > 1: self.overall_distance = vehicle.pitstops[-1].odometer - vehicle.pitstops[0].odometer @@ -22,6 +26,12 @@ class VehicleStats: self.average_litres_used = 100 * (self.overall_litres - vehicle.pitstops[0].litres) / self.overall_distance +class StatsEvent: + def __init__(self, date, value): + self.date = date + self.value = value + + def db_log_add(entity): logging.info('db_add: %s' % str(entity))