-
Notifications
You must be signed in to change notification settings - Fork 1
/
get-chart.js
238 lines (182 loc) · 5.35 KB
/
get-chart.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
// 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";
}
var g_dates = new Array();
// Put the data onto the page
function fill_data(series) {
// Set date of latest info
var date_options = { year: 'numeric', month: 'short', day: 'numeric' };
var latest = new Date(g_dates[g_dates.length - 1]).toLocaleDateString("en-US", date_options);
document.getElementById("date").textContent = latest.toUpperCase();
// Create the chart
const chart = new JSC.Chart('chart', {
// The type of chart
type: "line",
legend_visible: true,
legend: {
position: 'bottom',
template: '%icon,%name',
},
xAxis: {
crosshair_enabled: true,
scale: { type: 'time' }
},
defaultSeries: {
defaultPoint_marker: {
type: 'circle',
size: 2,
outline: { width: 1, color: 'currentColor'}
}
},
// The data for the dataset
series: series,
// Configuration options go here
options: {}
});
}
// Wait for document load
$(document).ready(function(){
// 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 dates_arr = [];
var confirmed_arr = [];
var deaths_arr = [];
var confirmed_list = {};
var deaths_list = {};
// Loop through each country
for (n = 0; n < Object.keys(data).length; n++) {
var date_counter = 0;
// Loop through each date in country
Object.values(data)[n].forEach(({ date, confirmed, recovered, deaths }) =>
{
if (n <= 0) {
dates_arr.push(date);
confirmed_arr.push([date, confirmed]);
deaths_arr.push([date, deaths]);
} else {
confirmed_arr[date_counter] = [date, confirmed_arr[date_counter][1] + (confirmed)];
deaths_arr[date_counter] = [date, deaths_arr[date_counter][1] + (deaths)];
date_counter++;
}
}
)
}
//Data
g_dates = dates_arr.slice(0);
var series = [
{name: 'Cases', points: confirmed_arr},
{name: 'Deaths', points: deaths_arr}
];
fill_data(series);
// Handle a country region
} else if (countries.includes(loc)) {
var n = 0;
var dates_arr = [];
var confirmed_arr = [];
var deaths_arr = [];
data[loc].forEach(({ date, confirmed, recovered, deaths }) =>
{
// Count up cases and deaths
dates_arr.push(date);
confirmed_arr.push([date, confirmed]);
deaths_arr.push([date, deaths]);
}
)
//Data
g_dates = dates_arr.slice(0);
var series = [
{name: 'Cases', points: confirmed_arr},
{name: 'Deaths', points: deaths_arr}
];
fill_data(series);
// Handle state & county regions
} else {
// Check if location already found
var loc_flag = 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, ",");
var dates_arr = [];
var confirmed_arr = [];
var deaths_arr = [];
countyData.forEach(({ date, county, state, fips, cases, deaths }) =>
{
if (loc === (county + ", " + state)) {
loc_flag = true;
// Count up cases and deaths
dates_arr.push(date);
confirmed_arr.push([date, cases]);
deaths_arr.push([date, deaths]);
}
}
)
if (loc_flag) {
//Data
g_dates = dates_arr.slice(0);
var series = [
{name: 'Cases', points: confirmed_arr},
{name: 'Deaths', points: deaths_arr}
];
fill_data(series);
}
});
// 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, ",");
var dates_arr = [];
var confirmed_arr = [];
var deaths_arr = [];
stateData.forEach(({ date, state, fips, cases, deaths }) =>
{
if (loc === state) {
// Count up cases and deaths
dates_arr.push(date);
confirmed_arr.push([date, cases]);
deaths_arr.push([date, deaths]);
}
})
if (!loc_flag) {
//Data
g_dates = dates_arr.slice(0);
var series = [
{name: 'Cases', points: confirmed_arr},
{name: 'Deaths', points: deaths_arr}
];
fill_data(series);
}
});
}
});
});