-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
312 lines (247 loc) · 8.74 KB
/
main.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
var list_countries = []
class MapPlot {
constructor() {
var map_container_svg = document.getElementById('map_container_svg');
const height = map_container_svg.offsetHeight;
const width = map_container_svg.offsetWidth;
var info;
this.svg = d3.select('div#map_container_svg')
.append('svg')
.attr('preserveAspectRatio', 'xMinYMin meet')
.attr('viewBox', '0 0 ' + width.toString(10) + ' ' + height.toString(10))
.classed('scaling-svg', true);
this.map_promise = d3.json("data/countries.json").then((topojson_raw) => {
const country_paths = topojson.feature(topojson_raw, topojson_raw.objects.countries);
return country_paths.features;
});
this.tool = d3.select('.scaling-svg-container').append('div')
.attr('class', 'hidden tool');
}
drawData(countryShapes, date, value, data, projection) {
throw new Error('Map plot class do not contain data and thus the map can not be drawn');
}
draw(date_indice = 0) {
throw new Error('Map plot class do not contain data and thus the map can not be drawn');
}
checkInstances() {
var g = document.getElementById('svg g');
if (g) g.remove();
var s = document.getElementById('slider_id');
if (s) s.remove();
var c = document.getElementById('colorbar-area');
if (c) c.remove();
var colorbar = document.getElementById('colorbar');
if (colorbar) colorbar.remove();
var point = document.getElementById('point_svg');
if (point)
point.remove();
}
}
function formatDate(input, formatInput, formatOutput) {
var dateParse = d3.timeParse("%Y-%m-%d");
var dateFormat = d3.timeFormat(formatOutput);
return dateFormat(dateParse(input));
}
function getData() {
const cdr = d3.csv("data/map_cdr.csv").then((data) => {
let year_to_cases = {};
let year_to_recovered = {};
let year_to_deaths = {};
let dates = {};
let max_cases = 0;
let max_recovered = 0;
let max_deaths = 0;
data.forEach((row) => {
// Cases
if (row.confirmed > max_cases) {
max_cases = parseInt(row.confirmed);
}
var date = this.formatDate(row.Date, "%Y-%m-%d", "%B %d, %Y");
if (year_to_cases[date] == undefined) {
year_to_cases[date] = [];
} else {
data = {};
data["lon"] = row.longitude;
data["lat"] = row.latitude;
data["data"] = parseInt(row.confirmed);
year_to_cases[date].push(data);
}
// Recoverd
if (row.recovered > max_recovered) {
max_recovered = parseInt(row.recovered);
}
var date = this.formatDate(row.Date, "%Y-%m-%d", "%B %d, %Y");
if (year_to_recovered[date] == undefined) {
year_to_recovered[date] = [];
} else {
data = {};
data["lon"] = row.longitude;
data["lat"] = row.latitude;
data["data"] = parseInt(row.recovered);
year_to_recovered[date].push(data);
}
//Deaths
if (row.deaths > max_deaths) {
max_deaths = parseInt(row.deaths);
}
var date = this.formatDate(row.Date, "%Y-%m-%d", "%B %d, %Y");
if (year_to_deaths[date] == undefined) {
year_to_deaths[date] = [];
} else {
data = {};
data["lon"] = row.longitude;
data["lat"] = row.latitude;
data["data"] = parseInt(row.deaths);
year_to_deaths[date].push(data);
}
dates[date] = date;
});
dates = d3.keys(dates).sort(function(a, b) {
return new Date(a) - new Date(b);
});
return [
[year_to_cases, max_cases],
[year_to_recovered, max_recovered],
[year_to_deaths, max_deaths],
[dates]
];
});
const gov_measures = d3.csv("data/gov_si.csv").then((data) => {
let country_to_year_to_data = {};
let dates = {};
data.forEach((row) => {
var date = this.formatDate(row.Date, "%Y-%m-%d", "%B %d, %Y");
if (country_to_year_to_data[row.Entity] == undefined) {
country_to_year_to_data[row.Entity] = {}
}
country_to_year_to_data[row.Entity][date] = parseFloat(row.StringencyIndex);
dates[date] = date;
});
dates = d3.keys(dates).sort(function(a, b) {
return new Date(a) - new Date(b);
});
return [country_to_year_to_data, dates];
});
return [cdr, gov_measures]
}
function checkInstancesButton() {
if (d3.select('#line_chart')) {
d3.select('#line_chart').remove();
}
if (d3.selectAll('#bar_plot_cont')) {
d3.selectAll('#bar_plot_cont').remove();
}
if (d3.selectAll('#sankey_diagram')) {
d3.selectAll('#sankey_diagram').remove();
}
if (d3.selectAll('#svg_cont_sankey')) {
d3.selectAll('#svg_cont_sankey').remove();
}
if (d3.selectAll('.svg_container_line_chart')) {
d3.selectAll('.svg_container_line_chart').remove();
}
if (d3.select('#line_chart_testing')) {
d3.select('#line_chart_testing').remove();
}
d3.select('#sankey_diagram_event').remove();
var point = document.getElementById('point_svg');
if (point)
point.remove();
}
function whenDocumentLoaded(action) {
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", action);
} else {
// `DOMContentLoaded` already fired
action();
}
}
function checkReady() {
var svg = document.getElementById("svg g");
if (svg == null) {
setTimeout("checkReady()", 5);
} else {
document.querySelector(
"#loader").style.display = "none";
document.querySelector(
"body").style.visibility = "visible";
}
}
whenDocumentLoaded(() => {
const data = getData();
const cdr = data[0];
const gov_measures = data[1];
bar_chart = new BarPlot("Confirmed cases");
bar_chart.draw();
// Initialize first page as cases
plot_object = new MapBubble(cdr);
plot_object.draw();
var change = false;
line_chart = new LinePlot("Confirmed cases");
line_chart.draw();
sankey_diagram = new Sankey();
sankey_diagram.draw_all_sankeys();
const cases_query = document.getElementById('cases');
cases_query.addEventListener('click', () => {
checkInstancesButton();
if (change) {
plot_object = new MapBubble(cdr);
change = false;
}
bar_chart = new BarPlot("Confirmed cases");
sankey_diagram = new Sankey();
line_chart = new LinePlot("Confirmed cases");
plot_object.class_name = "Confirmed cases";
plot_object.draw();
bar_chart.draw();
sankey_diagram.draw_all_sankeys();
line_chart.draw();
});
const recovered_query = document.getElementById('recovered');
recovered_query.addEventListener('click', () => {
checkInstancesButton();
if (change) {
plot_object = new MapBubble(cdr);
change = false;
}
bar_chart = new BarPlot("Recovered");
line_chart = new LinePlot("Recovered");
plot_object.class_name = "Recovered";
plot_object.draw();
bar_chart.draw();
line_chart.draw();
});
const deaths_query = document.getElementById('deaths');
deaths_query.addEventListener('click', () => {
checkInstancesButton();
if (change) {
plot_object = new MapBubble(cdr);
change = false;
}
bar_chart = new BarPlot("Deaths");
sankey_diagram = new Sankey();
line_chart = new LinePlot("Deaths");
plot_object.class_name = "Deaths";
plot_object.draw();
bar_chart.draw();
sankey_diagram.draw_all_sankeys();
line_chart.draw();
});
const measures_query = document.getElementById('measures');
measures_query.addEventListener('click', () => {
checkInstancesButton();
plot_object = new MapMeasures(gov_measures);
plot_object.draw();
top_measures = new BarPlot("Measures");
top_measures.draw();
line_chart = new LinePlot("Measures");
line_chart.draw();
line_tests = new LinePlot("Testing");
line_tests.draw();
change = true;
});
checkReady()
window.onresize = function() {
plot_object.draw(plot_object.date_ind);
};
});