Add snapshot plot
This commit is contained in:
parent
14fa4b38d0
commit
b1b3a5afa2
3 changed files with 48 additions and 0 deletions
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue