forked from paolini/covid19-charts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset_hopkins.js
299 lines (274 loc) · 12 KB
/
dataset_hopkins.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
class HopkinsDataset extends BaseDataset {
constructor(options) {
super(options);
this.REPOSITORY_URL = "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/";
this.filter_column = options.filter_column || "Country/Region";
this.subfilter_column = options.subfilter_column || "Province/State";
this.fields = options.fields;
this.first_time_column = 4;
this.fields = options.fields;
this.supranat_comp = {
"ASEAN" : ["Brunei", "Cambodia", "Indonesia", "Laos", "Malaysia", "Burma", "Philippines", "Singapore", "Thailand", "Vietnam"],
"AU" : ["Algeria", "Angola", "Benin", "Botswana", "Burkina Faso", "Burundi", "Cameroon", "Cabo Verde", "Central African Republic", "Chad", "Comoros", "Congo (Kinshasa)", "Congo (Brazzaville)", "Djibouti", "Egypt", "Equatorial Guinea", "Eritrea", "Eswatini", "Ethiopia", "Gabon", "Gambia", "Ghana", "Guinea", "Guinea-Bissau", "Cote d'Ivoire", "Kenya", "Lesotho", "Liberia", "Libya", "Madagascar", "Malawi", "Mali", "Mauritania", "Mauritius", "Morocco", "Mozambique", "Namibia", "Niger", "Nigeria", "Rwanda", "Western Sahara", "Sao Tome and Principe", "Senegal", "Seychelles", "Sierra Leone", "Somalia", "South Africa", "South Sudan", "Sudan", "Tanzania", "Togo", "Tunisia", "Uganda", "Zambia", "Zimbabwe"],
"CARICOM" : ["Antigua and Barbuda", "Bahamas", "Barbados", "Belize", "Dominica", "Grenada", "Guyana", "Haiti", "Jamaica", "Saint Kitts and Nevis", "Saint Lucia", "Saint Vincent and the Grenadines", "Suriname", "Trinidad and Tobago"],
"CIS": ["Armenia", "Azerbaijan", "Belarus", "Kazakhstan", "Kyrgyzstan", "Moldova", "Russia", "Tajikistan", "Uzbekistan"],
"ECO" : ["Afghanistan", "Azerbaijan", "Iran", "Kazakhstan", "Kyrgyzstan", "Pakistan", "Tajikistan", "Turkey", "Turkmenistan", "Uzbekistan"],
"EU" : ["Belgium", "Bulgaria", "Czechia", "Denmark", "Germany" , "Estonia", "Ireland", "Greece", "Spain", "France", "Croatia",
"Italy", "Cyprus", "Latvia", "Lithuania", "Luxembourg", "Hungary", "Malta", "Netherlands", "Austria", "Poland",
"Portugal", "Romania", "Slovenia", "Slovakia", "Finland", "Sweden"],
"GCC" : ["Bahrain", "Kuwait", "Oman", "Qatar", "Saudi Arabia", "United Arab Emirates"],
"Pacific Alliance" : ["Chile", "Colombia", "Mexico", "Peru"],
"PIF" : ["Australia", "Fiji", "Kiribati", "Marshall Islands", "Micronesia", "Nauru", "New Zealand", "Palau", "Papua New Guinea", "Samoa", "Solomon Islands", "Tonga", "Tuvalu", "Vanuatu"],
"Turkic Council" : ["Azerbaijan", "Kazakhstan", "Kyrgyzstan", "Turkey", "Uzbekistan"]
};
this.country_dict = null;
this.population_field = null;
this.world_label = 'world';
}
init_html() {
super.init_html();
if (this.supranat_comp) {
this.$supranat = $("select[name='" + this.prefix + "_supranat']");
}
this.$select = $("select[name='" + this.prefix + "_filter']");
this.$subselect = $("select[name='" + this.prefix + "_subfilter']");
this.$column = $("select[name='" + this.prefix + "_column']");
}
populate_html() {
var self = this;
if (self.country_dict === null) { // execute once
self.country_dict = {};
var i = this.table.headers.indexOf(this.filter_column);
var j = this.table.headers.indexOf(this.subfilter_column);
this.table.rows.forEach(function(row) {
var value = row[i];
var subvalue = row[j];
if (!self.country_dict.hasOwnProperty(value)) self.country_dict[value] = {};
if (subvalue !== "") {
self.country_dict[value][subvalue] = true;
}
});
}
if (self.supranat_comp) {
self.$supranat.find("option").remove();
self.$supranat.append("<option value=''>world</option>");
for(var option in this.supranat_comp) {
self.$supranat.append("<option value='" + option + "'>" + option + "</option>");
}
}
function populate_select(options) {
self.$select.find("option").remove();
self.$select.append("<option value=''>-- all states --</option>");
options.sort();
options.forEach(function(option) {
self.$select.append("<option value='" + option + "'>" + option + "</option>");
});
}
if (self.supranat_comp) {
self.$supranat.change(function() {
var value = self.$supranat.val();
if (value === "") { // world
populate_select(Object.getOwnPropertyNames(self.country_dict));
} else {
populate_select(self.supranat_comp[value]);
}
});
self.$supranat.change();
} else {
populate_select(Object.getOwnPropertyNames(self.country_dict));
}
self.$select.change(function() {
var value = self.$select.children("option:selected").val();
self.$subselect.find("option").remove();
self.$subselect.append("<option value=''>-- all regions --</option>");
var options = [];
if (value) {
options = Object.getOwnPropertyNames(self.country_dict[value]);
options.sort();
options.forEach(function(option) {
self.$subselect.append("<option value='" + option + "'>" + option + "</option>");
});
}
self.$subselect.toggle(options.length>0);
});
this.$select.change();
this.$subselect.change(function() {
var value = self.$subselect.children("option:selected").val();
self.$column.prop("disabled", false);
});
this.$subselect.change();
this.$column.find("option").remove();
this.fields.forEach(function(option) {
self.$column.append("<option value='" + option + "'>" + option + "</option>");
});
}
get_population(options) {
if (options['subvalue'] !== "") {
return 0; // population of region is unknown
}
var value = options['value'];
if (value === '') {
return 7800000000; // world population
} else if (value in this.supranat_comp) {
var population = 0;
this.supranat_comp[options['value']].forEach(function(country){
var p = country_population[country];
if (!p) {console.log(country + " has no population data");}
population += country_population[country];
});
return population;
}
return country_population[value];
}
get_series_basic(column, options) {
var self = this;
var value = options['value'];
var subvalue = options['subvalue'];
var subtable = this.table;
var label = column + " " + value;
if (value === '') { // world population
// no filtering!
label += self.world_label;
} else if (this.supranat_comp && (value in this.supranat_comp)) {
var k = subtable.headers.indexOf(this.filter_column); // column with country name
var lst = this.supranat_comp[value];
subtable = new Table(
subtable.headers,
subtable.rows.filter(function(row) {
return lst.indexOf(row[k]) >= 0;
}));
} else {
subtable = subtable.filter(this.filter_column, value);
if (subvalue !== "") {
subtable = subtable.filter(this.subfilter_column, subvalue);
label += " " + subvalue;
}
}
var data_x = this.table.headers.slice(this.first_time_column).map(anglo_to_date);
var data_y = new Array(data_x.length)
data_y.fill(0);
var population_i = null;
var population = 0;
if (self.population_field) {
population_i = subtable.headers.indexOf(self.population_field);
}
subtable.rows.forEach(function(row){
if (population_i !== null) {
population += parseInt(row[population_i]);
}
for (var i=0; i < data_y.length; i++) {
data_y[i] += parseInt(row[i+self.first_time_column]);
}
});
var series = new Series(data_x, data_y, label);
if (self.population_field) {
series.population = population;
} else {
series.population = this.get_population(options);
}
series.y_axis = 'count';
series.cumulative = true;
return series;
}
get_options() {
var options = super.get_options();
options.value = this.$select.children("option:selected").val();
if (options.value === '') {
if (this.supranat_comp) {
options.value = this.$supranat.val();
}
}
options.subvalue = this.$subselect.children("option:selected").val();
options.column = this.$column.children("option:selected").val();
return options;
}
}
class HopkinsConfirmedDataset extends HopkinsDataset {
constructor() {
super({
name: "confirmed",
path: "csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv",
fields: [
'confirmed',
'confirmed / population',
'confirmed increment',
'confirmed increment / population',
'confirmed increment rate'
],
});
}
}
class HopkinsDeathsDataset extends HopkinsDataset {
constructor() {
super({
name: "deaths",
path: "csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_deaths_global.csv",
fields: [
'deaths',
'deaths / population',
'deaths increment',
'deaths increment / population',
'deaths increment rate'
],
});
}
}
class HopkinsRecoveredDataset extends HopkinsDataset {
constructor() {
super({
name: "recovered",
path: "csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_recovered_global.csv",
fields: [
'recovered',
'recovered / population',
'recovered increment',
'recovered increment / population',
'recovered increment rate'
],
});
}
}
class HopkinsUSDataset extends HopkinsDataset {
constructor(options) {
super(options);
this.filter_column = "Province_State";
this.subfilter_column = "Admin2";
this.first_time_column = 12;
this.supranat_comp = null;
this.population_field = "Population";
this.world_label = "US";
}
get_population(options) {
throw new Error("don't use get_population since population_field exists");
}
}
class HopkinsConfirmedUSDataset extends HopkinsUSDataset {
constructor() {
super({
name: "confirmedUS",
path: "csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_US.csv",
fields: [
'confirmed',
'confirmed / population',
'confirmed increment',
'confirmed increment / population',
'confirmed increment rate'
],
});
}
}
class HopkinsDeathsUSDataset extends HopkinsUSDataset {
constructor() {
super({
name: "deathsUS",
path: "csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_deaths_US.csv",
fields: [
'deaths',
'deaths / population',
'deaths increment',
'deaths increment / population',
'deaths increment rate'
],
});
}
}