Update graphics
This commit is contained in:
parent
d9abdf798f
commit
73ce1a298e
4 changed files with 110 additions and 63 deletions
|
@ -6,8 +6,6 @@ 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,
|
||||
|
@ -165,6 +163,27 @@ def snapshot(request, uuid=None):
|
|||
"form": _form,
|
||||
}
|
||||
|
||||
context["categories"] = (
|
||||
_snapshot.transactions.values("category", "category__name", "category__icon")
|
||||
.annotate(
|
||||
sum=models.Sum("value"),
|
||||
sum_m=models.Sum("value", filter=models.Q(value__lt=0)),
|
||||
sum_p=models.Sum("value", filter=models.Q(value__gt=0)),
|
||||
)
|
||||
.order_by("-sum")
|
||||
)
|
||||
context["cat_lim"] = max(
|
||||
map(
|
||||
abs,
|
||||
context["categories"]
|
||||
.aggregate(
|
||||
max=models.Max("sum_p"),
|
||||
min=models.Min("sum_m"),
|
||||
)
|
||||
.values(),
|
||||
)
|
||||
)
|
||||
context["cat_lim_m"] = -context["cat_lim"]
|
||||
return render(
|
||||
request,
|
||||
"main/snapshot.html",
|
||||
|
@ -172,65 +191,6 @@ 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"))
|
||||
)
|
||||
_categories = (
|
||||
_snapshot.transactions.values("category")
|
||||
.annotate(sum=models.Sum("value"))
|
||||
.order_by("-sum")
|
||||
)
|
||||
|
||||
print(_categories_p)
|
||||
print(_categories_m)
|
||||
fig, ax = plt.subplots(constrained_layout=True)
|
||||
ax.barh(
|
||||
[
|
||||
"*" if (c := _cat["category"]) is None else Category.objects.get(id=c).name
|
||||
for _cat in _categories
|
||||
],
|
||||
[_cat["sum"] for _cat in _categories],
|
||||
hatch="/",
|
||||
fill=False,
|
||||
zorder=10,
|
||||
)
|
||||
ax.barh(
|
||||
[
|
||||
"*" 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",
|
||||
zorder=9,
|
||||
)
|
||||
ax.barh(
|
||||
[
|
||||
"*" 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",
|
||||
zorder=9,
|
||||
)
|
||||
ax.grid(color="k", alpha=0.2)
|
||||
ax.set(xlabel="Value (€)")
|
||||
# fig.tight_layout()
|
||||
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