Add history chart in categories

This commit is contained in:
Edgar P. Burkhart 2023-04-18 14:15:09 +02:00
parent 9fd41851bc
commit 3f92a6d2ba
Signed by: edpibu
GPG key ID: 9833D3C5A25BD227
3 changed files with 28 additions and 7 deletions

View file

@ -24,18 +24,15 @@ table.full-width col.bar {width: auto}
right: 0;
border-radius: var(--radius) 0 0 var(--radius);
}
.plot td.bar.m div:not(.tot) {
background: var(--red-1);
}
.plot td.bar.p div:not(.tot) {
background: var(--green-1);
}
.plot td.bar.m div {background: var(--red-1)}
.plot td.bar.p div {background: var(--green-1)}
.plot td.bar div.tot {
z-index: 10;
height: .5rem;
background: black;
}
.plot td.bar.m div.tot {background: var(--red)}
.plot td.bar.p div.tot {background: var(--green)}
.plot td.bar div.tot span {
position: absolute;
display: inline-block;

View file

@ -11,6 +11,9 @@
<link rel="stylesheet"
href="{% static 'main/css/table.css' %}"
type="text/css"/>
<link rel="stylesheet"
href="{% static 'main/css/plot.css' %}"
type="text/css"/>
{% endblock %}
{% block body %}
<h1>
@ -23,5 +26,7 @@
{% if form.instance.transactions %}
<h2>{% translate "Transactions" %}</h2>
{% include "main/table/transaction.html" %}
<h2>{% translate "History" %}</h2>
{% include "main/plot/history.html" %}
{% endif %}
{% endblock %}

View file

@ -228,11 +228,30 @@ class CategoryUpdateView(NummiUpdateView):
def get_context_data(self, **kwargs):
data = super().get_context_data(**kwargs)
category = data["form"].instance
_history = (
category.transaction_set.values(month=models.functions.TruncMonth("date"))
.annotate(
sum_p=models.Sum("value", filter=models.Q(value__gt=0)),
sum_m=models.Sum("value", filter=models.Q(value__lt=0)),
sum=models.Sum("value"),
)
.order_by("-month")
)
return data | {
"transactions": category.transaction_set.all()[:8],
"transactions_url": reverse_lazy(
"category_transactions", args=(category.pk,)
),
"history": {
"data": _history,
"max": max(
_history.aggregate(
max=models.Max("sum_p", default=0),
min=-models.Min("sum_m", default=0),
).values(),
),
},
}