-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
145 lines (109 loc) · 5.65 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
const submitBtn = document.querySelector('.submit');
var countryOne = document.getElementById("countryOne");
var countryTwo = document.getElementById("countryTwo");
const mainDiv = document.querySelector('.main-container');
const dataDiv = document.querySelector('.data-div');
const recoveryBtn = document.querySelector('.recov');
const deathBtn = document.querySelector('.deaths');
const title = document.querySelector(".compare-title");
var textOne = document.querySelector('.countryOneCases');
var textTwo = document.querySelector('.countryTwoCases');
const chart = document.getElementById("myChart");
const noChart = document.querySelector(".no-chart");
var url = "https://api.covid19api.com/country/";
var data = [];
submitBtn.addEventListener("click", () => {
title.textContent = countryOne.value + " V.S " + countryTwo.value;
mainDiv.style.display = "none";
dataDiv.style.display = "flex";
var data = [];
async function getData() {
const f1_data = await fetch(url + countryOne.value); // wait for fetched url
const f1_json = await f1_data.json(); // wait for json data, turn data into json data to retrieve confirmed cases
var indexOne = f1_json.length - 1; //get the last array (most recent data)
textOne.textContent = f1_json[indexOne].CountryCode + " TOTAL CASES: " + f1_json[indexOne].Confirmed; //get country code and confirmed
data.push(f1_json[indexOne].Confirmed); //push confirmed cases to data array
const f2_data = await fetch(url + countryTwo.value);
const f2_json = await f2_data.json(); //turn data into json
var indexTwo = f2_json.length - 1; //get the last array (most recent data)
textTwo.textContent = f2_json[indexTwo].CountryCode + " TOTAL CASES: " + f2_json[indexTwo].Confirmed; //country code and confirmed
data.push(f2_json[indexTwo].Confirmed); //push confirmed cases to data array
};
async function chartIt() {
await getData(); //wait for getData function
var ctx = document.getElementById('myChart').getContext('2d');
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: [countryOne.value, countryTwo.value],
datasets: [{
label: 'Case Comparison',
data: data,
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
],
borderColor: [
'rgba(255, 99, 132, 1)',
'rgba(54, 162, 235, 1)',
],
borderWidth: 1
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}],
xAxes: [{
// Change here
barPercentage: 0.5
}]
}
}
})
}
chartIt();
})
var recoveredData = [];
recoveryBtn.addEventListener("click", () => {
dataDiv.style.height = "100vh";
chart.style.display = "none";
noChart.style.display = "block";
async function getRecovered() {
const f1_data = await fetch(url + countryOne.value); // wait for fetched url
const f1_json = await f1_data.json(); // wait for json data, turn data into json data to retrieve confirmed cases
var indexOne = f1_json.length - 1; //get the last array (most recent data)
textOne.textContent = f1_json[indexOne].CountryCode + " TOTAL RECOVERIES: " + f1_json[indexOne].Recovered; //get country code and confirmed
recoveredData.push(f1_json[indexOne].Recovered); //push confirmed cases to data array
const f2_data = await fetch(url + countryTwo.value);
const f2_json = await f2_data.json(); //turn data into json
var indexTwo = f2_json.length - 1; //get the last array (most recent data)
textTwo.textContent = f2_json[indexOne].CountryCode + " TOTAL RECOVERIES: " + f2_json[indexOne].Recovered; //get country code and confirmed
recoveredData.push(f2_json[indexTwo].Recovered); //push confirmed cases to data array
console.log(recoveredData);
}
getRecovered();
})
var deathData = []
deathBtn.addEventListener("click", () => {
dataDiv.style.height = "100vh";
chart.style.display = "none";
noChart.style.display = "block";
async function getDeath() {
const f1_data = await fetch(url + countryOne.value); // wait for fetched url
const f1_json = await f1_data.json(); // wait for json data, turn data into json data to retrieve confirmed cases
var indexOne = f1_json.length - 1; //get the last array (most recent data)
textOne.textContent = f1_json[indexOne].CountryCode + " TOTAL DEATHS: " + f1_json[indexOne].Deaths; //get country code and confirmed
deathData.push(f1_json[indexOne].Deaths); //push confirmed cases to data array
const f2_data = await fetch(url + countryTwo.value);
const f2_json = await f2_data.json(); //turn data into json
var indexTwo = f2_json.length - 1; //get the last array (most recent data)
textTwo.textContent = f2_json[indexTwo].CountryCode + " TOTAL DEATHS: " + f2_json[indexTwo].Deaths; //get country code and confirmed
deathData.push(f2_json[indexTwo].Deaths); //push confirmed cases to data array
console.log(deathData);
}
getDeath();
})