-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfo.html
275 lines (246 loc) · 7.93 KB
/
info.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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Coronavirus information</title>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/Chart.min.js"></script>
<style>
body {
background-color: hsl(0, 0%, 95%);
font-family: sans-serif;
display: flex;
height: 100vh;
margin: 0;
}
.block {
margin: 10px;
}
#bar {
background-color: hsl(0, 0%, 85%, 50%);
}
#main {
flex: auto;
min-width: 0;
}
.grid {
display: grid;
grid-template-columns: auto 1fr;
grid-gap: 8px;
}
label { font-weight: bold; color: #0008; }
.value { text-align: right; font-family: monospace; color: #040; }
hr { border-top: 2px dotted black; }
</style>
</head>
<body>
<div id="bar">
<div id="input" class="block">
<select v-model="country">
<option v-for="country in countries">
{{ country }}
</option>
</select>
</div>
<hr>
<div id="info" class="grid block">
<label>Confirmed</label>
<div class="value">{{ confirmed | number }}</div>
<label>Deaths</label>
<div class="value">{{ deaths | number }}</div>
<label>Recovered</label>
<div class="value">{{ recovered | number }}</div>
<label>Active</label>
<div class="value">{{ active | number }}</div>
<label>New</label>
<div class="value">{{ recent | number }}</div>
<label>Confirmed growth</label>
<div class="value">{{ confirmed_gf | perc }}</div>
<label>Active growth</label>
<div class="value">{{ active_gf | perc }}</div>
<label>New growth</label>
<div class="value">{{ recent_gf | perc }}</div>
<label>Deaths growth</label>
<div class="value">{{ deaths_gf | perc }}</div>
<label>Rolling average growth</label>
<div class="value">{{ rolling_gf | perc }}</div>
<label>7-day prediction</label>
<div class="value">{{ prediction | number }}</div>
</div>
</div>
<div id="main">
<canvas id="chart"></canvas>
</div>
</body>
<script>
var number = new Intl.NumberFormat();
Vue.filter('number', v => number.format(v));
Vue.filter('perc', v => (v * 100).toFixed(2) + '%');
var input = new Vue({
el: '#input',
data: {
country: '',
countries: [],
},
watch: {
country: function (value) {
changeCountry(value);
}
},
})
var info = new Vue({
el: '#info',
data: {
confirmed: 0,
deaths: 0,
recovered: 0,
active: 0,
recent: 0,
confirmed_gf: 0,
active_gf: 0,
recent_gf: 0,
deaths_gf: 0,
rolling_gf: 0,
prediction: 0,
}
})
var chart = new Chart('chart', {
type: 'line',
data: {
labels: [],
datasets: [],
},
options: {
resposive: true,
maintainAspectRatio: true,
aspectRatio: 2,
layout: { padding: 8 },
legend: { position: 'bottom' },
elements: {
line: {
fill: false,
borderWidth: 2,
lineTension: 0,
},
},
tooltips: {
mode: 'x',
bodyAlign: 'right',
itemSort: (a, b) => b.yLabel - a.yLabel,
callbacks: {
label: (item, data) => {
return data.datasets[item.datasetIndex].label + ': ' + number.format(item.yLabel);
},
},
},
scales: {
xAxes: [{
scaleLabel: { labelString: 'Days after 100 cases', display: true, },
}],
yAxes: [{
scaleLabel: { display: true, },
ticks: {
callback: (value) => number.format(value),
},
}],
},
},
});
var data;
var filter;
function updateChart(country) {
chart.data.datasets = [];
let countries = [
{ id: 'Turkey', color: 'hsl(0, 75%, 50%)' },
{ id: 'US', color: 'hsl(30, 75%, 50%)' },
{ id: 'Spain', color: 'hsl(60, 75%, 50%)' },
{ id: 'Italy', color: 'hsl(90, 75%, 50%)' },
{ id: 'Iran', color: 'hsl(120, 75%, 50%)' },
{ id: 'United Kingdom', color: 'hsl(150, 75%, 50%)' },
{ id: 'Korea, South', color: 'hsl(180, 75%, 50%)' },
{ id: 'Japan', color: 'hsl(200, 75%, 50%)' },
{ id: 'Singapore', color: 'hsl(210, 75%, 50%)' },
];
const n = {
id: country,
color: 'hsl(280, 75%, 50%)',
params: { borderWidth: 4, pointStyle: 'rectRot' },
};
countries = countries.filter(e => e.id != n.id);
countries.unshift(n);
for (const {id, color, params} of countries) {
const dataset = {
label: id,
data: filter(data[id]),
borderColor: color,
backgroundColor: color,
};
if (params) Object.assign(dataset, params);
chart.data.datasets.push(dataset);
}
const max_days = chart.data.datasets.reduce((accum, e) => Math.max(accum, e.data.length), 0);
chart.data.labels = Array.apply(null, Array(max_days)).map((e, i) => i + 1);
chart.update();
}
function getGrowthFactor(data, day) {
const span = 5;
let sum = 0, total = 0;
for (let x = day - span + 1; x <= day; x += 1) {
sum += data[x].confirmed;
total += data[x - 1].confirmed;
}
return sum / total;
}
function getPrediction(confirmed, factor, factor_change, days) {
for (let x = 0; x < days; x++) {
confirmed = confirmed * factor;
factor += factor_change;
if (factor < 1.0) factor = 1.0;
}
return confirmed;
}
function changeCountry(country) {
const country_data = data[country];
const n = country_data.length;
const current = country_data[n - 1];
const prev = country_data[n - 2];
info.confirmed = current.confirmed;
info.deaths = current.deaths;
info.recovered = current.recovered;
info.active = current.confirmed - (current.deaths + current.recovered);
info.recent = current.confirmed - prev.confirmed;
info.confirmed_gf = (current.confirmed / prev.confirmed) - 1;
info.active_gf = ((current.confirmed - (current.deaths + current.recovered)) /
(prev.confirmed - (prev.deaths + prev.recovered))) - 1;
info.recent_gf = ((country_data[n - 1].confirmed - country_data[n - 2].confirmed) /
(country_data[n - 2].confirmed - country_data[n - 3].confirmed)) - 1;
info.deaths_gf = (current.deaths / prev.deaths) - 1;
info.rolling_gf = getGrowthFactor(country_data, n - 1) - 1;
const prev_days = 5;
const prev_rolling_gf = getGrowthFactor(country_data, n - 1 - prev_days) - 1;
const rate_change = (info.rolling_gf - prev_rolling_gf) / prev_days;
info.prediction = getPrediction(info.confirmed, 1 + info.rolling_gf, rate_change, 7).toFixed(0);
updateChart(country);
}
async function start() {
const response = await fetch('https://pomber.github.io/covid19/timeseries.json');
data = await response.json();
input.countries = Object.keys(data);
if (r = window.location.href.match(/[?&]country=([^&]+)/)) {
input.country = decodeURI(r[1]);
} else {
input.country = 'US';
}
}
chart.options.scales.yAxes[0].scaleLabel.labelString = 'Number of cases';
chart.options.scales.yAxes[0].type = 'logarithmic';
const axis = chart.options.scales.yAxes[0];
if (axis.type == 'logarithmic') {
axis.ticks.callback = (value) => {
return Number.isInteger(Math.log10(value)) ? number.format(value) : null;
};
}
filter = data => data.map(e => e.confirmed).filter(e => e >= 100);
start();
</script>
</html>