-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyRuns2021.html
396 lines (311 loc) · 7.74 KB
/
MyRuns2021.html
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>My Runs 2021</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@500&display=swap" rel="stylesheet">
<style type="text/css">
/* http://meyerweb.com/eric/tools/css/reset/
v2.0 | 20110126
License: none (public domain)
*/
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure,
footer, header, hgroup, menu, nav, section {
display: block;
}
body {
line-height: 1;
}
ol, ul {
list-style: none;
}
blockquote, q {
quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
content: '';
content: none;
}
table {
border-collapse: collapse;
border-spacing: 0;
}
h1 {
margin: 16px;
font-size: 48px;
}
body {
font-family: 'Roboto', sans-serif;
text-align: center;
}
table {
border-collapse: collapse;
border-spacing: 0;
text-align: center;
margin: 0 auto;
}
table td {
padding: 2px;
color: white;
}
table th {
color: #a5a5a5;
padding: 2px;
}
table tbody th {
padding-right: 8px;
}
table thead th {
padding-bottom: 8px;
}
table th.month {
text-align: left;
}
td.min .cell { background-color:#e81d61; }
td.below .cell { background-color: #00bad3; }
td.normal .cell { background-color: #278ef2; }
td.above .cell { background-color: #8ac249;}
td.max .cell { background-color: #374046; }
td.total { color: #a5a5a5; text-align: right; padding-left: 16px; }
.cell {
height: 36px;
width: 36px;
line-height: 36px;
font-size: 16px;
border-radius: 50%;
}
</style>
</head>
<body>
<script type="text/javascript">
const myData = [
{
year: 2021,
month: 1,
day: 1,
type: "run",
distance: 8.31,
time: "00:53:00",
steps: 9672
},
{
year: 2021,
month: 1,
day: 2,
type: "walk",
distance: 4.23,
time: "00:49:04",
steps: 8601
}
]
const ALL = 'ALL'
const RUN = 'run'
const WALK = 'walk'
const TYPE = {}
TYPE[ALL] = ALL
TYPE[RUN] = RUN
TYPE[WALK] = WALK
const DISTANCE = 'distance'
const TIME = 'time'
const STEPS = 'steps'
const VALUE = {}
VALUE[DISTANCE] = DISTANCE
VALUE[TIME] = TIME
VALUE[STEPS] = STEPS
const UNIT = {}
UNIT[DISTANCE] = 'km'
UNIT[TIME] = ''
UNIT[STEPS] = 'steps'
function timeStringToFloat(time) {
var hoursMinutes = time.split(/[.:]/)
var hours = parseInt(hoursMinutes[0], 10)
var minutes = hoursMinutes[1] ? parseInt(hoursMinutes[1], 10) : 0
var seconds = hoursMinutes[2] ? parseInt(hoursMinutes[2], 10) : 0
return hours + minutes / 60 + seconds / 3600
}
const TO_NUMBER = {}
TO_NUMBER[DISTANCE] = (v) => v
TO_NUMBER[TIME] = timeStringToFloat
TO_NUMBER[STEPS] = (v) => v
const TO_STRING = {}
TO_STRING[DISTANCE] = (v) => Math.round(v)
TO_STRING[TIME] = (v) => Math.round(v)
TO_STRING[STEPS] = (v) => Math.round(v / 1000) + 'k'
const numberOfMonths = 12
const numberOfDays = 31
function prepareData(arr, year, type = ALL, metric = DISTANCE) {
let farr = arr.filter((v) => { return v.year === year } )
if (type !== ALL) {
farr = farr.filter((v) => { return v.type === type } )
}
let data = []
for (let i in farr) {
if (farr[i][metric]) {
let m = farr[i].month
let d = farr[i].day
if(!data[m]) {
data[m] = []
}
if(!data[m][d]) {
data[m][d] = 0
}
data[m][d] += TO_NUMBER[metric](farr[i][metric])
}
}
return data;
}
function daysInMonth (month, year) {
return new Date(year, month, 0).getDate();
}
const year = 2021
const type = ALL
const value = TIME
const data = prepareData(myData, year, type, value)
//adapted from https://blog.poettner.de/2011/06/09/simple-statistics-with-php/
function Median(data) {
return Quartile_50(data);
}
function Quartile_25(data) {
return Quartile(data, 0.25);
}
function Quartile_50(data) {
return Quartile(data, 0.5);
}
function Quartile_75(data) {
return Quartile(data, 0.75);
}
function Quartile(data, q) {
data=Array_Sort_Numbers(data);
var pos = ((data.length) - 1) * q;
var base = Math.floor(pos);
var rest = pos - base;
if( (data[base+1]!==undefined) ) {
return data[base] + rest * (data[base+1] - data[base]);
} else {
return data[base];
}
}
function Array_Sort_Numbers(inputarray){
return inputarray.sort(function(a, b) {
return a - b;
});
}
function Array_Sum(t){
return t.reduce(function(a, b) { return a + b; }, 0);
}
function Array_Average(data) {
return Array_Sum(data) / data.length;
}
function Array_Stdev(tab){
var i,j,total = 0, mean = 0, diffSqredArr = [];
for(i=0;i<tab.length;i+=1){
total+=tab[i];
}
mean = total/tab.length;
for(j=0;j<tab.length;j+=1){
diffSqredArr.push(Math.pow((tab[j]-mean),2));
}
return (Math.sqrt(diffSqredArr.reduce(function(firstEl, nextEl){
return firstEl + nextEl;
})/tab.length));
}
function getStates(data) {
let nums = data.flat()
let min = 0
let q25 = Quartile_25(nums)
let q50 = Quartile_50(nums)
let q75 = Quartile_75(nums)
let max = Math.max.apply(Math, nums)
let pt1 = min
let pt2 = q25 + (q50 - q25) / 2
let pt3 = q50 + (q75 - q50) / 2
let pt4 = max
let states = [
[pt1, pt1, "MIN"],
[pt1, pt2, "BELOW"],
[pt2, pt3, "NORMAL"],
[pt3, Math.floor(pt4), "ABOVE"],
[Math.floor(pt4), pt4, "MAX"]
]
return states
}
let states = getStates(data)
function numberToState(num, states, func = Math.round) {
if (num === null) {
return "EMPTY"
}
for (var i in states) {
let n = func(num)
let min = func(states[i][0])
let max = func(states[i][1])
if (n >= min && n <= max) {
return states[i][2]
}
}
return 'UNKNOWN'
}
const monthsPL = ['S', 'L', 'M', 'K', 'M', 'C', 'L', 'S', 'W', 'P', 'L', 'G']
const monthsPL_SHORT = ['STY', 'LUT', 'MAR', 'KWI', 'MAJ', 'CZE', 'LIP', 'SIE', 'WRZ', 'PAŹ', 'LIS', 'GRU']
const monthsEN = ['J', 'F', 'M', 'A', 'M', 'J', 'J', 'A', 'S', 'O', 'N', 'D']
const months = monthsPL_SHORT
const colors = [];
let uom = UNIT[value]
let rows = '';
for (let m = 1; m <= numberOfMonths; m++) {
rows += `<tr>
<th class="month" scope="row">${months[m-1]}</th>`;
for (let d = 1; d <= numberOfDays; d++) {
let metric = data[m] ? (data[m][d] ? data[m][d] : null ) : null
let state = numberToState(metric, states)
metric = state !== "EMPTY" && state !== "UNKNOWN" ? TO_STRING[value](metric) : 0
rows += `<td class="${state.toLowerCase()}"><div class="cell">${metric}</div></td>`;
}
let total = data[m] ? Math.round(data[m].reduce((a, b) => a + b)) : 0
let totalValue = TO_STRING[value](total)
rows += `<td class="total">${totalValue} ${uom}</td>`;
rows += `</tr>`;
}
let table = `<table>
<thead>
<tr>
<th></th>`
for (let d = 1; d <= numberOfDays; d++) {
table += `<th scope="col">${d}</th>`
}
table += `<th scope="col">Total</th>`
table += `</tr>
</thead>
<tbody>
${rows}
</tbody>
</tabel>`;
let content = `<h1>${year}</h1>${table}`;
document.getElementsByTagName('body')[0].innerHTML += content;
</script>
</body>
</html>