-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
283 lines (242 loc) · 9.6 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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<head>
<link rel="icon" href="vaccine.png">
<title>Como vai a vacina?</title>
</head>
<style>
#chartdiv {
width: 100%;
height: 500px;
}
h1 {
text-align: center;
font-size: 4em;
color: black;
font-weight: 700;
}
</style>
<!-- Resources -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js" type="text/javascript"></script>
<script src="https://cdn.amcharts.com/lib/4/core.js"></script>
<script src="https://cdn.amcharts.com/lib/4/charts.js"></script>
<script src="https://cdn.amcharts.com/lib/4/themes/animated.js"></script>
<script src="https://cdn.amcharts.com/lib/4/themes/dark.js"></script>
<!-- Chart code -->
<script>
const VACCINATION_TARGET = 10155650; // +- Population of Portugal
const HERD_IMMUNITY_PERCENTAGE = 0.7; // 70%
function parseCSV(str) {
var arr = [];
var quote = false; // 'true' means we're inside a quoted field
// Iterate over each character, keep track of current row and column (of the returned array)
for (var row = 0, col = 0, c = 0; c < str.length; c++) {
var cc = str[c], nc = str[c+1]; // Current character, next character
arr[row] = arr[row] || []; // Create a new row if necessary
arr[row][col] = arr[row][col] || ''; // Create a new column (start with empty string) if necessary
// If the current character is a quotation mark, and we're inside a
// quoted field, and the next character is also a quotation mark,
// add a quotation mark to the current column and skip the next character
if (cc == '"' && quote && nc == '"') { arr[row][col] += cc; ++c; continue; }
// If it's just one quotation mark, begin/end quoted field
if (cc == '"') { quote = !quote; continue; }
// If it's a comma and we're not in a quoted field, move on to the next column
if (cc == ',' && !quote) { ++col; continue; }
// If it's a newline (CRLF) and we're not in a quoted field, skip the next character
// and move on to the next row and move to column 0 of that new row
if (cc == '\r' && nc == '\n' && !quote) { ++row; col = 0; ++c; continue; }
// If it's a newline (LF or CR) and we're not in a quoted field,
// move on to the next row and move to column 0 of that new row
if (cc == '\n' && !quote) { ++row; col = 0; continue; }
if (cc == '\r' && !quote) { ++row; col = 0; continue; }
// Otherwise, append the current character to the current column
arr[row][col] += cc;
}
console.log(arr);
return arr;
}
function getTwoDoseVaccinations(csv) {
return parseInt(csv.slice().reverse().find(elem => elem[7] !== "")[7]);
}
am4core.ready(function() {
// Themes begin
am4core.useTheme(am4themes_animated);
am4core.useTheme(am4themes_dark);
// Themes end
var chartMin = 0;
var chartMax = 100;
var data = {
score: 0,
gradingData: [
{
title: "",
color: "#ee1f25",
lowScore: -1,
highScore: 30
},
{
title: "",
color: "#f04922",
lowScore: 30,
highScore: 50
},
{
title: "",
color: "#fdae19",
lowScore: 50,
highScore: 70
},
{
title: "Imunidade de Grupo",
color: "#0f9747",
lowScore: 70,
highScore: 100
}
]
};
/**
Grading Lookup
*/
function lookUpGrade(lookupScore, grades) {
console.log(lookupScore);
console.log(grades);
// Only change code below this line
for (var i = 0; i < grades.length; i++) {
if (
grades[i].lowScore < lookupScore &&
grades[i].highScore >= lookupScore
) {
return grades[i];
}
}
return null;
}
// create chart
var chart = am4core.create("chartdiv", am4charts.GaugeChart);
chart.hiddenState.properties.opacity = 0;
chart.fontSize = 11;
chart.innerRadius = am4core.percent(80);
chart.resizable = true;
/**
* Normal axis
*/
var axis = chart.xAxes.push(new am4charts.ValueAxis());
axis.min = chartMin;
axis.max = chartMax;
axis.strictMinMax = true;
axis.renderer.radius = am4core.percent(80);
axis.renderer.inside = true;
axis.renderer.line.strokeOpacity = 0.1;
axis.renderer.ticks.template.disabled = false;
axis.renderer.ticks.template.length = 5;
axis.renderer.grid.template.disabled = true;
axis.renderer.labels.template.radius = am4core.percent(15);
axis.renderer.labels.template.fontSize = "0.9em";
/**
* Axis for ranges
*/
var axis2 = chart.xAxes.push(new am4charts.ValueAxis());
axis2.min = chartMin;
axis2.max = chartMax;
axis2.strictMinMax = true;
axis2.renderer.labels.template.disabled = false;
axis2.renderer.ticks.template.disabled = true;
axis2.renderer.grid.template.disabled = false;
axis2.renderer.grid.template.stroke = "#444444";
axis2.renderer.grid.template.strokeOpacity = 0.5
axis2.renderer.labels.template.bent = true;
axis2.renderer.labels.template.fill = am4core.color("#000");
axis2.renderer.labels.template.fontWeight = "bold";
axis2.renderer.labels.template.fontSize = "1.5em";
axis2.renderer.labels.template.fillOpacity = 0.8;
/**
Ranges
*/
for (let grading of data.gradingData) {
var range = axis2.axisRanges.create();
range.axisFill.fill = am4core.color(grading.color);
range.axisFill.fillOpacity = 0.8;
range.axisFill.zIndex = -1;
range.value = grading.lowScore > chartMin ? grading.lowScore : chartMin;
range.endValue = grading.highScore < chartMax ? grading.highScore : chartMax;
range.label.text = grading.title.toUpperCase();
range.label.inside = true;
range.label.location = 0.5;
range.label.radius = am4core.percent(10);
range.label.paddingBottom = -5; // ~half font size
range.label.fontSize = "2em";
}
var matchingGrade = lookUpGrade(data.score, data.gradingData);
/**
* Label 1
*/
var label = chart.radarContainer.createChild(am4core.Label);
label.isMeasured = false;
label.fontSize = "6em";
label.x = am4core.percent(50);
label.paddingBottom = 15;
label.horizontalCenter = "middle";
label.verticalCenter = "bottom";
label.text = data.score.toFixed(1);
label.fill = am4core.color(matchingGrade.color);
/**
* Label 2
*/
var label2 = chart.radarContainer.createChild(am4core.Label);
label2.isMeasured = false;
label2.fontSize = "2em";
label2.horizontalCenter = "middle";
label2.verticalCenter = "bottom";
label2.text = matchingGrade.title.toUpperCase();
label2.fill = am4core.color(matchingGrade.color);
/**
* Hand
*/
var hand = chart.hands.push(new am4charts.ClockHand());
hand.axis = axis2;
hand.innerRadius = am4core.percent(55);
hand.startWidth = 8;
hand.pin.disabled = true;
hand.value = data.score;
hand.fill = am4core.color("#444");
hand.stroke = am4core.color("#000");
hand.events.on("positionchanged", function(){
label.text = axis2.positionToValue(hand.currentPosition).toFixed(1) + "%";
var value2 = axis.positionToValue(hand.currentPosition);
var matchingGrade = lookUpGrade(axis.positionToValue(hand.currentPosition), data.gradingData);
label2.text = matchingGrade.title.toUpperCase();
label2.fill = am4core.color(matchingGrade.color);
label2.stroke = am4core.color(matchingGrade.color);
label.fill = am4core.color(matchingGrade.color);
})
$.ajax({
url: 'https://raw.githubusercontent.com/dssg-pt/covid19pt-data/master/vacinas.csv',
crossDomain: true,
dataType: 'text',
}).done(function(data) {
var csv = parseCSV(data);
var vaccinations = getTwoDoseVaccinations(csv);
setTimeout(() => {
hand.showValue(
(vaccinations / VACCINATION_TARGET) * 100.0,
1000,
am4core.ease.cubicOut
);
}, 500);
}).fail(function() {
alert( "error" );
});
}); // end am4core.ready()
</script>
<!-- HTML -->
<div />
<div class="container">
<div id="header" class="row" style="color:#000000; padding-top: 0.5em; padding-bottom: 1em;">
<h1>Como está a vacinação em Portugal?</h1>
</div>
<div id="chartdiv"></div>
<div style="padding-left: 1em;">
<p>* Apenas são contabilizados cidadãos que estão totalmente vacinados.</p>
<a style="font-weight: 700;">Desenvolvido por</a>
<br>
<a style="font-size:1.1em; font-weight: 700;">Bruno Calapez Ponte</a>
</div>
</div>