Commits

Tibor Tóth committed 4a02635 Merge

hg flow, merge release `charts` to develop branch `develop`

  • Participants
  • Parent commits 606d57a, e20b74e
  • Branches develop

Comments (0)

Files changed (6)

expense/templates/admin/expense/expense/bar_chart.html

+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
+<html>
+ <head>
+    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
+    <link href="layout.css" rel="stylesheet" type="text/css">
+    <!--[if lte IE 8]><script language="javascript" type="text/javascript" src="{{media_dir}}/js/flot/excanvas.min.js"></script><![endif]-->
+    <script language="javascript" type="text/javascript" src="{{media_dir}}js/flot/jquery.js"></script>
+    <script language="javascript" type="text/javascript" src="{{media_dir}}js/flot/jquery.flot.js"></script>
+
+    <script language="javascript" type="text/javascript" src="{{media_dir}}js/flot/jquery.flot.stack.js"></script>
+ </head>
+ <body>
+    <div id="placeholder" style="width:600px;height:300px;"></div>
+<script id="source">
+$(function () {
+    var stack = 0, bars = true, lines = false, steps = false;
+
+    var data = {{ chart_data|safe }};
+    var options = {
+        series: {
+            stack: stack,
+            lines: { show: lines, fill: true, steps: steps },
+            bars: { show: bars, barWidth: 0.9, align: 'center' }
+        },
+        xaxis: {ticks: {{ labels|safe }}}
+    };
+    $.plot($("#placeholder"), data, options);
+});
+</script>
+
+ </body>
+</html>

expense/templates/admin/expense/expense/change_list.html

   <h2>{% trans 'Filter' %}</h2>
   {% for spec in cl.filter_specs %}{% admin_list_filter cl spec %}{% endfor %}
   <h2 class="reports"><img src="{{ media_dir }}/images/bar-chart-icon.png">{% trans 'Reports' %}</h2>
+  <h3>{% trans 'Pie charts' %}</h3>
   <ul>
     {% for t in types_stat %}            
     <li>    
-      <a href="#" class="reportlink" id="chart_data-{{ t.id }}-{{ current_user_id }}-{{ current_year }}-{{current_month }}">
+      <a href="#" class="reportlink" id="pie_chart_data-{{ t.id }}-{{ current_user_id }}-{{ current_year }}-{{current_month }}">
+	{{ t.name }}
+      </a>
+    </li>  
+    {% endfor %}
+  </ul>
+  <h3>{% trans 'Bar charts' %}</h3>
+  <ul>
+    {% for t in types_stat %}            
+    <li>    
+      <a href="#" class="reportlink" id="bar_chart_data-{{ t.id }}-{{ current_user_id }}-{{ current_year }}-{{current_month }}">
 	{{ t.name }}
       </a>
     </li>  

expense/templates/admin/expense/expense/charts.html

-<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
-<html>
- <head>
-    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
-    <link href="layout.css" rel="stylesheet" type="text/css">
-    <!--[if lte IE 8]><script language="javascript" type="text/javascript" src="{{media_dir}}/js/flot/excanvas.min.js"></script><![endif]-->
-    <script language="javascript" type="text/javascript" src="{{media_dir}}js/flot/jquery.js"></script>
-    <script language="javascript" type="text/javascript" src="{{media_dir}}js/flot/jquery.flot.js"></script>
-
-    <script language="javascript" type="text/javascript" src="{{media_dir}}js/flot/jquery.flot.stack.js"></script>
- </head>
- <body>
-    <div id="placeholder" style="width:600px;height:300px;"></div>
-<script id="source">
-$(function () {
-    var stack = 0, bars = true, lines = false, steps = false;
-
-    var data = {{ chart_data|safe }};
-    var options = {
-        series: {
-            stack: stack,
-            lines: { show: lines, fill: true, steps: steps },
-            bars: { show: bars, barWidth: 0.9, align: 'center' }
-        },
-        xaxis: {ticks: {{ labels|safe }}}
-    };
-    $.plot($("#placeholder"), data, options);
-});
-</script>
-
- </body>
-</html>

expense/templates/admin/expense/expense/pie_chart.html

