-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgoalsPR.js
161 lines (148 loc) · 4.02 KB
/
goalsPR.js
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
var graph = 0;
function PR_leftButtonClicked() {
if (graph == 0) {
$("#sleepPlot").show();
$("#habitPlot").hide();
graph = 1;
} else {
$("#sleepPlot").hide();
$("#habitPlot").show();
graph = 0
}
}
function PR_rightButtonClicked() {
if (graph == 0) {
$("#sleepPlot").show();
$("#habitPlot").hide();
graph = 1;
} else {
$("#sleepPlot").hide();
$("#habitPlot").show();
graph = 0
}
}
function initGoalsPR() {
plotSleep();
plotRadar();
$("#sleepPlot").hide();
$("#habitPlot").show();
}
function plotSleep() {
var currentDay = new Date().getDate();
var dayOfTheWeek = new Date().getDay();
var sundayDay = currentDay - dayOfTheWeek;
var ctx = $("#sleepPlot");
var sleepArr = getWeekSleepTime(sundayDay);
var colourArr = getColourArr(sleepArr, 9, 7, 0);
var hoverColourArr = getColourArr(sleepArr, 9, 7, 50)
var myBarChart = new Chart(ctx, {
type: 'bar',
data: {
"labels": ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
"datasets": [{
"data": sleepArr,
"backgroundColor": colourArr,
"hoverBackgroundColor": hoverColourArr
}]
},
options: {
legend: {
display: false
},
scales: {
yAxes: [{
ticks: {
beginAtZero: true,
min: 0,
max:24
}
}]
}
}
});
}
function plotRadar() {
var ctx = $("#habitPlot");
var sleptCount = 0,
screenOffCount = 0,
drankTeaCount = 0;
for (var day of days) {
if (day.sleepTime > 6 && day.sleepTime < 12) {
sleptCount++;
}
if (day.didntLookAtScreen) {
screenOffCount++;
}
if (day.drunk) {
drankTeaCount++;
}
}
var myRadarChart = new Chart(ctx, {
type: 'radar',
data: {
"labels": ["Slept well", "Screen off before bed", "Drank tea"],
"datasets": [{
"data": [sleptCount, screenOffCount, drankTeaCount],
"backgroundColor": "rgba(100,50,200,0.6)",
"hoverBackgroundColor": "rgba(100,100,100,0.5)"
}]
},
options: {
labels: {
fontSize: 50
},
legend: {
display: false
},
scale: {
ticks: {
beginAtZero: true,
min: 0,
max: 30,
fontSize: 16
},
pointLabels: {
fontSize: 20
}
}
}
});
myRadarChart.options.elements.line.tension = 0.05;
myRadarChart.update()
ctx.prop("hidden", true);
}
function getWeekSleepTime(sundayDay) {
var sleepTimes = [];
for (var i = 0; i < 7; i++) {
sleepTimes.push(days[sundayDay + i].sleepTime);
}
return sleepTimes;
}
function getColourArr(data, optimal, max, darkness) {
var colourData = [];
for (var i = 0; i < data.length; i++) {
var tc = getGTR(constrain(optimal - data[i], 0, max) / max, darkness); //value from 0 to 1, 0 is perfect sleep
colourData.push("rgba(" + tc.r + "," + tc.g + "," + tc.b + "," + tc.a + ")");
}
return colourData;
}
function getGTR(failure, darkness) {
var netSuccess = 400 - failure * 400; // 0 is bad 400 is good
var colour = {
r: 200,
g: 200,
b: 0,
a: 0.4
}
if (netSuccess > 200) {
colour.r = 400 - netSuccess;
} else {
colour.g = netSuccess;
}
colour.r -= darkness;
colour.g -= darkness;
return colour;
}
function constrain(num, min, max) {
return Math.min(Math.max(parseInt(num), min), max);
}