-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathowid.html
486 lines (409 loc) · 14.2 KB
/
owid.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
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
484
485
486
<html>
<head>
<title>OWID Covid data</title>
<meta charset=utf-8 />
<meta http-equiv=“Pragma” content=”no-cache”>
<meta http-equiv=“Expires” content=”-1″>
<meta http-equiv=“CACHE-CONTROL” content=”NO-CACHE”>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="keywords" content="covid,statistics,world">
<meta name="author" content="Willem Hengeveld, [email protected]">
<meta name="description" content="show OurWorldInData covid information.">
<style>
#controlsdiv {
position:fixed;
right:0px;
top:100px;
width:120px;
}
#tablediv {
position:absolute;
top:140px;
left:0px;
right:140px;
}
</style>
<script src="filter.js" ></script>
<script src="plot.js" ></script>
<script src="jsutils.js" ></script>
<script language=javascript><!--
const OWIDURL = "https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/owid-covid-data.csv";
var mincases = 50;
var minpop = 500000;
class Filter {
constructor()
{
this.cutoff = 1/7;
this.bvalue = 0;
this.filt = null;
this.updfilt();
}
updfilt()
{
// update the global filter with the new cutoff and b values.
if (this.cutoff==0) {
// no filter
this.filt = null;
}
else if (this.bvalue==0) {
// smoothing
this.filt = new Smoothing(Math.round(1/this.cutoff));
}
else {
this.filt = new LowPass(this.cutoff, this.bvalue);
}
}
hasfilter()
{
return this.cutoff != 0;
}
calc(vec)
{
return this.filt.calc(vec);
}
setcutoff(t)
{
t = Number(t);
this.cutoff = t ? 1/t : 0;
this.updfilt();
}
setdepth(t)
{
t = Number(t);
this.bvalue = t ? 4/t : 0;
this.updfilt();
}
getcutofftext()
{
return this.cutoff ? (1/this.cutoff).toFixed(0)+" days" : "off"
}
getdepthtext()
{
return this.bvalue ? (4/this.bvalue).toFixed(0) : "smoothing"
}
getcutoffvalue()
{
return this.cutoff ? 1/this.cutoff : 0;
}
getdepthvalue()
{
return this.bvalue ? 4/this.bvalue : 0;
}
};
var filt = new Filter();
var g_graphvalues;
var g_rdaysa = 7;
var g_rdaysb = 14;
function hidedetailgraph()
{
var canvas = document.getElementById("graphdiv");
canvas.style.visibility = 'hidden';
var tbl = document.getElementById('tablediv');
tbl.style.visibility = 'visible';
}
function showdetailgraph()
{
var gdiv = document.getElementById("graphdiv");
gdiv.style.visibility = 'visible';
var tdiv = document.getElementById('tablediv');
tdiv.style.visibility = 'hidden';
gdiv.style.position = "absolute";
gdiv.style.top = window.scrollY;
}
function updatedetailgraph()
{
var canvas = document.getElementById("detailgraph");
var plot = new Plot(canvas);
for (var i=0 ; i<7 ; i++)
{
var stepped = [];
for (var x=i ; x<g_graphvalues.length ; x+=7)
stepped.push([x, g_graphvalues[x]]);
plot.setgraph(i, stepped);
}
plot.setgraph(7, g_graphvalues);
if (filt.hasfilter()) {
var values = filt.calc(g_graphvalues);
plot.setgraph(8, values);
}
plot.rescale();
plot.display();
}
function makestepgraph(values)
{
colors = [
"#b22727", "#b29727", "#5fb327", "#2797b3", "#2727b3", "#9727b3", "#b2275f"
];
// create a graph for the list of values, optionally applying the filter.
var td = el('td');
var canvas = el('canvas');
td.append(canvas);
var cvx = (x) => 2*x;
canvas.width = cvx(values.length);
canvas.height = 40;
var ymax = Math.max(...values);
var ymin = Math.min(...values);
values = values.map( y => (y-ymin)/(ymax-ymin) );
var cvy = (y) => (canvas.height-5-y*(canvas.height-10));
var ctx = canvas.getContext("2d");
for (var i=0 ; i<7 ; i++) {
ctx.strokeStyle = colors[i];
ctx.fillStyle = colors[i];
ctx.beginPath();
ctx.moveTo(cvx(i), cvy(values[i]));
for (var x=i+7 ; x<values.length ; x+=7)
ctx.lineTo(cvx(x), cvy(values[x]));
ctx.stroke();
}
return td;
}
function makegraph(values)
{
// create a graph for the list of values, optionally applying the filter.
var td = el('td');
var canvas = el('canvas');
td.append(canvas);
var origvalues = [...values];
canvas.addEventListener('click', (() => { g_graphvalues = origvalues; showdetailgraph(); update(); } ))
var cvx = (x) => 2*x;
canvas.width = cvx(values.length);
canvas.height = 40;
var ofs = 0;
if (filt.hasfilter()) {
values = filt.calc(values);
if (filt.bvalue && filt.cutoff) {
values = values.slice(Math.ceil(1/filt.bvalue), -Math.ceil(1/filt.bvalue))
ofs = Math.ceil(1/filt.bvalue);
cvx = (x) => 2*(x + ofs);
}
}
var ymax = Math.max(...values);
var ymin = Math.min(...values);
values = values.map( y => (y-ymin)/(ymax-ymin) );
var cvy = (y) => (canvas.height-5-y*(canvas.height-10));
var ctx = canvas.getContext("2d");
ctx.beginPath();
ctx.moveTo(cvx(0), cvy(values[0]));
for (var x=1 ; x<values.length ; x++)
ctx.lineTo(cvx(x), cvy(values[x]));
ctx.stroke();
return td;
}
function calcdifferences(table)
{
// calculate the difference between entries in the table.
var res = [];
var prev;
for (var ent of table)
{
if (prev)
res.push({
gevallen:ent.gevallen-prev.gevallen,
overleden:ent.overleden-prev.overleden,
ziekenhuis:ent.ziekenhuis-prev.ziekenhuis,
inwoners:ent.inwoners,
});
prev = ent;
}
return res;
}
function makerow(name, data)
{
// create a row in the table
var tr = el('tr');
tr.append(el('td', name));
var table = Object.entries(data).sort().map( ([k,v])=>v );
var diff = calcdifferences(table);
var today = table[table.length-1];
var fourdays = table[table.length-1-g_rdaysa];
var lastweek = table[table.length-8];
var last14 = table[table.length-1-g_rdaysb];
tr.append(makenumtd(today.inwoners));
if (today.inwoners < minpop)
return;
tr.append(makenumtd(today.gevallen/today.inwoners*100000, 1));
tr.append(makenumtd(today.overleden/today.inwoners*100000, 1));
var weekincidence = today && lastweek ? (today.gevallen/today.inwoners-lastweek.gevallen/lastweek.inwoners)*100000 : 0;
tr.append(makenumtd(weekincidence, 1));
if (weekincidence < mincases)
return;
var rval = last14 ? (today.gevallen-fourdays.gevallen)/(today.gevallen-last14.gevallen)*g_rdaysb/g_rdaysa : 0;
tr.append(makenumtd(rval, 2));
tr.append(makegraph(diff.slice(-90, -1).map( e => e.gevallen )));
var weekincidence = (today.overleden/today.inwoners-lastweek.overleden/lastweek.inwoners)*100000;
tr.append(makenumtd(weekincidence, 1));
tr.append(makegraph(diff.slice(-90, -1).map( e => e.overleden )));
tr.append(makestepgraph(diff.slice(-90).map( e => e.gevallen )));
tr.append(makestepgraph(diff.slice(-90).map( e => e.overleden )));
return tr;
}
function createheader(th)
{
// create the table header row
var headers = [ "Country", "population", "total/100k", "deaths/100k", "cases<br>/week/100k", "rval", "casegraph", "deaths<br>/week/100k", "deathsgraph", "dayofweek" ];
var tr = th.insertRow(0);
for (var h of headers) {
var c = el('th');
c.innerHTML = h;
tr.appendChild(c);
}
}
function makesliders()
{
// create the sliders for the 'controlsdiv'
return [
makeslider("smoothing:<br>", "cutoff", 0, 20, filt.getcutoffvalue(), t => filt.setcutoff(t), () => filt.getcutofftext()),
makeslider("depth:", "depth", 0, 100, filt.getdepthvalue(), t => filt.setdepth(t), () => filt.getdepthtext()),
makeslider("mincases:", "mincases", 0, 800, 50, t => mincases = Number(t), () => mincases.toFixed(0)),
makeslider("minpop:", "minpop", 0, 1000, 50, t => minpop = Number(t)*100000, () => (minpop/1000000).toFixed(2)+"M"),
makeslider("r1:", "r1", 1, 14, 7, t => g_rdaysa = Number(t), () => g_rdaysa.toFixed(0)+"days"),
makeslider("r2:", "r2", 1, 28, 14, t => g_rdaysb = Number(t), () => g_rdaysb.toFixed(0)+"days"),
];
}
function bycountrybydate(table)
{
// reorganize the source data by country and date
var dict = {};
function addto(d, key, date, src)
{
if (!rec.total_cases)
return;
if (!d[key]) d[key] = { };
if (!d[key][date]) d[key][date] = { overleden:0, ziekenhuis:0, gevallen:0, inwoners:0 };
d[key][date].overleden += Number(rec.total_deaths);
d[key][date].gevallen += Number(rec.total_cases);
d[key][date].inwoners += Number(rec.population);
}
for (rec of table)
{
var date = rec.date;
var name = rec.location;
if (rec.iso_code.startsWith("OWID_"))
continue;
if (name)
addto(dict, name, date, rec);
var cont = rec.continent;
if (cont)
addto(dict, "Continent "+cont, date, rec);
addto(dict, "World", date, rec);
}
return dict;
}
function start()
{
// This function is called from body.onLoad, starts the fetch,
// and creates the controls and tables.
fetch(OWIDURL, { cache: 'no-store' }).then(r => r.text()).then( r => { g_table = bycountrybydate(decodecsv(r)); update(); });
var div = document.getElementById("controlsdiv");
div.append(...makesliders());
var div = document.getElementById("tablediv");
var tab = el('table');
tab.id = "table";
tab.cellSpacing = 0;
tab.cellPadding = 5;
tab.border = true;
createheader(tab.createTHead());
div.appendChild(tab);
add_sorter(tab);
var gdiv = document.getElementById("graphdiv");
gdiv.addEventListener('click', (() => { g_graphvalues = null; hidedetailgraph(); update(); } ))
gdiv.width = 800;
gdiv.height = 500;
var canvas = document.getElementById("detailgraph");
canvas.width = 800;
canvas.height = 500;
}
function update()
{
if (g_graphvalues) {
updatedetailgraph();
return;
}
// the table has three body sections, one for the whole world,
// one section with continents, and one with the list of countries.
// The three sections always stay in the same order, entries
// within a section can be sorted
var bodies = [];
var tbody = el('tbody');
name = "World";
data = g_table[name];
var row = makerow(name, data); if (row) tbody.append(row);
bodies.push(tbody);
var tbody = el('tbody');
Object.entries(g_table).forEach( ([name, data]) => { if (!name.startsWith("Continent")) return; var row = makerow(name, data); if (row) tbody.append(row); } );
bodies.push(tbody);
var tbody = el('tbody');
Object.entries(g_table).forEach( ([name, data]) => { if (name.startsWith("Continent") || name=="World") return; var row = makerow(name, data); if (row) tbody.append(row); } );
bodies.push(tbody);
var tab = document.getElementById('table');
tab.replaceChildren(tab.tHead, ...bodies);
if (tab.currentsortcolumn)
sortbycolumn(tab, tab.currentsortcolumn);
var infor = document.getElementById("infodiv").getBoundingClientRect();
var tabler = document.getElementById("tablediv").getBoundingClientRect();
if (infor.bottom > tabler.top) {
var tdiv = document.getElementById("tablediv")
tdiv.style.top = 20;
var adiv = document.getElementById("authordiv")
adiv.style.position = "absolute";
adiv.style.top = 0;
var idiv = document.getElementById("infodiv");
idiv.style.position = "absolute";
idiv.style.top = 30;
idiv.style.zIndex = 0;
idiv.style.opacity= 0.1;
}
}
function sortbycolumn(table, th)
{
// sort each section by the specified column.
// the 'th' object has extra properties: asc and colnr
const getCellValue = (tr, idx) => tr.children[idx].innerText || tr.children[idx].textContent;
const comparer = (idx, asc) => (a, b) => ((v1, v2) =>
v1 !== '' && v2 !== '' && !isNaN(v1) && !isNaN(v2) ? v1 - v2 : v1.toString().localeCompare(v2)
)(getCellValue(asc ? a : b, idx), getCellValue(asc ? b : a, idx));
for (var oldtbody of Array.from(table.tBodies)) {
var newtbody = table.createTBody();
Array.from(oldtbody.children)
.sort(comparer(th.colnr, th.asc))
.forEach(tr => newtbody.append(tr) );
table.removeChild(oldtbody);
}
table.currentsortcolumn = th;
}
function add_sorter(table)
{
// add column sorters to the specified table.
table.currentsortcolumn = null;
table.querySelectorAll('th').forEach(th => {
th.colnr = Array.from(th.parentNode.children).indexOf(th);
th.asc = true;
th.addEventListener('click', (() => { th.asc = !th.asc; sortbycolumn(table, th); } ))
});
}
--></script>
</head>
<body onLoad="start()">
<div id="infodiv">
Data from <a href="https://github.com/owid/covid-19-data/blob/master/public/data/">OurWorldInData</a>.
More tables:
<a href="owid.html">country info</a>,
<a href="rivm.html">Dutch municipalities</a>,
<a href="vac.html">Worldwide vaccinations</a>,
<a href="usvac.html">US vaccinations</a>.<br>
<br>
This table shows the current Covid-19 infection levels of all countries and continents.
Click table headers to sort, slide the filter to smooth the graphs, slide 'cutoff' up to turn off smoothing filter, use `mincases` and `minpop` to show less entries.
Note that the filtered graphs will be shorter, this can be adjusted with the 'bvalue' slider, this makes the filter less accurate. Click on the graph for a detailed graph view.
The colored graphs are the graphs for the days of the week, all mondays, all tuesdays, etc.
<br><p>
</div>
<div id="authordiv">
Created by Willem Hengeveld <<a href="mailto:[email protected]">[email protected]</a>>, <a href="https://itsme.home.xs4all.nl/">homepage</a>, <a href="https://github.com/nlitsme/covidcalculations">source code</a>.
</div>
<div id="controlsdiv"></div>
<div id="tablediv"></div>
<div id="graphdiv"> <canvas id="detailgraph"></canvas> </div>
<div id="weekdiv"> </div>
</body>
</html>