-
Notifications
You must be signed in to change notification settings - Fork 6
/
app.js
412 lines (357 loc) · 12.3 KB
/
app.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
// SELECT ALL ELEMENTS
const country_name_element = document.querySelector(".country-name");
const active_element = document.querySelector(".th-1 .value");
const total_cases_element = document.querySelector(".th-5 .value");
const recovered_element = document.querySelector(".th-2 .value");
const deaths_element = document.querySelector(".th-3 .value");
const test_done_element = document.querySelector(".th-4 .value");
// const country_element = document.querySelector(".th-6 .value");
const world_confirmed_element= document.querySelector(".side-table-1 .value");
const world_recovered_element= document.querySelector(".side-table-2 .value");
const world_deaths_element= document.querySelector(".side-table-3 .value");
const ctx = document.getElementById("axes_line_chart").getContext("2d");
// APP VARIABLES
let app_data = [],
cases_list = [],
recovered_list = [],
deaths_list = [],
deaths = [],
test_done_list= [],
formatedDates = [],
world_confirmed_data,
world_recovered_data,
world_deaths_data,
countries2d = [];
var all_countries_data = {};
var sorted_countries2d = [];
var flag_codes = [];
var top_ten_country_names = [];
var top_ten_country_cases = [];
// var create_map= 1;
// GET USERS COUNTRY CODE
// let country_code = geoplugin_countryCode();
let user_country = "Germany";
// country_list.forEach((country) => {
// if (country.code == country_code) {
// user_country = country.name;
// }
// });
fetchData(user_country);
function get_countries2d(){
var requestOptions = {
method: "GET",
redirect: "follow",
};
fetch(
"https://corona-api.com/countries",
requestOptions
)
.then((res) => {
return res.json();
})
.then((data) => {
// console.log(data);
// console.log(data.data);
all_countries_data = data.data;
populate_countries2d();
});
}
get_countries2d();
function populate_countries2d(){
// console.log(all_countries_data);
countries2d.push(['Country', 'Confirmed Cases']);
for(i=0; i<all_countries_data.length;++i){
// var country = all_countries_data[i].name;
// var cases = all_countries_data[i].latest_data.confirmed;
// console.log(all_countries_data[i].name);
// console.log(all_countries_data[i].latest_data.confirmed);
// if (all_countries_data[i].name == "USA"){
// countries2d.push(["US", all_countries_data[i].latest_data.confirmed]);
// }
// else
countries2d.push([all_countries_data[i].name, all_countries_data[i].latest_data.confirmed]);
}
console.log(countries2d);
//Making array as inbuilt sort function can only be used on array
// var sorted_countries2d = []; //make this global variable
for(i=0; i<countries2d.length;++i){
sorted_countries2d.push(countries2d[i]);
}
sorted_countries2d.sort(function(a, b) {
return b[1] - a[1];
});
console.log(sorted_countries2d);
// console.log(countries2d[2][1]);
createMap();
fetch_top_ten_countries();
}
function fetch_top_ten_countries(){
for(i=1; i<=25;++i){
for(j=0;j<country_list.length;++j){
if(sorted_countries2d[i][0]== country_list[j].name){
// console.log(country_list[j].code);
flag_codes.push(country_list[j].code);
top_ten_country_names.push(country_list[j].name);
top_ten_country_cases.push(sorted_countries2d[i][1]);
}
}
}
console.log(flag_codes);
console.log(top_ten_country_names);
console.log(top_ten_country_cases);
for(i=1; i<=25;++i){
const flag_element = document.getElementById("td-"+i+"-flag");
const country_name_element = document.getElementById("td-"+i+"-country-name");
const country_cases_element = document.getElementById("td-"+i+"-country-cases");
// console.log(element);
// const td_8_flag_element = document.getElementById("td-8-flag");
// const td_8_country_name_element = document.getElementById("td-8-country-name");
// const td_8_country_cases_element = document.getElementById("td-8-country-cases");
flag_code = flag_codes[i-1];
// console.log(flag_code);
flag_element.innerHTML += `<img class="flags" src="https://www.countryflags.io/${flag_code}/flat/64.png">`;
// console.log(td_8_flag_element);
country_name_element.innerHTML = top_ten_country_names[i-1];
// console.log(td_8_country_name_element);
country_cases_element.innerHTML = top_ten_country_cases[i-1];
}
}
function fetchData(country) {
user_country = country;
country_name_element.innerHTML = "Loading...";
(cases_list = []),
(recovered_list = []),
(deaths_list = []),
(dates = []),
(formatedDates = []);
var requestOptions = {
method: "GET",
redirect: "follow",
};
const api_fetch = async (country) => {
await fetch(
"https://api.covid19api.com/total/country/" +
country +
"/status/confirmed",
requestOptions
)
.then((res) => {
return res.json();
})
.then((data) => {
data.forEach((entry) => {
dates.push(entry.Date);
// console.log(dates[dates.length-1]);
cases_list.push(entry.Cases);
});
});
await fetch(
"https://api.covid19api.com/total/country/" +
country +
"/status/recovered",
requestOptions
)
.then((res) => {
return res.json();
})
.then((data) => {
data.forEach((entry) => {
recovered_list.push(entry.Cases);
});
});
await fetch(
"https://api.covid19api.com/total/country/" + country + "/status/deaths",
requestOptions
)
.then((res) => {
return res.json();
})
.then((data) => {
data.forEach((entry) => {
deaths_list.push(entry.Cases);
});
});
//tests done
await fetch(
"https://corona.lmao.ninja/v2/countries/" +
country ,
requestOptions
)
.then((res) => {
return res.json();
})
.then((data) => {
test_done_list= data.tests;
// console.log(data);
// data.forEach((entry) => {
// dates.push(entry.Date);
// // console.log(dates[dates.length-1]);
//
// cases_list.push(entry.Cases);
// });
});
//Fetching Statistics for entire World
await fetch(
"https://covid19.mathdro.id/api",
requestOptions
)
.then((res) => {
return res.json();
})
.then((data) => {
world_confirmed_data = data.confirmed.value;
world_recovered_data = data.recovered.value;
world_deaths_data = data.deaths.value;
});
updateUI();
};
api_fetch(country);
}
// fetchData(user_country);
// UPDATE UI FUNCTION
function updateUI() {
updateStats();
axesLinearChart();
}
function updateStats() {
const total_cases = cases_list[cases_list.length - 1];
// const new_confirmed_cases = total_cases - cases_list[cases_list.length - 2];
const total_recovered = recovered_list[recovered_list.length - 1];
// const new_recovered_cases =
// total_recovered - recovered_list[recovered_list.length - 2];
const total_deaths = deaths_list[deaths_list.length - 1];
// const new_deaths_cases = total_deaths - deaths_list[deaths_list.length - 2];
const total_country = country_list.length;
country_name_element.innerHTML = user_country;
total_cases_element.innerHTML = total_cases;
// new_cases_element.innerHTML = `+${new_confirmed_cases}`;
recovered_element.innerHTML = total_recovered;
// new_recovered_element.innerHTML = `+${new_recovered_cases}`;
deaths_element.innerHTML = total_deaths;
test_done_element.innerHTML = test_done_list;
// new_deaths_element.innerHTML = `+${new_deaths_cases}`;
active_element.innerHTML= total_cases- total_recovered;
// console.log(country_element);
// country_element.innerHTML = country_list.length;
world_confirmed_element.innerHTML= world_confirmed_data;
world_recovered_element.innerHTML= world_recovered_data;
world_deaths_element.innerHTML= world_deaths_data;
// format dates
dates.forEach((date) => {
formatedDates.push(formatDate(date));
});
}
// UPDATE CHART
let my_chart;
function axesLinearChart() {
if (my_chart) {
my_chart.destroy();
}
my_chart = new Chart(ctx, {
type: "line",
data: {
datasets: [
{
label: "Cases",
data: cases_list,
fill: false,
borderColor: "#FFF",
backgroundColor: "#FFF",
borderWidth: 1,
},
{
label: "Recovered",
data: recovered_list,
fill: false,
borderColor: "#009688",
backgroundColor: "#009688",
borderWidth: 1,
},
{
label: "Deaths",
data: deaths_list,
fill: false,
borderColor: "#f44336",
backgroundColor: "#f44336",
borderWidth: 1,
},
],
labels: formatedDates,
},
options: {
responsive: true,
maintainAspectRatio: false,
},
});
}
// FORMAT DATES
const monthsNames = [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
];
function formatDate(dateString) {
let date = new Date(dateString);
return `${date.getDate()} ${monthsNames[date.getMonth() - 1]}`;
}
function createMap(){
var modified_countries2d = [];
modified_countries2d.push(['Country', 'Confirmed Cases']);
for(i=0; i<all_countries_data.length;++i){
// var country = all_countries_data[i].name;
// var cases = all_countries_data[i].latest_data.confirmed;
// console.log(all_countries_data[i].name);
// console.log(all_countries_data[i].latest_data.confirmed);
if (all_countries_data[i].name == "USA"){
modified_countries2d.push(["US", all_countries_data[i].latest_data.confirmed]);
}
else
modified_countries2d.push([all_countries_data[i].name, all_countries_data[i].latest_data.confirmed]);
}
// for(i=0; i<modified_countries2d.length;++i){
// if (modified_countries2d[i][0] == "USA"){
// modified_countries2d[i][0] = "US";
// // countries2d.push(["US", all_countries_data[i].latest_data.confirmed]);
// }
// }
google.charts.load('current', {
'packages':['geochart'],
'mapsApiKey': 'AIzaSyB2AdWGI5geyvPnxxTPKUv6rUvbrLuK8bE'
});
google.charts.setOnLoadCallback(drawRegionsMap);
function drawRegionsMap() {
var data = google.visualization.arrayToDataTable(modified_countries2d);
var options = {
colorAxis: {colors: ['#fce9c4','#f8cb74','#f7c461','#f5b539','#dda333','#d5873f','#ac7f28','#941e40'], stroke: '#34c', strokeWidth: 130},
backgroundColor: {fill:'transparent',stroke:'#fff' ,strokeWidth:0 },
datalessRegionColor: '#F5F0E7',
displayMode: 'regions',
enableRegionInteractivity: 'true',
resolution: 'countries',
sizeAxis: {minValue: 1, maxValue:1,minSize:10, maxSize: 10},
region:'world',
keepAspectRatio: false,
tooltip: {isHtml:'true',textStyle: {color: '#444444'}, trigger:'focus'}};
var chart = new google.visualization.GeoChart(document.getElementById('regions_div'));
//To update statistics when a region is clicked
google.visualization.events.addListener(chart, "regionClick", function (eventData) {
let countrycode= eventData.region;
for (i=0;i<country_list.length;++i){
if(country_list[i].code == eventData.region){
fetchData(country_list[i].name);
}
}
})
chart.draw(data, options);
}
}
// app.txt
// Displaying app.txt.