-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
483 lines (411 loc) · 15.3 KB
/
app.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
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
var cvfGraphOptions = {
layout: {
padding: {
bottom: -20,
left: -20,
top: 50
}
},
responsive: true,
maintainAspectRatio: false,
tooltips: {
mode: 'index',
enabled: false,
intersect: false,
custom: function (model) {
document.getElementById(this._chart.canvas.dataset.chartTooltip).innerHTML = model.title + '<br>' + numberWithSpaces(model.dataPoints[0].yLabel) + ' tartuntaa';
}
},
hover: {
mode: 'index',
intersect: false
},
scales: {
yAxes: [{
gridLines: {
display: false
},
ticks: {
display: false
}
}],
xAxes: [{
gridLines: {
display: false
},
ticks: {
display: false
}
}]
},
elements: {
point: {
radius: 0
}
},
legend: {
display: false
}
};
Chart.defaults.LineWithLine = Chart.defaults.line;
Chart.controllers.LineWithLine = Chart.controllers.line.extend({
draw: function (ease) {
Chart.controllers.line.prototype.draw.call(this, ease);
if (this.chart.tooltip._active && this.chart.tooltip._active.length) {
var activePoint = this.chart.tooltip._active[0],
ctx = this.chart.ctx,
x = activePoint.tooltipPosition().x,
topY = this.chart.scales['y-axis-0'].top - 50,
bottomY = this.chart.scales['y-axis-0'].bottom;
//console.log(this.chart.canvas);
document.getElementById(this.chart.canvas.dataset.chartTooltip).classList.remove('animated', 'fadeOutDown');
document.getElementById(this.chart.canvas.dataset.chartTooltip).style.display = 'block';
document.getElementById(this.chart.canvas.dataset.chartTooltip).classList.add('animated', 'fadeInUp');
// draw line
ctx.save();
ctx.beginPath();
ctx.setLineDash([2]);
ctx.moveTo(x, topY);
ctx.lineTo(x, bottomY);
ctx.lineWidth = 2;
ctx.strokeStyle = '#2D5520';
ctx.stroke();
ctx.restore();
}
}
});
var c1 = document.getElementById('canvas1');
var c2 = document.getElementById('canvas2');
var ctx = c1.getContext('2d');
var ctx2 = c2.getContext('2d');
var graphTooltip1 = document.getElementById('graphTooltip1');
var graphTooltip2 = document.getElementById('graphTooltip2');
function clearTooltips() {
graphTooltip1.classList.remove('animated', 'fadeInUp');
graphTooltip2.classList.remove('animated', 'fadeInUp');
graphTooltip1.classList.add('animated', 'fadeOutDown');
graphTooltip2.classList.add('animated', 'fadeOutDown');
};
c1.onmouseleave = clearTooltips;
c1.ontouchleave = clearTooltips;
c2.onmouseleave = clearTooltips;
c2.ontouchleave = clearTooltips;
function getLastUpdated() {
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://api.github.com/repos/ahnl/coronavirus-finland/commits?path=data/regional.csv&page=1&per_page=1', true);
xhr.responseType = 'json';
xhr.onload = function () {
var status = xhr.status;
if (status === 200) {
//document.getElementById('lastUpdated').innerHTML = 'Tilastot päivitetty viimeeksi ' + timeago.format(new Date(xhr.response[0].commit.author.date), 'fi');
document.getElementById('lastUpdated').innerHTML = 'Tilastot päivitetty viimeeksi ' + timeago.format(new Date("2021-12-31T08:34:25Z"), 'fi');
}
};
xhr.send();
}
getLastUpdated();
function dataFromCsv(url, type, columnShift, callback) {
// type = false : chart dataset and clean dates
// type = true : object
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.onload = function () {
var status = xhr.status;
if (status === 200) {
var rows = xhr.response.trim().split('\n');
if (type) {
var data = {
labels: [],
data: []
};
} else {
var data = {};
}
for (var i = 0; i < rows.length; i++) {
try {
if (rows[i].includes(',')) {
var row = rows[i].split(',');
} else {
var row = rows[i].split(';');
}
if (type) {
var date = new Date(row[0 + columnShift]);
var cleanDate = date.getDate() + '.' + (date.getMonth() + 1) + '.';
data.labels.push(cleanDate);
data.data.push(row[1 + columnShift]);
} else {
data[row[0 + columnShift]] = row[1 + columnShift];
}
} catch (e) {
console.log('dataFromCsv invalid row ' + i)
}
}
callback(null, data);
} else {
callback(status, xhr.response);
}
};
xhr.send();
}
var eastereggCounter = 0;
function eastereggClick() {
eastereggCounter += 1;
if (eastereggCounter == 10) {
document.body.classList.add('rainbowBackground');
document.getElementById("eastereggSound").play();
}
}
var tabSelector = document.getElementById('tabSelector');
var tabA = document.getElementById('tabA');
var tabB = document.getElementById('tabB');
var tabContentA = document.getElementById('tabContentA');
var tabContentB = document.getElementById('tabContentB');
tabContentA.addEventListener('animationend', removeTabAnimations);
tabContentB.addEventListener('animationend', removeTabAnimations);
function removeTabAnimations() {
tabContentA.classList.remove('animated', 'bounce');
tabContentB.classList.remove('animated', 'bounce');
}
function toggleTab() {
tabContentA.classList.add('animated', 'bounce');
tabContentB.classList.add('animated', 'bounce');
if (tabSelector.dataset.selected == 'A') {
tabSelector.dataset.selected = 'B';
tabSelector.style.left = '50%';
tabA.classList.remove('selectedTab');
tabB.classList.add('selectedTab');
tabContentA.style.display = 'none';
tabContentB.style.display = 'block';
} else {
tabSelector.dataset.selected = 'A';
tabSelector.style.left = '0';
tabB.classList.remove('selectedTab');
tabA.classList.add('selectedTab');
tabContentB.style.display = 'none';
tabContentA.style.display = 'block';
}
}
// https://stackoverflow.com/questions/5560248/programmatically-lighten-or-darken-a-hex-color-or-rgb-and-blend-colors
function shadeColor(color, percent) {
var R = parseInt(color.substring(1, 3), 16);
var G = parseInt(color.substring(3, 5), 16);
var B = parseInt(color.substring(5, 7), 16);
R = parseInt(R * (100 + percent) / 100);
G = parseInt(G * (100 + percent) / 100);
B = parseInt(B * (100 + percent) / 100);
R = (R < 255) ? R : 255;
G = (G < 255) ? G : 255;
B = (B < 255) ? B : 255;
var RR = ((R.toString(16).length == 1) ? "0" + R.toString(16) : R.toString(16));
var GG = ((G.toString(16).length == 1) ? "0" + G.toString(16) : G.toString(16));
var BB = ((B.toString(16).length == 1) ? "0" + B.toString(16) : B.toString(16));
return "#" + RR + GG + BB;
}
var regionNames = {
'uusimaa': 'Uusimaa',
'varsinais-suomi': 'Varsinais-Suomi',
'etela-karjala': 'Etelä-Karjala',
'lappi': 'Lappi',
'pohjois-pohjanmaa': 'Pohjois-Pohjanmaa',
'kainuu': 'Kainuu',
'pohjois-karjala': 'Pohjois-Karjala',
'etela-savo': 'Etelä-Savo',
'pohjois-savo': 'Pohjois-Savo',
'kanta-hame': 'Kanta-Häme',
'paijat-hame': 'Päijät-Häme',
'ahvenanmaa': 'Ahvenanmaa',
'etela-pohjanmaa': 'Etelä-Pohjanmaa',
'keski-pohjanmaa': 'Keski-Pohjanmaa',
'keski-suomi': 'Keski-Suomi',
'pirkanmaa': 'Pirkanmaa',
'satakunta': 'Satakunta',
'kymenlaakso': 'Kymenlaakso',
'pohjanmaa': 'Pohjanmaa'
};
document.getElementById('mapTooltipValue').addEventListener('animationend', function () {
document.getElementById('mapTooltipValue').classList.remove('animated', 'fadeInUp');
document.getElementById('mapTooltipArea').classList.remove('animated', 'fadeInUp');
});
function mapAreaTooltipAnimations() {
document.getElementById('mapTooltipValue').classList.add('animated', 'fadeInUp');
document.getElementById('mapTooltipArea').classList.add('animated', 'fadeInUp');
}
function mapArea(property) {
if (document.getElementById('mapTooltipArea').innerHTML != regionNames[property]) {
mapAreaTooltipAnimations();
}
document.getElementById('mapTooltipValue').innerHTML = numberWithSpaces(regionData[property]) + ' tartuntaa';
document.getElementById('mapTooltipArea').innerHTML = regionNames[property];
for (var regionName in regionNames) {
if (regionNames.hasOwnProperty(regionName)) {
if (document.getElementById('smap_' + regionName).getAttribute('fill') != undefined) {
document.getElementById('smap_' + regionName).style.opacity = '0.5';
}
}
}
document.getElementById('smap_' + property).style.opacity = '1';
}
document.getElementById('mapSvgContainer').onmouseout = function () {
for (var regionName in regionNames) {
if (regionNames.hasOwnProperty(regionName)) {
document.getElementById('smap_' + regionName).style.opacity = '1';
}
}
if (document.getElementById('mapTooltipArea').innerHTML != 'Koko suomi') {
mapAreaTooltipAnimations();
}
document.getElementById('mapTooltipValue').innerHTML = numberWithSpaces(total) + ' tartuntaa';
document.getElementById('mapTooltipArea').innerHTML = 'Koko suomi';
};
function makeChart(canvasContext, label, data) {
new Chart(canvasContext, {
type: 'LineWithLine',
data: {
labels: data.labels,
datasets: [{
label: label,
data: data.data,
borderColor: '#2D5520',
fill: true,
backgroundColor: '#2D5520'
}]
},
options: cvfGraphOptions
});
}
var regionData = {};
function formatRegionData(data) {
const conversions = {
'ahvenanmaa': ['Ahvenanmaa'],
'varsinais-suomi': ['Varsinais-Suomen SHP'],
'uusimaa': ['Helsingin ja Uudenmaan SHP'],
'etela-karjala': ['Etelä-Karjalan SHP'],
'kanta-hame': ['Kanta-Hämeen SHP'],
'satakunta': ['Satakunnan SHP'],
'etela-pohjanmaa': ['Etelä-Pohjanmaan SHP'],
'keski-suomi': ['Keski-Suomen SHP'],
'pohjois-savo': ['Pohjois-Savon SHP'],
'pohjois-karjala': ['Pohjois-Karjalan SHP'],
'pirkanmaa': ['Pirkanmaan SHP'],
'lappi': ['Lapin SHP', 'Länsi-Pohjan SHP'],
'pohjois-pohjanmaa': ['Pohjois-Pohjanmaan SHP'],
'keski-pohjanmaa': ['Keski-Pohjanmaan SHP'],
'pohjanmaa': ['Vaasan SHP'],
'etela-savo': ['Etelä-Savon SHP', 'Itä-Savon SHP'],
'paijat-hame': ['Päijät-Hämeen SHP'],
'kymenlaakso': ['Kymenlaakson SHP'],
'kainuu': ['Kainuun SHP']
}
let newData = {};
for (let shp in data) {
let maakunta = Object.keys(conversions).find(p => conversions[p].includes(shp));
if (maakunta) {
if (newData[maakunta] == undefined) {
newData[maakunta] = 0;
}
newData[maakunta] += parseInt(data[shp]);
} else {
console.log('Unknown shp: ' + shp);
}
}
console.log(newData);
return newData;
}
function numberWithSpaces(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, " ");
}
dataFromCsv('https://raw.githubusercontent.com/ahnl/coronavirus-finland/master/data/regional.csv', false, 0, function (err, data) {
if (!err) {
//data
data = formatRegionData(data);
regionData = data;
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://raw.githubusercontent.com/ahnl/coronavirus-finland/master/suomi.svg', true);
xhr.onload = function () {
var status = xhr.status;
if (status === 200) {
document.getElementById('mapSvgContainer').innerHTML = xhr.response;
for (var property in regionData) {
if (regionData.hasOwnProperty(property)) {
var shade = -(0.004 * regionData[property]);
if (shade < -70) {
shade = -70;
}
console.log(property + ' ' + shade + ' ' + regionData[property]);
document.getElementById('smap_' + property).setAttribute("fill", shadeColor('#2D5520', shade)); // smap_
document.getElementById('smap_' + property).setAttribute('onmousemove', 'mapArea(\'' + property + '\')')
}
}
}
};
xhr.send();
}
});
function makeCumulative(data) {
// data = {data: , labels: }
let cumData = {data: [], labels: []};
let current = 0;
data.labels.forEach((point, index) => {
let value = parseInt(data.data[index]);
current += (value ? value : 0);
//console.log(point, current);
cumData.labels.push(point);
cumData.data.push(current);
});
return cumData;
}
function trimData(data) {
function trimmer(i) {
let value = parseInt(data.data[i]);
value = (value ? value : 0);
if (value > 0) {
return true;
} else {
delete data.labels[i];
delete data.data[i];
return false;
}
}
for (let i = 0; i < data.labels.length; i++) {
if (trimmer(i)) break
}
for (let i = data.labels.length; i--;) {
if (trimmer(i)) break
}
return data;
}
function reindexData(data) {
let newData = {data: [], labels: []};
data.labels.forEach((point, index) => {
let value = parseInt(data.data[index]);
value = (value ? value : 0);
newData.labels.push(point);
newData.data.push(value);
});
return newData;
}
var total = null;
dataFromCsv('https://raw.githubusercontent.com/ahnl/coronavirus-finland/master/data/daily.csv', true, 1, function (err, data) {
if (!err) {
delete data.labels[0];
delete data.data[0];
data = trimData(data);
data = reindexData(data);
console.log(data);
makeChart(ctx, 'Tapaukset', data);
let cumData = makeCumulative(data);
total = cumData.data[cumData.data.length - 1];
document.getElementById('total').innerHTML = numberWithSpaces(total);
document.getElementById('mapTooltipValue').innerHTML = numberWithSpaces(total) + ' tartuntaa';
makeChart(ctx2, 'Tartunnat yhteensä', cumData);
}
});
/*
dataFromCsv('https://raw.githubusercontent.com/ahnl/coronavirus-finland/master/total.csv', true, function (err, data) {
if (!err) {
total = data.data[data.data.length - 1];
document.getElementById('total').innerHTML = total;
document.getElementById('mapTooltipValue').innerHTML = total + ' tartuntaa';
makeChart(ctx2, 'Tartunnat yhteensä', data);
}
});*/