Added class to generate presence graph

parent e465b28d
from typing import List
from colour import Color
from svgwrite import Drawing
from svgwrite.container import Group
from svgwrite.gradients import LinearGradient
from svgwrite.shapes import Line
class PresenceGraph(Drawing):
def __init__(self, data: List[List[float]], *args, **kwargs):
super().__init__(profile='full', debug=True, *args, **kwargs)
self.viewbox(width=100, height=100)
self._data = data
def generate(self) -> None:
self._mk_gradients()
self._add_rect()
self._add_hours()
self._add_grid()
def _mk_gradients(self) -> None:
COLOR_OPEN = Color("green")
COLOR_CLOSE = Color("red")
NUMBER_COLORS = 11
# Calculate the discrete gradient
gradient = list(COLOR_CLOSE.range_to(COLOR_OPEN, NUMBER_COLORS))
# For each day of the week
for i in range(0, 6):
# Make a vertical linear gradient
lg = LinearGradient((0, 0), (0, 1), id="grad_{}".format(i))
# Add the gradient to the svg
self.defs.add(lg)
# For each data for the single day
for j in range(len(self._data[i])):
# Calculate the position in the gradient
position = j / len(self._data[i])
# Calculate the rescaled value on the number of the colors in the gradient
value = round(self._data[i][j] * (NUMBER_COLORS - 1))
# Get the hex value of the computed color
color = gradient[value].hex
# Add the color in the right position of the gradient
lg.add_stop_color(position, color)
def _add_rect(self) -> None:
g = self.add(Group(id="rects"))
for i in range(0, 6):
g.add(self.rect((i * 100 / 6, 0), (100 / 6, 100), fill="url(#grad_{})".format(i)))
def _add_grid(self) -> None:
g = self.add(Group(id="grid", stroke="black"))
for i in range(1, 6):
g.add(Line((i * 100 / 6, 0), (i * 100 / 6, 100)))
def _add_hours(self) -> None:
pass
......@@ -5,3 +5,5 @@ tornado==4.*
paho-mqtt
mysqlclient
psycopg2
svgwrite~=1.3
colour~=0.1
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment