From 19490e62b2461aa7c111b9890c2d0cc544d37ba6 Mon Sep 17 00:00:00 2001 From: "Edgar P. Burkhart" Date: Mon, 19 Dec 2022 14:36:02 +0100 Subject: [PATCH] Add category plot --- nummi/plot/nummi.mplstyle | 2 +- nummi/plot/urls.py | 1 + nummi/plot/views.py | 25 +++++++++++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/nummi/plot/nummi.mplstyle b/nummi/plot/nummi.mplstyle index 5fd1a08b..90249c4 100644 --- a/nummi/plot/nummi.mplstyle +++ b/nummi/plot/nummi.mplstyle @@ -7,7 +7,7 @@ figure.figsize: 8, 4 figure.dpi: 300 axes.prop_cycle: cycler('color', ["66cc66", "#338033", "#99ff99", "#802653", "#cc6699"]) -axes.xmargin: 0 +axes.axisbelow: True axes.grid: True svg.fonttype: none diff --git a/nummi/plot/urls.py b/nummi/plot/urls.py index dde7b92..fbff014 100644 --- a/nummi/plot/urls.py +++ b/nummi/plot/urls.py @@ -4,4 +4,5 @@ from . import views urlpatterns = [ path("timeline", views.timeline, name="timeline"), + path("categories", views.categories, name="categories"), ] diff --git a/nummi/plot/views.py b/nummi/plot/views.py index fe417d8..781ab89 100644 --- a/nummi/plot/views.py +++ b/nummi/plot/views.py @@ -3,6 +3,7 @@ import io import matplotlib import matplotlib.pyplot as plt from matplotlib import dates as mdates +from django.db import models from django.contrib.auth.decorators import login_required from django.http import HttpResponse from django.utils.translation import gettext as _ @@ -33,6 +34,30 @@ def timeline(request): ax.xaxis.set_major_formatter( mdates.ConciseDateFormatter(ax.xaxis.get_major_locator()) ) + ax.autoscale(True, "x", True) + + _io = io.StringIO() + + fig.savefig(_io, format="svg") + + return HttpResponse(_io.getvalue(), headers={"Content-Type": "image/svg+xml"}) + + +@login_required +def categories(request): + _categories = Category.objects.all() + + fig, ax = plt.subplots(figsize=(8, _categories.count() / 4)) + ax.barh( + [str(c) for c in _categories][::-1], + [ + Transaction.objects.filter(category=c).aggregate(sum=models.Sum("value"))[ + "sum" + ] + for c in _categories + ][::-1], + ) + _io = io.StringIO() fig.savefig(_io, format="svg")