-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstatechanges.html
More file actions
142 lines (135 loc) · 4 KB
/
Copy pathstatechanges.html
File metadata and controls
142 lines (135 loc) · 4 KB
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
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="-1" />
<style>
table {
padding-bottom: 20px;
}
th {
font-weight: bold;
text-align: center;
}
td {
width: 30px;
height: 30px;
font-size: 69%;
text-align: right;
padding: 2px;
text-align: center;
}
</style>
</head>
<body>
<div id="json_div"></div>
<script type="text/javascript">
"use strict";
function handleTimePeriod(element, timePeriod, description, showNow) {
const xhr = new XMLHttpRequest();
const url = 'statechanges.php?' + timePeriod;
xhr.open('GET', url);
xhr.responseType = 'json';
xhr.onload = function(event) {
const states = event.target.response;
const table = document.createElement('table');
addHead(table, description);
addBody(table, states, showNow);
element.append(table);
};
xhr.send();
}
function addHead(table, description) {
const head = table.createTHead();
const rowDescription = head.insertRow();
const thDescription = document.createElement('th');
thDescription.colSpan = 26;
rowDescription.appendChild(thDescription);
thDescription.append(description);
const rowHours = head.insertRow();
const thHours = document.createElement('th');
rowHours.appendChild(thHours);
const hours = Array.from(Array(24).keys());
hours.push('avg');
for (const hour of hours) {
const thHour = document.createElement('th');
const text = hour.toString().padStart(2, '0');
thHour.append(text);
rowHours.appendChild(thHour);
}
}
function addBody(table, states, showNow) {
const days = [
'',
'Mon', 'Tue', 'Wed', 'Thu', 'Fri',
'Sat', 'Sun',
'Avg'
]
const timeZone = 'Europe/Amsterdam';
const date = new Date(new Date().toLocaleString(
'en-US', { 'timeZone': timeZone }
));
const dateDay = date.getDay();
const dateHour = date.getHours();
for (const dayNumber in states) {
const row = table.insertRow();
const th = document.createElement('th');
th.append(days[dayNumber]);
row.appendChild(th);
for(const hourNumber in states[dayNumber]) {
const value = states[dayNumber][hourNumber];
const percentage = Math.round(value * 100);
const now = showNow && (
(dayNumber <= 7 ) &&
(dateDay == (dayNumber % 7)) &&
(dateHour == hourNumber )
);
cell(row, percentage, now);
}
}
}
function cell(row, percentage, now) {
const cell = row.insertCell();
const text = percentage;
cell.append(text);
if (now) {
//cell.style.fontWeight = 'bold';
cell.style.textAlign = 'center';
cell.style.border = '2px solid black';
cell.style.padding = '0px';
}
let red = 0, green = 0;
if (percentage > 50) {
red = 255 - (percentage - 50) * 5.12;
green = 255;
}
else {
red = 255;
green = percentage * 5.12;
}
red = parseInt(red);
green = parseInt(green);
cell.style.backgroundColor =
'rgba(' + red + ',' + green + ',0,0.6)'
;
return cell
}
const timePeriods = {
'1week' : "Last week's space state",
'1month' : "Last month's space state average",
'3month' : "Last quarter's space state average",
'1year' : "Last year's space state average",
'2year' : "Two year's space state average"
};
let showNow = true;
const div = document.getElementById('json_div');
for (const [timePeriod, description] of Object.entries(timePeriods)) {
const element = document.createElement('div');
div.append(element);
handleTimePeriod(element, timePeriod, description, showNow);
showNow = false;
}
</script>
</body>
</html>