2017-08-24 18:54:01 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2024-01-28 03:42:12 +00:00
|
|
|
import time
|
|
|
|
|
|
|
|
|
|
|
|
def autorangeoptions(dates, padding=10000000):
|
|
|
|
"""
|
|
|
|
Default autorange mix and max for all graphs.
|
|
|
|
See: https://github.com/babybuddy/babybuddy/issues/706
|
|
|
|
:param dates: list of datetime.date objects organized latest to oldest.
|
|
|
|
:param padding: additional padding to add to the bounds.
|
|
|
|
:return: a dict of our autorange options.
|
|
|
|
"""
|
|
|
|
return dict(
|
|
|
|
{
|
|
|
|
"minallowed": int(time.mktime(dates[-1].timetuple())) * 1000 - padding,
|
|
|
|
"maxallowed": int(time.mktime(dates[0].timetuple())) * 1000 + padding,
|
|
|
|
},
|
|
|
|
)
|
2017-08-24 18:54:01 +00:00
|
|
|
|
2018-02-18 10:01:55 +00:00
|
|
|
|
2017-08-24 18:54:01 +00:00
|
|
|
def default_graph_layout_options():
|
2017-11-04 03:03:24 +00:00
|
|
|
"""
|
|
|
|
Default layout options for all graphs.
|
|
|
|
:returns: a dict of default options.
|
2017-08-24 18:54:01 +00:00
|
|
|
"""
|
|
|
|
return {
|
2022-02-10 00:00:30 +00:00
|
|
|
"paper_bgcolor": "rgb(52, 58, 64)",
|
|
|
|
"plot_bgcolor": "rgb(52, 58, 64)",
|
|
|
|
"font": {
|
|
|
|
"color": "rgba(255, 255, 255, 1)",
|
2017-08-24 18:54:01 +00:00
|
|
|
# Bootstrap 4 font family.
|
2022-02-10 00:00:30 +00:00
|
|
|
"family": '-apple-system, BlinkMacSystemFont, "Segoe UI", '
|
|
|
|
'Roboto, "Helvetica Neue", Arial, sans-serif, '
|
|
|
|
'"Apple Color Emoji", "Segoe UI Emoji", '
|
|
|
|
'"Segoe UI Symbol"',
|
|
|
|
"size": 14,
|
2017-08-24 18:54:01 +00:00
|
|
|
},
|
2022-02-10 00:00:30 +00:00
|
|
|
"margin": {"b": 80, "t": 80},
|
|
|
|
"xaxis": {
|
|
|
|
"titlefont": {"color": "rgba(255, 255, 255, 0.5)"},
|
|
|
|
"gridcolor": "rgba(0, 0, 0, 0.25)",
|
|
|
|
"zerolinecolor": "rgba(0, 0, 0, 0.5)",
|
|
|
|
},
|
|
|
|
"yaxis": {
|
|
|
|
"titlefont": {"color": "rgba(255, 255, 255, 0.5)"},
|
|
|
|
"gridcolor": "rgba(0, 0, 0, 0.25)",
|
|
|
|
"zerolinecolor": "rgba(0, 0, 0, 0.5)",
|
2017-08-24 18:54:01 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-19 17:19:52 +00:00
|
|
|
def rangeselector_date():
|
2017-11-04 03:03:24 +00:00
|
|
|
"""
|
|
|
|
Graph date range selectors settings for 1w, 2w, 1m, 3m, and all.
|
|
|
|
:returns: a dict of settings for the selectors.
|
2017-10-19 17:19:52 +00:00
|
|
|
"""
|
|
|
|
return {
|
2022-02-10 00:00:30 +00:00
|
|
|
"bgcolor": "rgb(35, 149, 86)",
|
|
|
|
"activecolor": "rgb(25, 108, 62)",
|
|
|
|
"buttons": [
|
|
|
|
{"count": 7, "label": "1w", "step": "day", "stepmode": "backward"},
|
|
|
|
{"count": 14, "label": "2w", "step": "day", "stepmode": "backward"},
|
|
|
|
{"count": 1, "label": "1m", "step": "month", "stepmode": "backward"},
|
|
|
|
{"count": 3, "label": "3m", "step": "month", "stepmode": "backward"},
|
|
|
|
{"step": "all"},
|
|
|
|
],
|
2017-10-19 17:19:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-02 23:27:15 +00:00
|
|
|
def rangeselector_time():
|
|
|
|
"""
|
|
|
|
Graph time range selectors settings for 12h, 24h, 48h, 3d and all.
|
|
|
|
:returns: a dict of settings for the selectors.
|
|
|
|
"""
|
|
|
|
return {
|
|
|
|
"bgcolor": "rgb(35, 149, 86)",
|
|
|
|
"activecolor": "rgb(25, 108, 62)",
|
|
|
|
"buttons": [
|
|
|
|
{"count": 12, "label": "12h", "step": "hour", "stepmode": "backward"},
|
|
|
|
{"count": 24, "label": "24h", "step": "hour", "stepmode": "backward"},
|
|
|
|
{"count": 48, "label": "48h", "step": "hour", "stepmode": "backward"},
|
|
|
|
{"count": 3, "label": "3d", "step": "day", "stepmode": "backward"},
|
|
|
|
{"count": 7, "label": "7d", "step": "day", "stepmode": "backward"},
|
|
|
|
{"step": "all"},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
|
2023-01-02 23:35:49 +00:00
|
|
|
|
2017-08-24 18:54:01 +00:00
|
|
|
def split_graph_output(output):
|
2017-11-04 03:03:24 +00:00
|
|
|
"""
|
|
|
|
Split out of a Plotly graph in to html and javascript.
|
|
|
|
:param output: a string of html and javascript comprising the graph.
|
2024-01-28 03:42:12 +00:00
|
|
|
:returns: a tuple of the graph's html and javascript.
|
2017-08-24 18:54:01 +00:00
|
|
|
"""
|
2022-02-10 00:00:30 +00:00
|
|
|
html, js = output.split("<script")
|
|
|
|
js = "<script" + js
|
2019-05-29 16:47:21 +00:00
|
|
|
return html, js
|