-
Notifications
You must be signed in to change notification settings - Fork 6
/
line_graph.py
84 lines (71 loc) · 2.54 KB
/
line_graph.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# Function to produce a json file for web to display a Plotly line graph that
# maps the history of a specific student's submission scores
# Imports
import plotly.graph_objects as go
def line_graph(score_history, name):
"""
Function produces a line graph of a student's squad scores over time
Input: A list containing the history of the students scores, in
chronological order. And student's name.
Output: Plotly JSON for web to display using plotly.js on the
parent dashboard
"""
# Plotly line graph to show improvement over time
fig = go.Figure(
data=go.Scatter(
x=[i + 1 for i in range(len(score_history))],
y=score_history,
line_width=7,
line_color="#EB7E5B",
mode="lines+markers+text",
marker_size=18,
marker_color="#FED23E",
marker_symbol="star",
)
)
fig.update_layout(
title={
"text": f"{name}'s Squad Score Over Time",
"y": 0.95,
"x": 0.5,
"font": {"size": 25, "family": "PT Sans Narrow"},
},
plot_bgcolor="#6CEAE6",
)
fig.update_traces(hoverinfo="none", hovertemplate=None)
fig.update_xaxes(
title_text="Week Number",
title_font={"size": 20, "family": "PT Sans Narrow"},
showgrid=False,
zeroline=False,
ticks="inside",
tickvals=[i + 1 for i in range(len(score_history))],
)
fig.update_yaxes(
title_text="Squad Score",
title_font={"size": 20, "family": "PT Sans Narrow"},
showticklabels=False,
showgrid=False,
)
# If there is only 1 submission, it will only show a single data point
# Adds a sentence to the parent to check back when more data is available
if len(score_history) == 1:
fig.update_layout(
annotations=[
dict(
xref="paper",
yref="paper",
x=0.5,
y=1.15,
text="Your child only has one submission so far. Please check back next week to view their progress!",
showarrow=False,
font=dict(size=18, family="PT Sans Narrow"),
)
]
),
# If there are zero submissions
# Web will not hit our API if there are no submissions, but just in case
if len(score_history) == 0:
return "No Submissions for This User"
# Return as json for web to use in plotly.js
return fig.to_json()