-
Notifications
You must be signed in to change notification settings - Fork 1
/
get-data.js
163 lines (127 loc) · 4.02 KB
/
get-data.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
// Get the URL parameter (i.e. region)
var $_GET = {};
if (document.location.toString().indexOf('?') !== -1) {
var query = document.location
.toString()
// Get the query string
.replace(/^.*?\?/, '')
// Remove any existing hash string
.replace(/#.*$/, '')
.split('&');
for(var i=0, l=query.length; i<l; i++) {
var aux = decodeURIComponent(query[i]).split('=');
$_GET[aux[0]] = aux[1];
}
}
var loc = $_GET['in'];
// Show worldwide data by default
if (typeof(loc) === "undefined") {
loc = "Worldwide";
}
// Put the data onto the page
function fill_data(date, cases, deaths) {
document.getElementById("date").textContent = date.toUpperCase();
document.getElementById("confirmed").textContent = cases.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
document.getElementById("deaths").textContent = deaths.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
// Wait for document load
$(document).ready(function(){
// Date format options
var date_options = { year: 'numeric', month: 'short', day: 'numeric' };
// Set the region/location text
document.getElementById("region").textContent = loc.toUpperCase();
// Fetch country data from Rodrigo Pombo & Johns Hopkins CSSE
fetch('https://pomber.github.io/covid19/timeseries.json')
.then(response => response.json())
.then(data => {
// List of countries
var countries = [];
for (n = 0; n < Object.keys(data).length; n++) {
countries.push(Object.keys(data)[n]);
}
// Handle the worldwide region
if (loc === "Worldwide") {
var total_date;
var total_confirmed = 0;
var total_deaths = 0;
for (n = 0; n < Object.keys(data).length; n++) {
var subtotal_confirmed = 0;
var subtotal_deaths = 0;
Object.values(data)[n].forEach(({ date, confirmed, recovered, deaths }) =>
{
total_date = date;
subtotal_confirmed = confirmed;
subtotal_deaths = deaths;
}
)
// Count up cases and deaths
total_confirmed += subtotal_confirmed;
total_deaths += subtotal_deaths;
}
//Data
fill_data(
new Date(total_date).toLocaleDateString("en-US", date_options),
total_confirmed,
total_deaths
);
// Handle a country region
} else if (countries.includes(loc)) {
data[loc].forEach(({ date, confirmed, recovered, deaths }) =>
{
// Display data
if (loc === "US") {
document.getElementById("region").textContent = "UNITED STATES";
}
// Data
fill_data(
new Date(date).toLocaleDateString("en-US", date_options),
confirmed,
deaths
);
})
// Handle state & county regions
} else {
// Flag for determined undefined region/location
var locFlag = false;
// Fetch & check county data from NY Times
JSC.fetch('https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-counties.csv')
.then(response => response.text())
.then(data => {
let countyData = JSC.csv2Json(data, ",");
countyData.forEach(({ date, county, state, fips, cases, deaths }) =>
{
if (loc === (county + ", " + state)) {
locFlag = true;
// Data
fill_data(
new Date(date).toLocaleDateString("en-US", date_options),
cases,
deaths
);
}
})
});
// Fetch & check state data from NY Times
JSC.fetch('https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-states.csv')
.then(response => response.text())
.then(data => {
let stateData = JSC.csv2Json(data, ",");
stateData.forEach(({ date, state, fips, cases, deaths }) =>
{
if (loc === state) {
locFlag = true;
// Data
fill_data(
new Date(date).toLocaleDateString("en-US", date_options),
cases,
deaths
);
}
})
});
if (!locFlag) {
fill_data(new Date().toLocaleDateString("en-US", date_options), "Unreported", "Unreported")
}
}
});
});