-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
250 lines (216 loc) · 6.26 KB
/
index.html
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
<!doctype html>
<html>
<head>
<title>Corona Line Chart by country</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.css"></link>
<link rel="stylesheet" href="lib/css/choices.min.css" />
<script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.bundle.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script>
<!-- <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js" integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU=" crossorigin="anonymous"></script>
-->
<script src="lib/js/choices.min.js"></script>
<style>
canvas {
-moz-user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
}
</style>
</head>
<body>
<div class="container">
<div class="row my-3">
<div class="col">
<h4>Corona Line Chart by country</h4>
</div>
</div>
<div class="row my-2">
<div class="col-md-12">
<div class="card">
<div class="card-body">
<canvas id="canvas"></canvas>
</div>
<div class="card-title"></div>
<select
class="form-control"
data-trigger
name="choices-multiple"
id="choices-multiple"
placeholder="This is a placeholder"
multiple
>
</select>
</div>
</div>
</div>
</div>
<script>
var timeFormat = 'MM/DD/YY';
var datasets = [];
var config = {
type: 'line',
data: {
datasets: datasets
},
options: {
responsive: true,
hoverMode: 'index',
stacked: false,
title: {
display: true,
text: "Corona Line Chart by country"
},
tooltips: {
mode: 'index'
},
scales: {
xAxes: [{
type: "time",
time: {
parse: timeFormat,
tooltipFormat: 'll'
},
scaleLabel: {
display: true,
labelString: 'Date'
}
}],
yAxes: [{
scaleLabel: {
display: true,
labelString: 'confirmed cases'
}
}]
}
}
};
// Get the context of the canvas element we want to select
var ctx = document.getElementById("canvas").getContext("2d");
// Instantiate a new chart
var myLineChart = new Chart(ctx, config);
const element = document.getElementById('choices-multiple');
const multipleFetch = new Choices(element, {
placeholder: false,
removeItemButton: true,
});
function addCountry(countryCode) {
fetch('https://coronavirus-tracker-api.herokuapp.com/v2/locations?country_code=' + countryCode + '&timelines=1')
.then(res => res.json())
.then((results) => {
var dataset = {
label: countryCode,
lineTension: 0,
fill: false,
borderColor: getRandomColor()
};
var mergedResult = [];
for(const location of results.locations) {
Object.keys(location.timelines.confirmed.timeline).forEach(function(key) {
mergedResult.push({
x: key,
y: location.timelines.confirmed.timeline[key]});
})
}
var dataPoints = aggregate(mergedResult, "x", "y", add);
var dataset = {
label: countryCode,
data: dataPoints,
lineTension: 0,
fill: false,
borderColor: getRandomColor()
};
addData(myLineChart, dataset)
return dataset;
})
.catch(err => { throw err });
}
function aggregate(object, toGroup, toAggregate, fn, val0) {
function deepFlatten(x) {
if ( x[toGroup] !== undefined ) // Leaf
return x;
return _.chain(x)
.map(function(v) { return deepFlatten(v); })
.flatten()
.value();
}
return _.chain(deepFlatten(object))
.groupBy(toGroup)
.map(function(g, key) {
return {
x: key,
y: _(g).reduce(function(m, x) {
return fn(m, x[toAggregate]);
}, val0 || 0)
};
})
.sortBy(toGroup)
.value();
}
function add(a,b) { return a + b; }
function getRandomColor() {
var letters = '0123456789ABCDEF';
var color = '#';
for (var i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
function drawCountryDropdown() {
var countrysToSet = [ "CH", "DE", "FR", "ES", "GB", "US"]
element.addEventListener(
'addItem',
function(event) {
addCountry(event.detail.value);
}
);
element.addEventListener(
'removeItem',
function(event) {
removeData(myLineChart, event.detail.value)
}
);
multipleFetch.setChoices(async () => {
return fetch(
'https://coronavirus-tracker-api.herokuapp.com/v2/locations'
)
.then(function(response) {
return response.json();
})
.then(function(data) {
countrys = _.chain(data.locations)
.groupBy('country_code')
.map(function(value, key) {
return {
country_code: key,
country: value[0].country
}
})
.value();
return countrys.map(function(release) {
if (countrysToSet.includes(release.country_code)) {
return { value: release.country_code, label: release.country, selected: true };
} else {
return { value: release.country_code, label: release.country };
}
});
});
});
}
function addData(chart, countryCode) {
chart.data.datasets.push(countryCode);
chart.update();
}
function removeData(chart, countryCode) {
chart.data.datasets = chart.data.datasets.filter(function(obj) {
return (obj.label != countryCode);
});
chart.update();
}
drawCountryDropdown();
</script>
</body>
</html>