2020-01-21 06:35:58 +00:00
|
|
|
from operator import itemgetter
|
|
|
|
|
2022-05-14 17:57:27 +00:00
|
|
|
__all__ = ("chart_colors", "highlight_colors", "get_pie_chart", "get_bar_chart")
|
|
|
|
|
|
|
|
|
|
|
|
chart_colors = [
|
|
|
|
0x3366CC,
|
|
|
|
0xDC3912,
|
|
|
|
0xFF9900,
|
|
|
|
0x109618,
|
|
|
|
0x990099,
|
|
|
|
0x3B3EAC,
|
|
|
|
0x0099C6,
|
|
|
|
0xDD4477,
|
|
|
|
0x66AA00,
|
|
|
|
0xB82E2E,
|
|
|
|
0x316395,
|
|
|
|
0x994499,
|
|
|
|
0x22AA99,
|
|
|
|
0xAAAA11,
|
|
|
|
0x6633CC,
|
|
|
|
0xE67300,
|
|
|
|
0x8B0707,
|
|
|
|
0x329262,
|
|
|
|
0x5574A6,
|
|
|
|
0x3B3EAC,
|
|
|
|
]
|
2020-01-21 06:35:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
highlight_colors = []
|
|
|
|
|
|
|
|
|
|
|
|
def _highlight_colors():
|
|
|
|
for color in chart_colors:
|
|
|
|
r, g, b = color >> 16, (color >> 8) & 0xFF, color & 0xFF
|
2022-05-14 17:57:27 +00:00
|
|
|
highlight_colors.append(
|
|
|
|
"#%02X%02X%02X"
|
|
|
|
% (min(int(r * 1.2), 255), min(int(g * 1.2), 255), min(int(b * 1.2), 255))
|
|
|
|
)
|
2020-01-21 06:35:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
_highlight_colors()
|
|
|
|
|
|
|
|
|
2022-05-14 17:57:27 +00:00
|
|
|
chart_colors = list(map("#%06X".__mod__, chart_colors))
|
2020-01-21 06:35:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_pie_chart(data):
|
|
|
|
return {
|
2022-05-14 17:57:27 +00:00
|
|
|
"labels": list(map(itemgetter(0), data)),
|
|
|
|
"datasets": [
|
2020-01-21 06:35:58 +00:00
|
|
|
{
|
2022-05-14 17:57:27 +00:00
|
|
|
"backgroundColor": chart_colors,
|
|
|
|
"highlightBackgroundColor": highlight_colors,
|
|
|
|
"data": list(map(itemgetter(1), data)),
|
2020-01-21 06:35:58 +00:00
|
|
|
},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def get_bar_chart(data, **kwargs):
|
|
|
|
return {
|
2022-05-14 17:57:27 +00:00
|
|
|
"labels": list(map(itemgetter(0), data)),
|
|
|
|
"datasets": [
|
2020-01-21 06:35:58 +00:00
|
|
|
{
|
2022-05-14 17:57:27 +00:00
|
|
|
"backgroundColor": kwargs.get("fillColor", "rgba(151,187,205,0.5)"),
|
|
|
|
"borderColor": kwargs.get("strokeColor", "rgba(151,187,205,0.8)"),
|
|
|
|
"borderWidth": 1,
|
|
|
|
"hoverBackgroundColor": kwargs.get(
|
|
|
|
"highlightFill", "rgba(151,187,205,0.75)"
|
|
|
|
),
|
|
|
|
"hoverBorderColor": kwargs.get(
|
|
|
|
"highlightStroke", "rgba(151,187,205,1)"
|
|
|
|
),
|
|
|
|
"data": list(map(itemgetter(1), data)),
|
2020-01-21 06:35:58 +00:00
|
|
|
},
|
|
|
|
],
|
|
|
|
}
|
2021-06-02 00:20:39 +00:00
|
|
|
|
2022-05-14 17:57:27 +00:00
|
|
|
|
2021-06-02 00:20:39 +00:00
|
|
|
def get_histogram(data, **kwargs):
|
|
|
|
return {
|
2022-05-14 17:57:27 +00:00
|
|
|
"labels": [round(i, 1) for i in list(map(itemgetter(0), data))],
|
|
|
|
"datasets": [
|
2021-06-02 00:20:39 +00:00
|
|
|
{
|
2022-05-14 17:57:27 +00:00
|
|
|
"backgroundColor": kwargs.get("fillColor", "rgba(151,187,205,0.5)"),
|
|
|
|
"borderColor": kwargs.get("strokeColor", "rgba(151,187,205,0.8)"),
|
|
|
|
"borderWidth": 1,
|
|
|
|
"hoverBackgroundColor": kwargs.get(
|
|
|
|
"highlightFill", "rgba(151,187,205,0.75)"
|
|
|
|
),
|
|
|
|
"hoverBorderColor": kwargs.get(
|
|
|
|
"highlightStroke", "rgba(151,187,205,1)"
|
|
|
|
),
|
|
|
|
"data": list(map(itemgetter(1), data)),
|
2021-06-02 00:20:39 +00:00
|
|
|
},
|
|
|
|
],
|
2022-05-14 17:57:27 +00:00
|
|
|
}
|