2017-11-04 03:03:24 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2019-04-14 20:32:46 +00:00
|
|
|
from django.utils.translation import gettext as _
|
|
|
|
|
2017-11-04 03:03:24 +00:00
|
|
|
import plotly.offline as plotly
|
|
|
|
import plotly.graph_objs as go
|
|
|
|
|
|
|
|
from reports import utils
|
|
|
|
|
|
|
|
|
|
|
|
def diaperchange_lifetimes(changes):
|
|
|
|
"""
|
|
|
|
Create a graph showing how long diapers last (time between changes).
|
|
|
|
:param changes: a QuerySet of Diaper Change instances.
|
|
|
|
:returns: a tuple of the the graph's html and javascript.
|
|
|
|
"""
|
|
|
|
changes = changes.order_by('time')
|
|
|
|
durations = []
|
|
|
|
last_change = changes.first()
|
|
|
|
for change in changes[1:]:
|
|
|
|
duration = change.time - last_change.time
|
|
|
|
if duration.seconds > 0:
|
|
|
|
durations.append(duration)
|
|
|
|
last_change = change
|
|
|
|
|
|
|
|
trace = go.Box(
|
|
|
|
y=[round(d.seconds/3600, 2) for d in durations],
|
2019-04-14 20:32:46 +00:00
|
|
|
name=_('Changes'),
|
2017-11-04 03:03:24 +00:00
|
|
|
jitter=0.3,
|
|
|
|
pointpos=-1.8,
|
|
|
|
boxpoints='all'
|
|
|
|
)
|
|
|
|
|
|
|
|
layout_args = utils.default_graph_layout_options()
|
|
|
|
layout_args['height'] = 800
|
2019-04-14 20:32:46 +00:00
|
|
|
layout_args['title'] = _('<b>Diaper Lifetimes</b>')
|
|
|
|
layout_args['yaxis']['title'] = _('Time between changes (hours)')
|
2017-11-04 03:03:24 +00:00
|
|
|
layout_args['yaxis']['zeroline'] = False
|
|
|
|
layout_args['yaxis']['dtick'] = 1
|
|
|
|
|
|
|
|
fig = go.Figure({
|
|
|
|
'data': [trace],
|
|
|
|
'layout': go.Layout(**layout_args)
|
|
|
|
})
|
|
|
|
output = plotly.plot(fig, output_type='div', include_plotlyjs=False)
|
|
|
|
return utils.split_graph_output(output)
|