From 9854a17f4efb9e4d561d38c99764010e24820b7b Mon Sep 17 00:00:00 2001 From: Nikolai Kristiansen Date: Sun, 15 Apr 2012 17:50:13 +0200 Subject: [PATCH] Stats: hourly --- main/urls.py | 1 + main/views.py | 18 +++++++++++++- static/js/stats.js | 56 +++++++++++++++++++++++++++++++++++++++++++- templates/stats.html | 23 +++++++++++------- 4 files changed, 87 insertions(+), 11 deletions(-) diff --git a/main/urls.py b/main/urls.py index 9eccc07..4203ee6 100644 --- a/main/urls.py +++ b/main/urls.py @@ -7,6 +7,7 @@ urlpatterns = patterns('main.views', url(r'^log/all$', 'log', kwargs={'limit': None}, name='log-all'), url(r'^stats/$', 'stats', name='stats'), url(r'^stats/orders/$', 'stats_orders', name='stats-orders'), + url(r'^stats/orders/hourly$', 'stats_orders_hourly', name='stats-orders-hourly'), url(r'^stats/products/$', 'stats_products', name='stats-products'), url(r'^stats/products/(?P[0-9]+)/$', 'stats_products', name='stats-product'), url(r'^products/(?P[0-9]+)/$', 'products', name='json-product'), diff --git a/main/views.py b/main/views.py index ce5bc28..9200a60 100755 --- a/main/views.py +++ b/main/views.py @@ -1,6 +1,7 @@ # coding: utf-8 -from datetime import timedelta from datetime import datetime +from datetime import timedelta +from itertools import groupby import simplejson as json from django.contrib.auth import login, logout as auth_logout @@ -210,6 +211,21 @@ def stats_orders(request): f_per_user.append([who, num]) return HttpResponse(json.dumps(f_per_user), content_type='application/javascript; charset=utf8') +def stats_orders_hourly(request): + orders = Order.objects.all() + f_hourly = dict([(key,0) for key in range(0,24)]) # init + # group by hour + for key, values in groupby(orders, key=lambda row: row.created.hour): + count = len(list(values)) + f_hourly[key] = f_hourly[key] + count + + response = { + 'start': str(orders[0].created), + 'hourly' : [[k,v] for k,v in f_hourly.iteritems()], + 'total' : sum(f_hourly.values()), + } + return HttpResponse(json.dumps(response), content_type='application/javascript; charset=utf8') + def logout(request): auth_logout(request) return render_to_response('registration/logout.html', locals(), context_instance=RequestContext(request)) diff --git a/static/js/stats.js b/static/js/stats.js index 92cacf1..1a3f15e 100644 --- a/static/js/stats.js +++ b/static/js/stats.js @@ -1,7 +1,9 @@ $(document).ready(function() { + /* TODO select data element with index 0 of each pie-chart */ var charts = []; var products_url = '/stats/products/'; var order_url = '/stats/orders/'; + var order_hourly_url = '/stats/orders/hourly'; /* orders */ $.getJSON(order_url, function(data) { @@ -35,7 +37,7 @@ $(document).ready(function() { color: '#000000', connectorColor: '#000000', formatter: function() { - return ''+ this.point.name +': '+ Highcharts.numberFormat(this.percentage, 1) +' %'; + return ''+ this.point.name +': '+ this.y; } } } @@ -97,4 +99,56 @@ $(document).ready(function() { charts.push(new Highcharts.Chart(options)); } }); + /* orders hourly */ + $.getJSON(order_hourly_url, function(data) { + var render_to = 'order_hourly_chart'; + + // define the options + var options = { + credits: { + enabled: false + }, + chart: { + renderTo: render_to, + type: 'column' + }, + title: { + text: 'Ordre - time for time' + }, + tooltip: { + formatter: function() { + return this.y + " stk"; + } + }, + yAxis: { + title: { + text: 'Ordre' + }, + }, + xAxis: { + title: { + text: 'Tid (h)' + }, + labels: { + formatter: function() { + return this.value + ":00"; + }, + } + }, + plotOptions: { + column: { + minPointLength: 3, + }, + series: { + groupPadding: 0, + pointPadding: 0.05, + }, + }, + series: [{ + name: 'Ordre', + data: data.hourly + }] + }; + charts.push(new Highcharts.Chart(options)); + }); }); diff --git a/templates/stats.html b/templates/stats.html index be9792e..fb51f83 100644 --- a/templates/stats.html +++ b/templates/stats.html @@ -5,25 +5,30 @@ {% block content %}
-
-
-

Ordre

-
- -
-
-

Produkter

{% for product in products %}
-
{% endfor %}
+
+

Time for time

+
+
+
+
+ +
+
+

Ordre

+
+
+
+
{% endblock %} {% block tailinc %}