+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
+<html>
+ <head>
+    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
+    <link href="layout.css" rel="stylesheet" type="text/css">
+    <!--[if lte IE 8]><script language="javascript" type="text/javascript" src="{{media_dir}}/js/flot/excanvas.min.js"></script><![endif]-->
+    <script language="javascript" type="text/javascript" src="{{media_dir}}js/flot/jquery.js"></script>
+    <script language="javascript" type="text/javascript" src="{{media_dir}}js/flot/jquery.flot.js"></script>
+
+    <script language="javascript" type="text/javascript" src="{{media_dir}}js/flot/jquery.flot.pie.js"></script>
+ </head>
+ <body>
+    <div id="placeholder" style="width:600px;height:300px;"></div>
+<script id="source">
+$(function () {
+    var data = {{ chart_data|safe }};
+    var options = {
+	series: {
+	    pie: { 
+		show: true,
+		radius: 1,
+		label: {
+		    show: true,
+		    radius: 3/4,
+		    formatter: function(label, series){
+			return '<div style="font-size:8pt;text-align:center;padding:2px;color:white;">'+label+'<br/>'+Math.round(series.percent)+'%</div>';
+		    },
+		    background: { opacity: 0.5 }
+		}
+	    }
+	},
+	legend: {
+	    show: false
+	}
+    };
+    
+    $.plot($("#placeholder"), data, options);
+});
+</script>
+
+ </body>
+</html>
 from django.conf.urls.defaults import patterns, url
 import views
 
-urlpatterns = patterns('expense.views',
-                      url(r'^chart_data/(?P<user_id>\d+)/(?P<type>\d+)/(?P<year>\d+)/(?P<month>\d+)', 
-                          views.chart_data),
+urlpatterns = patterns(
+    'expense.views',
+    url(r'^bar_chart_data/(?P<user_id>\d+)/(?P<type>\d+)/(?P<year>\d+)/(?P<month>\d+)', 
+        views.bar_chart_data),
+    url(r'^pie_chart_data/(?P<user_id>\d+)/(?P<type>\d+)/(?P<year>\d+)/(?P<month>\d+)', 
+        views.pie_chart_data),
 )
+
 from django.conf import settings
 import models
 
-def chart_data(request, user_id, type, year, month):
+def bar_chart_data(request, user_id, type, year, month):
     expense_type =  models.Type.objects.get(id = type)
-    data =  _get_chart_data(expense_type, user_id, year, month)
+    data =  _get_bar_chart_data(expense_type, user_id, year, month)
     labels =  []
     index = 0
     for c in expense_type.categories.all().order_by('name'):
         labels.append([index, c.name])
         index = index + 1
     
-    t = loader.get_template('admin/expense/expense/charts.html')
+    t = loader.get_template('admin/expense/expense/bar_chart.html')
     c = Context({
         'media_dir': settings.MEDIA_URL + "/expense/", 
         'type': expense_type.name, 
         'labels': simplejson.dumps(labels), 
     })
     return HttpResponse(t.render(c))
-    
-def _get_chart_data(type, user_id=None, year = None, month = None):
+   
+def _get_bar_chart_data(type, user_id=None, year = None, month = None):
     results = []
     users = []
     q = Q()
         results.append({'label': u.username, 'data': user_data})
 
     return results
+
+def pie_chart_data(request, user_id, type, year, month):
+    expense_type =  models.Type.objects.get(id = type)
+    data =  _get_pie_chart_data(expense_type, user_id, year, month)
+    t = loader.get_template('admin/expense/expense/pie_chart.html')
+    c = Context({
+        'media_dir': settings.MEDIA_URL + "/expense/", 
+        'type': expense_type.name, 
+        'year':  year, 
+        'month': month, 
+        'chart_data': simplejson.dumps(data),
+    })
+    return HttpResponse(t.render(c))
+
+def _get_pie_chart_data(type, user_id=None, year = None, month = None):
+    results = []
+    q = Q()
+    if user_id and int(user_id) > 0:
+        q = Q(expenses__user__id = user_id)
+    if year and int(year) > 0:
+        q = q & Q(expenses__date__year = year)
+    if month and int(month) > 0:
+        q = q & Q(expenses__date__month = month)
+    
+    #calculate values
+    
+    type.category_list = type.categories.filter(q).annotate(
+        category_total = Sum('expenses__amount'))
+
+    #format the results
+    for c in type.category_list:
+        results.append({
+                'label': c.name,
+                'data': c.category_total
+                })
+
+    return results
+
+
+