-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathglobal.js
73 lines (68 loc) · 1.98 KB
/
global.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
function global_data1() {
document.getElementById("statelist").style.display = "none";
document.getElementById("header3").style.display = "none";
document.getElementById("extra").style.display = "none";
function makechart(x, y) {
var ctx = document.getElementById("c1").getContext("2d");
var chart = new Chart(ctx, {
type: "horizontalBar",
data: {
labels: x,
datasets: [
{
data: y,
label: "COVID-19 Cases",
backgroundColor: "#e420208f",
borderColor: "#cd5c5c",
},
],
},
options: {
responsive: true,
maintainAspectRatio: false,
title: {
display: true,
text: "TOP 10 COUNTRIES BY CASES",
},
},
});
return chart;
}
//this is the javascript file to show the graphs
//the ajax part(to get response fromm the api)
var myxml = new XMLHttpRequest();
myxml.onreadystatechange = function () {
if (this.status == 200 && this.readyState == 4) {
var base = JSON.parse(this.response).locations;
base.sort(function (a, b) {
return b.latest.confirmed - a.latest.confirmed;
});
var l = base.map(function (x) {
return x.country;
});
var label = l.slice(0, 10);
var d = base.map(function (x) {
return x.latest.confirmed;
});
var data = d.slice(0, 10);
//alert(typeof(currchart1));
if (typeof currchart1 === "undefined") {
currchart1 = makechart(label, data);
} else {
currchart1.destroy();
currchart1 = makechart(label, data);
}
} else {
document.getElementById("c1").innerHTML =
"ERROR CONNECTING TO API. KINDLY REFRESH THE PAGE!" +
" HTTP code:" +
this.status;
}
};
myxml.open(
"GET",
"https://coronavirus-tracker-api.herokuapp.com/v2/locations",
true
);
myxml.send();
}