-
Notifications
You must be signed in to change notification settings - Fork 0
/
visualization.js
304 lines (267 loc) · 10.4 KB
/
visualization.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
let country_code = 'ESP';
let countries_data = null;
async function loadData(url = '') {
/**
* Loads data from a given url in JSON format and returns it as an
* objects array.
* See:
* https://developer.mozilla.org/es/docs/Web/API/Fetch_API/Using_Fetch
*
* Due to CORS restrictions, file can be loaded from local storage. In that case, file should define JSON data through a variable called "covid_data".
*
* @returns {Array}
*/
if (!url.startsWith('http')) {
return covid_data; // defined in local file
}
const response = await fetch(
url,
{
method: 'GET',
mode: 'same-origin',
redirect: 'follow',
referrerPolicy: 'no-referrer',
headers: {
'Content-Type': 'application/json',
},
}
);
return response.json();
}
function roundTo(num, digits = 5) {
/**
* Rounds a float to a given value of digits
*/
return +(Math.round(num + "e+" + digits) + "e-" + digits);
}
function preprocessData(item) {
/**
* Given a data registry, add percent values fields and a date
* field to be used as x-axis value.
*/
var dateParser = d3.timeParse("%d/%m/%Y");
item.cases_percent = roundTo(item.cases * 100.0 / item.popData2020);
item.deaths_percent = roundTo(item.deaths * 100.0 / item.popData2020);
item.date = dateParser(item.dateRep);
return item;
}
function tooltipHtml(d) {
let html = '<table>';
html += `<tr><td>Date:</td><td class="value">${d.dateRep}</td></tr>`;
html += `<tr><td>Cases:</td><td class="value">${d.cases}</td></tr>`;
html += `<tr><td>Deaths:</td><td class="value">${d.deaths}</td></tr>`;
html += '</table>';
return html;
}
function showCountryHtml(d) {
let html = `<h2>${d.name}</h2>`;
html += '<div class="row">';
html += `<div class="floating"><b>Country:</b> <a href="${d.link}" target=_blank>${d.name} (${d.country_code}).</a></div><div class="floating"><b>Population:</b> ${d.population.toLocaleString()}</div>`;
html += '</div>';
html += '<div class="row">';
html += '<div class="floating"><h3>Cases</h3></div>';
html += '<div class="floating"><table>';
html += '<tr><th>Total</th><th>Total vs population</th><th>Last 30d</th><th>Last 7d</th></tr>';
html += `<tr><td>${d.total_cases.toLocaleString()}</td><td>${d.total_pcnt_cases.toLocaleString()} %</td><td>${d.last_month_cases.toLocaleString()}</td><td>${d.last_week_cases.toLocaleString()}</td></tr>`;
html += '</table></div>';
html += '</div>';
html += '<div class="row">';
html += '<div class="floating"><h3>Deaths</h3></div>';
html += '<div class="floating"><table>';
html += '<tr><th>Total</th><th>Total vs population</th><th>Last 30d</th><th>Last 7d</th></tr>';
html += `<tr><td>${d.total_deaths.toLocaleString()}</td><td>${d.total_pcnt_deaths.toLocaleString()} %</td><td>${d.last_month_deaths.toLocaleString()}</td><td>${d.last_week_deaths.toLocaleString()}</td></tr>`;
html += '</table></div>';
html += '</div>';
d3.select("#country_data").html(html);
}
function showGraph(data, yAttr) {
// Margin setup
var margin = {top: 100, right: 100, bottom: 20, left: 100};
var width = 900 - margin.left - margin.right;
var height = 300 - margin.top - margin.bottom;
d3.select("#graph_tooltip").remove();
var div = d3.select("body")
.append("div")
.attr("id", "graph_tooltip")
.attr("class", "tooltip")
.style("opacity", 0)
.style("display", "none");
// Basic SVG canvas
var svg = d3.select("#graph").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// Temporal scale
var x = d3.scaleTime().range([0, width]);
// Linear scale
var y = d3.scaleLinear().range([height, height - 250]);
// Line graph
var line = d3.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d[yAttr]); });
// g is a group of vectorial graphics
var g = svg.append("g").attr("transform", "translate(0, 0)");
x.domain(d3.extent(data, function(d) { return d.date; }));
y.domain([0, d3.max(data, function(d) { return d[yAttr]; })]);
g.selectAll("circle").data(data).enter()
.append("circle")
.attr("cx", function(d) { return x(d.date); })
.attr("cy", function(d) { return y(d[yAttr]); })
.attr("r", function(d, i) { return 5; })
// .attr("id", function(d) { return d.id; })
.style("fill", "#fcb0b5")
.on("mouseover", function(event){
const d = event.target.__data__;
div.html(tooltipHtml(d))
.style("left", (event.pageX + 20) + "px")
.style("top", (event.pageY - 20) + "px")
.style("opacity", .9)
.style("display", "block");
d3.select(this).transition().duration(200).style("fill", "#d30715");
g.selectAll("#tooltip_path").data([d]).enter().append("line")
.attr("id", "tooltip_path")
.attr("class", "line")
.attr("d", line)
.attr("x1", function(d) {return x(d.date)})
.attr("x2", function(d) {return x(d.date)})
.attr("y2", height)
.attr("y1", function(d) {return y(d[yAttr]);})
.attr("stroke", "black")
.style("stroke-dasharray", ("3, 3"));
})
.on("mouseout", function(d) {
d3.select(this).transition().duration(500).style("fill", "#fcb0b5");
g.selectAll("#tooltip_path").remove();
div.style("opacity", 0)
.style("left", "0px")
.style("right", "0px")
.style("display", "none")
});
g.selectAll("path").data([data]).enter().append("path")
.attr("class", "line")
.attr("d", line);
svg.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
svg.append("g")
.attr("class", "axis axis--y")
.attr("transform", "translate(0," + 0 + ")")
.call(d3.axisLeft(y));
}
function getCountryRegistries(countryCode) {
/**
* Since Country data appears to be in reverse order and has
* a registry with accumulated summary, we should remove that
* registry and reverse registries (from older to newer).
*/
let countryRegistries = countries_data.filter(d => d.countryterritoryCode == countryCode);
countryRegistries.pop(); // remove summary registry
return countryRegistries.reverse();
}
function getCountrySummary(countryCode) {
/**
* Since Country data appears to be in reverse order and has
* a registry with accumulated summary, we should remove that
* registry and reverse registries (from older to newer).
*/
let countryRegistries = countries_data
.filter(d => d.countryterritoryCode == countryCode)
.reverse();
const first_registry = countryRegistries.shift(); // first registry is a summary
let country_summary = {
name: first_registry.countriesAndTerritories,
country_code: first_registry.countryterritoryCode,
total_cases: first_registry.cases,
total_deaths: first_registry.deaths,
population: parseInt(first_registry.popData2020),
last_week_cases: 0,
last_month_cases: 0,
last_week_deaths: 0,
last_month_deaths: 0,
total_pcnt_cases: 0,
total_pcnt_deaths: 0,
link: '',
};
var pos = countryRegistries.length - 1;
for (var i = 0; i < 7; i++) {
country_summary.last_week_deaths += countryRegistries[pos - i].deaths;
country_summary.last_week_cases += countryRegistries[pos - i].cases;
}
pos = countryRegistries.length - 1;
for (var i = 0; i < 30; i++) {
country_summary.last_month_deaths += countryRegistries[pos - i].deaths;
country_summary.last_month_cases += countryRegistries[pos - i].cases;
}
for (var i = 0; i < countryRegistries.length; i++) {
country_summary.total_deaths += countryRegistries[i].deaths;
country_summary.total_cases += countryRegistries[i].cases;
}
country_summary.total_pcnt_deaths = roundTo(country_summary.total_deaths * 100 / country_summary.population, 2);
country_summary.total_pcnt_cases = roundTo(country_summary.total_cases * 100 / country_summary.population, 2);
country_summary.link = `https://en.wikipedia.org/wiki/${country_summary.name}`;
return country_summary;
}
function getCountries() {
/**
* Returns an array of 'name' and 'code' for all unique
* countries that appears in countries_data.
*/
var countries = [];
let seen_countries = {}
for (var item, i = 0; item = countries_data[i++];) {
if (!(item.countriesAndTerritories in seen_countries)) {
seen_countries[item.countriesAndTerritories] = item.countryterritoryCode;
countries.push({
'name': item.countriesAndTerritories,
'code': item.countryterritoryCode,
});
}
}
return countries;
}
function showSelector() {
/**
* Draows a selector with options for selecting any country
* present in countries_data.
* When selection changes, it shows that country data.
*/
const countries = getCountries();
var select = d3.select('#countries')
.append('select')
.attr('class','select')
.on('change', select_country);
select
.selectAll('option')
.data(countries).enter()
.append('option')
.text(function (d) { return d.name; })
.attr('value', function (d) { return d.code })
.attr('id', function (d) { return `opt_${d.code}` });
d3.select(`#opt_${country_code}`)
.property('selected', true);
}
function select_country() {
/**
* Refresh screen shown data according to selected country.
*/
country_code = d3.select('select').property('value');
d3.select('#graph').html('');
d3.select('#country_data').html('');
showGraph(getCountryRegistries(country_code), 'cases');
showCountryHtml(getCountrySummary(country_code));
};
/**
* On document ready ...
* - Load data
* - Filter by country
* - Show D3 SVG graphics
* - Show country selector
*/
document.addEventListener("DOMContentLoaded", async function(event) {
countries_data = (await loadData()).map(preprocessData);
showGraph(getCountryRegistries(country_code), 'cases');
showCountryHtml(getCountrySummary(country_code));
showSelector();
});