mybuddy/reports/graphs/weight_weight.py

35 lines
1.1 KiB
Python
Raw Normal View History

2017-11-10 12:20:34 +00:00
# -*- coding: utf-8 -*-
from django.utils.translation import gettext as _
2017-11-10 12:20:34 +00:00
import plotly.offline as plotly
import plotly.graph_objs as go
from reports import utils
def weight_weight(objects):
"""
Create a graph showing weight over time.
:param objects: a QuerySet of Weight instances.
:returns: a tuple of the the graph's html and javascript.
"""
2022-02-10 00:00:30 +00:00
objects = objects.order_by("-date")
2017-11-10 12:20:34 +00:00
trace = go.Scatter(
2022-02-10 00:00:30 +00:00
name=_("Weight"),
x=list(objects.values_list("date", flat=True)),
y=list(objects.values_list("weight", flat=True)),
fill="tozeroy",
2017-11-10 12:20:34 +00:00
)
layout_args = utils.default_graph_layout_options()
2022-02-10 00:00:30 +00:00
layout_args["barmode"] = "stack"
layout_args["title"] = _("<b>Weight</b>")
layout_args["xaxis"]["title"] = _("Date")
layout_args["xaxis"]["rangeselector"] = utils.rangeselector_date()
layout_args["yaxis"]["title"] = _("Weight")
2017-11-10 12:20:34 +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)
2017-11-10 12:20:34 +00:00
return utils.split_graph_output(output)