From 9771a7352e0eafb8ed011f1bee98a537e23ebaa5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20=27Necoro=27=20Neumann?= Date: Sun, 1 May 2016 14:36:48 +0200 Subject: Add normale expenses to stats-page --- app/views/stats.py | 17 ++++++++++----- static/js/kosten.js | 54 ++++++++++++++++++++++++++++++++++------------ static/js/kosten.ls | 24 ++++++++++++++++----- templates/stats/show.jinja | 3 +-- 4 files changed, 72 insertions(+), 26 deletions(-) diff --git a/app/views/stats.py b/app/views/stats.py index 83c8154..639f73b 100644 --- a/app/views/stats.py +++ b/app/views/stats.py @@ -4,10 +4,11 @@ from . import Blueprint, flash, db, \ today from .. import forms as F -from ..model import ConstExpense +from ..model import ConstExpense, SingleExpense import sqlalchemy as sql import calendar from collections import defaultdict +from datetime import date from flask import jsonify mod = Blueprint('stats', __name__) @@ -35,15 +36,21 @@ def const_dialog(year,month): @templated def show(): # easy way: fetch them all and then do some computation - expenses = defaultdict(int) + consts = defaultdict(int) t = today().replace(day = 1) for e in ConstExpense.of(current_user): cur = e.start end = min(e.end, t) while cur <= end: - expenses[date_to_ms(cur)] += e.monthly + consts[date_to_ms(cur)] += e.monthly cur = next_date(cur) - expenses = list(sorted(expenses.iteritems())) + consts = list(sorted(consts.iteritems())) - return { 'consts': expenses } + expQuery = SingleExpense.of(current_user)\ + .group_by(SingleExpense.year, SingleExpense.month)\ + .values(SingleExpense.year, SingleExpense.month, sql.func.sum(SingleExpense.expense)) + + expenses = list(sorted((date_to_ms(date(year,month,1)), exp) for (year, month, exp) in expQuery)) + + return { 'consts': consts, 'expenses' : expenses } diff --git a/static/js/kosten.js b/static/js/kosten.js index a1c30fb..936e38c 100644 --- a/static/js/kosten.js +++ b/static/js/kosten.js @@ -1,4 +1,4 @@ -// Generated by LiveScript 1.2.0 +// Generated by LiveScript 1.4.0 (function(){ var jq, setLang, extendDate, addJS, showJS, statJS, catsJS, out$ = typeof exports != 'undefined' && exports || this; jQuery.fn.extend({ @@ -135,12 +135,12 @@ }); }); out$.statJS = statJS = jq(function(){ - var month, constDialog; + var df, month, constDialog; setLang(); + df = Highcharts.dateFormat; month = 30 * 24 * 60 * 60 * 1000; constDialog = function(){ - var df, time; - df = Highcharts.dateFormat; + var time; time = this.x; $.get(df('/stats/_const/%Y/%m', time), function(data){ $(data).dialog({ @@ -169,11 +169,7 @@ }, plotOptions: { series: { - point: { - events: { - click: constDialog - } - } + stacking: 'normal' } }, chart: { @@ -194,12 +190,42 @@ minTickInterval: month, minRange: month }, - series: [{ - data: x$.data('stats'), - step: 'left' - }], + yAxis: { + reversedStacks: false, + tickInterval: 250 + }, + series: [ + { + data: x$.data('consts'), + step: 'left', + name: 'Konstant', + point: { + events: { + click: constDialog + } + } + }, { + data: x$.data('expenses'), + name: 'Variabel', + step: 'left' + } + ], tooltip: { - pointFormat: '{point.y} €
' + shared: true, + formatter: function(){ + var header, body, p, footer; + header = "" + df('%B %Y', this.x) + "
"; + body = (function(){ + var i$, ref$, len$, results$ = []; + for (i$ = 0, len$ = (ref$ = this.points).length; i$ < len$; ++i$) { + p = ref$[i$]; + results$.push(p.series.name + ": " + p.point.y + " €
"); + } + return results$; + }.call(this)).join(''); + footer = "Summe: " + this.points[0].total + ""; + return header + body + footer; + } } }); }); diff --git a/static/js/kosten.ls b/static/js/kosten.ls index 163d670..699fcd4 100644 --- a/static/js/kosten.ls +++ b/static/js/kosten.ls @@ -113,10 +113,10 @@ export showJS = jq !-> export statJS = jq !-> set-lang! + df = Highcharts.date-format month = 30d * 24h * 60min * 60s * 1000ms const-dialog = !-> - df = Highcharts.date-format time = @x data <-! $.get df '/stats/_const/%Y/%m' time @@ -140,7 +140,7 @@ export statJS = jq !-> 0,0,0,0 # time plot-options: series: - point: events: click: const-dialog + stacking: \normal chart: events: click: !-> @@ -151,13 +151,27 @@ export statJS = jq !-> x-axis: min-tick-interval: month min-range: month + y-axis: + reversed-stacks: false + tick-interval: 250 series: [ - data: ..data \stats + # const + * data: ..data \consts + step: \left + name: \Konstant + point: events: click: const-dialog + # normal exps + * data: ..data \expenses + name: \Variabel step: \left ] tooltip: - point-format: '{point.y} €
' - + shared: true + formatter: -> + header = "#{df '%B %Y' @x}
" + body = ["#{p.series.name}: #{p.point.y} €
" for p in @points] .join '' + footer = "Summe: #{@points.0.total}" + header + body + footer # Categories export catsJS = jq !-> diff --git a/templates/stats/show.jinja b/templates/stats/show.jinja index 859a80b..1a2614e 100644 --- a/templates/stats/show.jinja +++ b/templates/stats/show.jinja @@ -13,6 +13,5 @@ {% endblock %} {% block content %} -

Konstante Kosten

-
+
{% endblock content %} -- cgit v1.2.3