diff --git a/nummi/main/templates/main/snapshot.html b/nummi/main/templates/main/snapshot.html
index 18bde0b..e015765 100644
--- a/nummi/main/templates/main/snapshot.html
+++ b/nummi/main/templates/main/snapshot.html
@@ -31,6 +31,9 @@
+
Plot
+
+
{% if snapshot.transactions %}
Transactions ({% pmvalue sum %} / {% pmvalue snapshot.diff %})
diff --git a/nummi/main/urls.py b/nummi/main/urls.py
index 272fc29..4c23cb3 100644
--- a/nummi/main/urls.py
+++ b/nummi/main/urls.py
@@ -17,5 +17,6 @@ urlpatterns = [
path("category//del", views.del_category, name="del_category"),
path("snapshot", views.snapshot, name="snapshot"),
path("snapshot/", views.snapshot, name="snapshot"),
+ path("snapshot//graph.svg", views.snapshot_graph, name="snapshot_graph"),
path("snapshot//del", views.del_snapshot, name="del_snapshot"),
]
diff --git a/nummi/main/views.py b/nummi/main/views.py
index 2aafd3b..c4b7120 100644
--- a/nummi/main/views.py
+++ b/nummi/main/views.py
@@ -5,6 +5,9 @@ from django.contrib.auth import views as auth_views
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic import ListView
from django.core.paginator import Paginator
+from django.db import models
+import matplotlib.pyplot as plt
+import tempfile
from .models import (
Transaction,
@@ -169,6 +172,47 @@ def snapshot(request, uuid=None):
)
+@login_required
+def snapshot_graph(request, uuid):
+ _snapshot = get_object_or_404(Snapshot, id=uuid)
+ _categories_p = (
+ _snapshot.transactions.filter(value__gt=0)
+ .values("category")
+ .annotate(sum=models.Sum("value"))
+ )
+ _categories_m = (
+ _snapshot.transactions.filter(value__lt=0)
+ .values("category")
+ .annotate(sum=models.Sum("value"))
+ )
+
+ print(_categories_p)
+ print(_categories_m)
+ fig, ax = plt.subplots()
+ ax.bar(
+ [
+ "*" if (c := _cat["category"]) is None else Category.objects.get(id=c).name
+ for _cat in _categories_p
+ ],
+ [_cat["sum"] for _cat in _categories_p],
+ color="#007339",
+ )
+ ax.bar(
+ [
+ "*" if (c := _cat["category"]) is None else Category.objects.get(id=c).name
+ for _cat in _categories_m
+ ],
+ [_cat["sum"] for _cat in _categories_m],
+ color="#bf1500",
+ )
+ ax.grid(color="k", alpha=0.2)
+ ax.set(ylabel="Value (€)")
+ with tempfile.NamedTemporaryFile(suffix=".svg") as f:
+ fig.savefig(f.name)
+ f.seek(0)
+ return HttpResponse(f.read(), content_type="image/svg+xml")
+
+
@login_required
def del_snapshot(request, uuid):
_snapshot = get_object_or_404(Snapshot, id=uuid)