2020-01-26 22:12:27 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from django.utils import timezone
|
|
|
|
from django.utils.translation import gettext as _
|
|
|
|
|
|
|
|
import plotly.offline as plotly
|
|
|
|
import plotly.graph_objs as go
|
|
|
|
|
|
|
|
from reports import utils
|
|
|
|
|
|
|
|
|
|
|
|
def diaperchange_amounts(instances):
|
|
|
|
"""
|
|
|
|
Create a graph showing daily diaper change amounts over time.
|
|
|
|
:param instances: a QuerySet of DiaperChange instances.
|
2024-01-28 03:42:12 +00:00
|
|
|
:returns: a tuple of the graph's html and javascript.
|
2020-01-26 22:12:27 +00:00
|
|
|
"""
|
|
|
|
totals = {}
|
|
|
|
for instance in instances:
|
|
|
|
time_local = timezone.localtime(instance.time)
|
|
|
|
date = time_local.date()
|
|
|
|
if date not in totals.keys():
|
|
|
|
totals[date] = 0
|
|
|
|
totals[date] += instance.amount or 0
|
|
|
|
|
|
|
|
amounts = [round(amount, 2) for amount in totals.values()]
|
|
|
|
trace = go.Bar(
|
2022-02-10 00:00:30 +00:00
|
|
|
name=_("Diaper change amount"),
|
2020-01-26 22:12:27 +00:00
|
|
|
x=list(totals.keys()),
|
|
|
|
y=amounts,
|
2022-02-10 00:00:30 +00:00
|
|
|
hoverinfo="text",
|
|
|
|
textposition="outside",
|
|
|
|
text=amounts,
|
2020-01-26 22:12:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
layout_args = utils.default_graph_layout_options()
|
2022-02-10 00:00:30 +00:00
|
|
|
layout_args["title"] = _("<b>Diaper Change Amounts</b>")
|
|
|
|
layout_args["xaxis"]["title"] = _("Date")
|
|
|
|
layout_args["xaxis"]["rangeselector"] = utils.rangeselector_date()
|
|
|
|
layout_args["yaxis"]["title"] = _("Change amount")
|
2020-01-26 22:12:27 +00:00
|
|
|
|
2022-02-10 00:00:30 +00:00
|
|
|
fig = go.Figure({"data": [trace], "layout": go.Layout(**layout_args)})
|
|
|
|
output = plotly.plot(fig, output_type="div", include_plotlyjs=False)
|
2020-01-26 22:12:27 +00:00
|
|
|
return utils.split_graph_output(output)
|