-
Notifications
You must be signed in to change notification settings - Fork 0
/
covid.html
174 lines (143 loc) · 4.7 KB
/
covid.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
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8" />
<title>Covid</title>
<script src="https://d3js.org/d3.v5.min.js"></script>
<script src="https://code.jquery.com/jquery-latest.min.js"></script>
<style type="text/css">
</style>
</head>
<body>
<input type="radio" id="confirmedRate" name="mode" value="confirmedRate" onclick="switchMode();" checked="true">
<label for="confirmedRate">Confirmed Rate</label>
<input type="radio" id="deathRate" name="mode" value="deathRate" onclick="switchMode();">
<label for="deathRate">Death Rate</label>
<input type="radio" id="confirmed" name="mode" value="confirmed" onclick="switchMode();">
<label for="confirmed">Confirmed</label>
<input type="radio" id="deaths" name="mode" value="deaths" onclick="switchMode();">
<label for="deaths">Deaths</label>
<div id="divSvg"></div>
<script>
let replaceToken = "###";
let baseUrl = "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/" +
`csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_${replaceToken}_global.csv`
let deathsUrl = baseUrl.replace("###", "deaths");
let confirmedUrl = baseUrl.replace("###", "confirmed");
let deaths = null;
let confirmed = null;
let confirmedRate = null;
let deathRate = null;
let width = 1600;
let height = 800;
let chart = d3.select("#divSvg")
.append("svg")
.attr("width", width)
.attr("height", height)
.attr("style", "background-color: #ccc")
.on("wheel", function (d) {
var direction = d3.event.wheelDelta < 0 ? 'down' : 'up';
zoom(direction === 'up' ? 1 : -1);
});
let records = null;
let yScale = 1; // TODO: this should be calculated based on largest value (largest of the latest values)
let hoffset = 850;
let hscale = 2; // pixels per day
function loadData(text) {
let lines = text.match(/[^\r\n]+/g);
let records = lines.slice(1);
let result = {};
for (let ri in records) {
let record = records[ri].split(',');
let countryName = record[1];
if (record[0].length > 0) {
countryName += ` (${record[0]})`;
}
cases = record.slice(4).map(parseFloat);
result[countryName] = cases;
}
return result;
}
function rate(records) {
result = {}
const offset = 21;
for (let countryName in records) {
let cases = records[countryName];
let rates = Array(offset).fill(0);
for (let i = offset; i < cases.length; i++) {
rates.push(cases[i] - cases[i-offset]);
}
result[countryName] = rates;
}
return result;
}
function zoom(direction) {
yScale *= (1 + (direction * 0.2));
redraw();
}
function getScale(records) {
let largest = 0;
for (let countryName in records) {
let cases = records[countryName];
let max = Math.max(...cases)
if (max > largest)
largest = max;
}
return 0.95 * height / largest
}
function switchMode() {
let mode = document.querySelector('input[name="mode"]:checked').value;
records = {confirmed, deaths, confirmedRate, deathRate}[mode];
yScale = getScale(records);
redraw();
}
function redraw() {
d3.selectAll(".refreshable").remove();
for (let countryName in records) {
let cases = records[countryName];
let color = "gray"
switch (countryName) {
case "Brazil":
color = "green";
break;
case "US":
color = "red";
break;
case "India":
color = "orange";
break;
case "Mexico":
color = "brown";
break;
case "France":
color = "blue";
break;
case "Italy":
color = "#FF5733";
break;
}
chart.append("path")
.classed("refreshable", true)
.attr("stroke", color)
.attr("fill", "none")
.attr("d", d3.line()(cases.map((v, i) => [i * hscale - hoffset, 800 - v * yScale])))
chart.append("text")
.classed("refreshable", true)
.attr("x", cases.length * hscale - hoffset)
.attr("y", 800 - (cases[cases.length - 1] * yScale))
.text(countryName);
}
}
fetch(deathsUrl).then(response => response.text()).then(text => {
deaths = loadData(text);
deathRate = rate(deaths);
switchMode();
});
fetch(confirmedUrl).then(response => response.text()).then(text => {
confirmed = loadData(text);
confirmedRate = rate(confirmed);
switchMode();
});
</script>
</body>
</html>