summaryrefslogtreecommitdiff
path: root/app/views/svg.py
blob: 06666b3ccfbb9a13caf90fac5dd02951a9ee2bc1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from ..flask_extend import Blueprint

from .expenses import calc_month_exp
from . import static_url
from ..model import Category

from pygal.style import DefaultStyle as Style
from pygal import Pie as _Pie

mod = Blueprint('svg', __name__)

Style.background = Style.plot_background = "rgba(255,0,0,0)"

def Pie(*args, **kwargs):
    # does not work as functools.partial, because static_url would be called without request
    return _Pie(*args, 
            style=Style, 
            js = [static_url('pygal/pygal.js', _external = True)],
            legend_font_size = 24,
            tooltip_font_size = 24,
            value_font_size = 24,
            margin = 0,
            legend_at_bottom = True,
            pretty_print = True, 
            **kwargs)

@mod.route("/month/<int(fixed_digits=4):year>/<int(fixed_digits=2):month>.svg")
def month(year, month):
    exp = calc_month_exp(year, month)
    pie = Pie()

    expenses = {}
    for c in exp.catexps:
        expenses[c.cat.name] = float(c.expense)

    for c in Category.query.order_by(Category.name).all():
        pie.add(c.name, expenses.get(c.name, 0.0))

    if not expenses:
        pie.raw_series = []
        pie.add("Empty", 0.1)

    return pie.render_response()