-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualize_canton.html
More file actions
177 lines (165 loc) · 3.71 KB
/
visualize_canton.html
File metadata and controls
177 lines (165 loc) · 3.71 KB
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
<!DOCTYPE html>
<meta charset="utf-8">
<html>
<head>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<script src="openzh_covid19.js"></script>
</head>
<body>
<div id="plot" style="width:100%; height:600px;"></div>
<select id="canton-selector" />
<script>
var canton = "ZH";
const dataColumns = ['ncumul_conf', 'ncumul_deceased', 'current_hosp', 'current_isolated', 'current_quarantined'];
function addTrace(trace) {
let plotDiv = document.getElementById("plot");
Plotly.addTraces(plotDiv, trace);
}
function makeplot() {
getOpenZhCovid19Data(dataColumns, function(total_dates, total_data, data_column) {
if (data_column === 'ncumul_conf') {
let cantonal_data = selectCantonalData(total_data, canton);
let cantonal_data_inc = diffData(cantonal_data);
let trace = {
x: total_dates,
y: cantonal_data_inc,
name: 'Confirmed cases for ' + canton,
type: 'scatter',
mode: 'markers',
};
addTrace(trace);
let cantonal_data_avg = average(cantonal_data_inc, 7);
trace = {
x: total_dates,
y: cantonal_data_avg,
name: 'Confirmed cases averaged for ' + canton,
type: 'scatter',
mode: 'lines',
line: {
width: 1,
},
};
addTrace(trace);
}
else if (data_column === 'ncumul_deceased') {
let cantonal_data = selectCantonalData(total_data, canton);
let cantonal_data_inc = diffData(cantonal_data);
let trace = {
x: total_dates,
y: cantonal_data_inc,
name: 'Deceased cases for ' + canton,
type: 'scatter',
mode: 'markers',
};
addTrace(trace);
}
else if (data_column === 'current_hosp') {
let cantonal_data = selectCantonalData(total_data, canton);
let trace = {
x: total_dates,
y: cantonal_data,
name: 'Hospitalized cases for ' + canton,
type: 'scatter',
mode: 'lines',
};
addTrace(trace);
}
else if (data_column === 'current_isolated') {
let cantonal_data = selectCantonalData(total_data, canton);
let trace = {
x: total_dates,
y: cantonal_data,
name: 'Isolated for ' + canton,
type: 'scatter',
mode: 'lines',
line: {
dash: 'dot',
},
visible: "legendonly",
};
addTrace(trace);
}
else if (data_column === 'current_quarantined') {
let cantonal_data = selectCantonalData(total_data, canton);
let trace = {
x: total_dates,
y: cantonal_data,
name: 'Quarantined for ' + canton,
type: 'scatter',
mode: 'lines',
line: {
dash: 'dot',
},
visible: "legendonly",
};
addTrace(trace);
}
});
}
function makePlotly(){
let cantons = getCantons();
var cantonSelector = document.getElementById("canton-selector");
for (let i = 0; i < cantons.length; ++i) {
var option = document.createElement('option');
option.text = cantons[i];
cantonSelector.appendChild(option);
}
cantonSelector.value = canton;
cantonSelector.addEventListener('change', function() {
let plotDiv = document.getElementById("plot");
for (let i = 0; i < dataColumns.length + 1; ++i) {
Plotly.deleteTraces(plotDiv, 0);
}
canton = cantonSelector.value;
makeplot();
}, false);
var traces = [];
var selectorOptions = {
buttons: [
{
step: 'day',
stepmode: 'backward',
count: 14,
label: '2w'
},
{
step: 'day',
stepmode: 'backward',
count: 21,
label: '3w'
},
{
step: 'month',
stepmode: 'backward',
count: 1,
label: '1m'
},
{
step: 'month',
stepmode: 'backward',
count: 2,
label: '2m'
},
{
step: 'all',
}],
};
var layout = {
title: 'Cantonal data',
xaxis: {
rangeselector: selectorOptions,
rangeslider: {
visible: true,
},
},
yaxis: {
fixedrange: false,
},
};
Plotly.newPlot('plot', traces, layout);
}
makePlotly();
makeplot();
</script>
</body>
</html>