Added comments

parent 6ea6a7a9
from datetime import time
from typing import List
from colour import Color
......@@ -7,14 +8,12 @@ from svgwrite.gradients import LinearGradient
from svgwrite.shapes import Line, Rect
from svgwrite.text import Text
from bitsd.server.presence_graph.parameters import START_TIME, END_TIME
from bitsd.server.presence_graph.parameters import START_TIME, END_TIME, COLOR_CLOSE, COLOR_OPEN
HEIGHT = 1000
WIDTH = 1000
HEADER_SIZE = 70
COLOR_OPEN = Color("#387002")
COLOR_CLOSE = Color("#d32f2f")
GRADIENT_NUMBER_COLORS = 11
......@@ -22,8 +21,10 @@ class PresenceGraph(Drawing):
def __init__(self, data: List[List[float]], data_header: List[float] = None, *args, **kwargs):
super().__init__(profile='full', debug=True, *args, **kwargs)
# Set the viewbox instead of the size
self.viewbox(width=WIDTH, height=HEIGHT)
# Save the data
self._data = data
self._data_header = data_header
......@@ -31,6 +32,8 @@ class PresenceGraph(Drawing):
self._gradient = list(COLOR_CLOSE.range_to(COLOR_OPEN, GRADIENT_NUMBER_COLORS))
def generate(self) -> None:
"""Generates the graph with the provided data"""
self._mk_gradients()
self._mk_header_style()
......@@ -52,6 +55,8 @@ class PresenceGraph(Drawing):
self.defs.add(Style("#day_{} {{fill: {};}}".format(i, self._gradient[value].hex)))
def _mk_gradients(self) -> None:
"""Creates the gradient for the graph"""
# For each day of the week
for i in range(0, 5):
# Make a vertical linear gradient
......@@ -75,61 +80,79 @@ class PresenceGraph(Drawing):
lg.add_stop_color(position, color)
def _add_rect(self) -> None:
g = self.add(Group(id="rects"))
"""Adds the colored block to the graph"""
# Create a group for the blocks of the graph
g = self.add(Group())
# Add the blocks of the graph and link them to the gradients
for i in range(0, 5):
g.add(Rect((i * WIDTH / 5, HEADER_SIZE), (WIDTH / 5, HEIGHT), fill="url(#grad_{})".format(i)))
def _add_grid(self) -> None:
"""Adds the grid to the svg"""
# Create a group for the grid
g = self.add(Group(id="grid",
stroke="black",
stroke_width="5"
))
# Vertical lines
# Add the vertical lines for the grid
for i in range(1, 5):
g.add(Line((i * WIDTH / 5, 0), (i * WIDTH / 5, HEIGHT)))
# Header line
# Add the line of the header
g.add(Line((0, HEADER_SIZE), (WIDTH, HEADER_SIZE)))
g = self.add(Group(id="hours_grid",
stroke="black",
stroke_width="2"
))
def _add_hours(self) -> None:
"""Adds the hours labels and the lines to the svg"""
for i in [j*930/13 for j in range(1,13)]:
g.add(Line((0, HEADER_SIZE + i),
(WIDTH/2 - 50, HEADER_SIZE + i)))
g.add(Line((WIDTH/2 + 50, HEADER_SIZE + i),
(WIDTH, HEADER_SIZE + i)))
# Create a group for the lines of the hours
gl = self.add(Group(id="hours_lines",
stroke="black",
stroke_width="2"
))
def _add_hours(self) -> None:
# Create a group for the labels of the hours
g = self.add(Group(id="hours",
text_anchor="middle",
font_size="20",
font_family="Sans",
font_size="25",
font_family="'Open Sans', Sans",
font_weight="bold"
))
#g.add(Text(START_TIME.strftime("%H:%M"), x=[WIDTH / 2], y=[HEADER_SIZE], dy=[30]))
#g.add(Text(END_TIME.strftime("%H:%M"), x=[WIDTH / 2], y=[HEIGHT], dy=[-10]))
number_hours = END_TIME.hour - START_TIME.hour
# Calculate the vertical position for the labels
for i, y in enumerate([j * (HEIGHT - HEADER_SIZE) / number_hours for j in range(1, number_hours)]):
# Create the lines for the hours
gl.add(Line((0, HEADER_SIZE + y),
(WIDTH / 2 - 50, HEADER_SIZE + y)))
gl.add(Line((WIDTH / 2 + 50, HEADER_SIZE + y),
(WIDTH, HEADER_SIZE + y)))
for i, y in enumerate([j * 930 / 13 for j in range(1, 13)]):
g.add(Text("{}:00".format(i+9), x=[WIDTH / 2], y=[HEADER_SIZE + y], dy=[7]))
# Create the labels of the hours
g.add(Text(time(hour=(i + START_TIME.hour + 1)).strftime("%H:%M"),
x=[WIDTH / 2], y=[HEADER_SIZE + y], dy=[9]))
def _add_header(self) -> None:
"""Adds the colored header blocks and their labels"""
# Create a group for the colored header blocks
g = self.add(Group(id="header"))
# Create the header blocks
for i in range(5):
g.add(Rect((i * WIDTH / 5, 0), (WIDTH / 5, HEADER_SIZE), id="day_{}".format(i)))
# Create a group for the labels of the header
g = self.add(Group(id="days",
text_anchor="middle",
font_size="30",
font_family="Sans",
font_family="'Open Sans', Sans",
font_weight="bold"
))
for i, day in enumerate(["Mon.", "Tue.", "Wed.", "Thu.", "Fri."]):
# Create the days labels
for i, day in enumerate(["Mon", "Tue", "Wed", "Thu", "Fri"]):
g.add(Text(day, x=[(i * 2 + 1) * WIDTH / 10], y=[HEADER_SIZE / 2], dy=[13]))
from datetime import timedelta, time
# Time of the start of the day
from colour import Color
START_TIME = time(hour=8)
# Time of the end of the day
END_TIME = time(hour=21)
......@@ -16,4 +18,8 @@ WINDOW_WIDTH = timedelta(weeks=8)
DATA_WEIGHTS = [1000, 300, 200, 150, 100, 70, 30, 20]
# The valid in time for the cache of the graph
GRAPH_CACHE_DURATION = timedelta(hours=6)
\ No newline at end of file
GRAPH_CACHE_DURATION = timedelta(hours=6)
# The color for the graph
COLOR_OPEN = Color("#387002")
COLOR_CLOSE = Color("#d32f2f")
\ 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