Add category plot

This commit is contained in:
Edgar P. Burkhart 2022-12-19 14:36:02 +01:00
parent e095d8d35f
commit 19490e62b2
Signed by: edpibu
GPG key ID: 9833D3C5A25BD227
3 changed files with 27 additions and 1 deletions

View file

@ -7,7 +7,7 @@ figure.figsize: 8, 4
figure.dpi: 300 figure.dpi: 300
axes.prop_cycle: cycler('color', ["66cc66", "#338033", "#99ff99", "#802653", "#cc6699"]) axes.prop_cycle: cycler('color', ["66cc66", "#338033", "#99ff99", "#802653", "#cc6699"])
axes.xmargin: 0 axes.axisbelow: True
axes.grid: True axes.grid: True
svg.fonttype: none svg.fonttype: none

View file

@ -4,4 +4,5 @@ from . import views
urlpatterns = [ urlpatterns = [
path("timeline", views.timeline, name="timeline"), path("timeline", views.timeline, name="timeline"),
path("categories", views.categories, name="categories"),
] ]

View file

@ -3,6 +3,7 @@ import io
import matplotlib import matplotlib
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
from matplotlib import dates as mdates from matplotlib import dates as mdates
from django.db import models
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.http import HttpResponse from django.http import HttpResponse
from django.utils.translation import gettext as _ from django.utils.translation import gettext as _
@ -33,6 +34,30 @@ def timeline(request):
ax.xaxis.set_major_formatter( ax.xaxis.set_major_formatter(
mdates.ConciseDateFormatter(ax.xaxis.get_major_locator()) 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() _io = io.StringIO()
fig.savefig(_io, format="svg") fig.savefig(_io, format="svg")