-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcoronalib.js
317 lines (262 loc) · 8.68 KB
/
coronalib.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
"use strict"
var labels = null
var days = {}
function beforeDayZero(day) {
return day["confirmed"] < 500
}
function getLabel(day, daynum)
{
return daynum
}
function getDays(data, country) {
var confirmed = []
for (var day of data[country]) {
if (beforeDayZero(day)) continue
confirmed.push(day["confirmed"])
if (confirmed.length > labels.length) labels.push(getLabel(day, labels.length))
}
days[country] = confirmed
return confirmed
}
function getInfected(data, country) {
var infected = []
var last_valid_recovered = null // workaround data issue at JHU
for (var day of data[country]) {
if (beforeDayZero(day)) continue
if (day["recovered"])
last_valid_recovered = day["recovered"]
infected.push(day["confirmed"] - day["deaths"] - last_valid_recovered)
}
return infected
}
function getFatalityRate(data, country) {
var fatality = []
for (var day of data[country]) {
if (beforeDayZero(day)) continue
fatality.push(day["deaths"] / day["confirmed"] * 100)
}
return fatality
}
function getDeadPerMillion(data, country) {
var dead = []
for (var day of data[country]) {
if (beforeDayZero(day)) continue
dead.push(day["deaths"] / (population[country] / 1e6))
}
return dead
}
function getRate(data, country) {
var confirmed = days[country]
var rate = [0]
for (var i = 1; i < confirmed.length; i++) {
rate.push((confirmed[i] / confirmed[i - 1]))
}
var avgd = new Array(7).fill(0)
for (var i = 6; i < rate.length; i++) {
avgd.push((rate[i] + rate[i - 1] + rate[i - 2] + rate[i - 3] + rate[i - 4] + rate[i - 5] + rate[i - 6]) / 7)
}
return avgd
}
function getRe(data, country) {
var delta = 7 // days
var confirmed = days[country]
var rate = new Array(2*delta).fill(-1)
for (var i = delta; i < confirmed.length; i++) {
rate.push((confirmed[i] - confirmed[i - delta]) / (confirmed[i - delta] - confirmed[i - 2*delta]))
}
return rate
}
function getIncidence(data, country) {
var delta = 7 // days
var confirmed = days[country]
var incidence = new Array(confirmed.length).fill(0)
for (var i = delta; i < confirmed.length; i++) {
// todays incidence
var dc = Math.max(0, confirmed[i] - confirmed[i - 1])
// add todays incidence to following 7 days
for(var j = 0; j < delta; j++)
incidence[Math.min(i + j, confirmed.length)] += dc
}
for (var i = 0; i < incidence.length; i++) {
incidence[i] /= (population[country] / 1e5)
}
return incidence
}
function getDoublingTime(data, country) {
return getRate(data, country).map(rate => Math.log(2) / Math.log(rate))
}
function getDatasets(data, fn, countries, fill) {
var colors = ['rgb(255, 132, 255)', 'rgb(255, 99, 132)', 'rgb(99, 255, 132)', 'rgb(99, 132, 255)', 'rgb(132, 255, 255)', 'rgb(255, 255, 132)']
var datasets = []
for (var i = 0; i < countries.length; i++) {
datasets.push(
{
label: countries[i],
fill: fill,
backgroundColor: colors[i],
borderColor: colors[i],
data: fn(data, countries[i]).map(x => x.toFixed(2))
}
)
}
return datasets
}
function tooltipDecimalPoint(tooltipItem, chart)
{
var datasetLabel = chart.datasets[tooltipItem.datasetIndex].label
return datasetLabel+": "+tooltipItem.yLabel.toLocaleString("en-US")
}
function tickDecimalPoint(value, index, values)
{
return value.toLocaleString("en-US")
}
Chart.defaults.global.defaultFontColor = 'white'
Chart.defaults.scale.gridLines.color = "rgba(255,255,255, 0.2)"
var config = {
// The type of chart we want to create
type: 'line',
// The data for our dataset
data: {
datasets: null,
labels: null
},
// Configuration options go here
options: {
responsive: true,
maintainAspectRatio: false,
title: {
display: true,
text: 'Corona Confirmed Comparison'
},
tooltips: {
mode: 'index',
intersect: false,
callbacks: {}
},
scales: {
xAxes: [{
display: true,
scaleLabel: {
display: true,
labelString: 'Days since 500 confirmed'
},
ticks: {
maxTicksLimit: 30
}
}],
yAxes: [{
display: true,
scaleLabel: {
display: true,
labelString: 'confirmed'
},
ticks: {}
}]
},
animation: {
duration: 0 // general animation time
},
hover: {
animationDuration: 0 // duration of animations when hovering an item
},
responsiveAnimationDuration: 0, // animation duration after a resize
elements: {
point: {
radius: 0 // default to disabled in all datasets
}
}
}
}
var charts = {}
function makeChart(name, config) {
var canvas = document.getElementById(name).getContext('2d')
if (name in charts) charts[name].destroy()
charts[name] = new Chart(canvas, config)
}
function mergeDays(from, to) {
// not smart: quadratic complexity, but whatever
outer:
for (var d2 of from) {
for (var i = 0; i < to.length; i++) {
var d1 = to[i]
if (d1.date == d2.date) {
d1.confirmed += d2.confirmed
d1.deaths += d2.deaths
d1.recovered += d2.recovered
continue outer
}
}
to.splice(i, 0, Object.assign({}, d2))
}
}
function aggregateEU(data, population) {
var countries = ["Austria", "Belgium", "Bulgaria", "Croatia", "Cyprus", "Czechia", "Denmark", "Estonia", "Finland", "France", "Germany", "Greece", "Hungary", "Ireland", "Italy", "Latvia", "Lithuania", "Luxembourg", "Malta", "Netherlands", "Poland", "Portugal", "Romania", "Slovakia", "Slovenia", "Spain", "Sweden"]
console.assert(countries.length == 27, "https://en.wikipedia.org/wiki/Member_state_of_the_European_Union")
data["EU"] = []
population["EU"] = 0
for (var c of countries) {
mergeDays(data[c], data["EU"])
population["EU"] += population[c]
}
}
function show_autocomplete()
{
var countries_str = document.getElementById('countries').value
var elements = countries_str.split(";")
var typing = elements[elements.length - 1].trim()
var autocomplete = document.getElementById("autocomplete")
autocomplete.classList.remove("show")
if(typing.length < 2) return
var re = RegExp("^"+typing, 'i')
var matches = all_countries.filter(x => re.test(x))
if(matches.length == 0) return
let completeHTML = ""
for (const m of matches) {
completeHTML += `<a class="dropdown-item" href="#" onclick="complete(this)">${m}</a>`
}
autocomplete.innerHTML = completeHTML
autocomplete.classList.add("show")
}
function complete(elt)
{
var countries_str = document.getElementById('countries').value
var elements = countries_str.split(";")
elements[elements.length - 1] = " "+elt.innerText
document.getElementById('countries').value = elements.join(";")
document.getElementById('autocomplete').classList.remove("show")
}
async function refresh() {
var countries_str = document.getElementById('countries').value
var countries = countries_str.split(";").map(e => e.trim())
var err = ""
var response = await fetch("https://pomber.github.io/covid19/timeseries.json")
var data = await response.json()
all_countries = Object.keys(data)
aggregateEU(data, population)
var initial = !Object.keys(charts).length
for (var c of countries) {
if (!(c in data))
err += c + " is not a know country name "
if (!(c in population))
err += " population of " + c + " not known"
}
if (err) {
alert(err)
return
}
if (!initial) {
history.pushState(null, "title", "?countries=" + encodeURI(countries_str))
}
labels = [] // force recompute labels length
makeCharts(data, countries)
}
var urlParams = new URLSearchParams(window.location.search)
var countries_str = urlParams.get('countries')
var all_countries = null
// fallback for old links
if (window.location.hash && !document.getElementById(window.location.hash.substring(1))) {
countries_str = decodeURI(window.location.hash.substring(1))
}
if (countries_str) {
document.getElementById('countries').value = countries_str
}