-
Notifications
You must be signed in to change notification settings - Fork 17
/
index.html
345 lines (301 loc) · 11.8 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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
<!DOCTYPE html>
<html lang="de" id="top">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta content="width=device-width, initial-scale=1.0" name="viewport">
<title>Risikogebiete Deutschland</title>
<meta name="description" content="Risikogebiete Deutschland: Deutschland Karte der Covid-19-Inzidenzen, aufgeschlüsselt nach Altersgruppen, Landkreisen, und Datum." />
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate"/>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="expires" content="-1">
<!-- Bootstrap CSS -->
<link href="assets/css/bootstrap/bootstrap.min.css" rel="stylesheet">
<!-- Master CSS -->
<link href="assets/css/main.css" rel="stylesheet">
<!-- Projection -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/proj4js/2.5.0/proj4.js"></script>
<!-- D3 js-->
<script src="https://d3js.org/d3.v7.min.js"></script>
<!-- HDF5 js-->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/hdf5.js"></script>
<!-- Own utils-->
<script src="assets/js/utils.js"></script>
</head>
<body>
<!-- Spinning loader -->
<div id="loader" class="center"></div>
<!-- Element for the map-->
<div id="map__container" style="width: 100%; height:100vh; margin: auto;" align="center"></div>
<!-- Slider for iterating days-->
<div id="inputs" style="width: max-content;">
<div class="accordion active" collapsed-header="Selektion">
<h5>Altersgruppen</h5> <img class="pm" style="height: 1em; margin: auto 3px auto 3px;"src="assets/img/minus.png" alt="">
</div>
<div class="panel">
<ul>
<li class="radio-inline"><input id="total" type="radio" name="optradio" checked="">Alle</li>
<li class="radio-inline"><input id="A00-A04" type="radio" name="optradio"><label for="A00-A04">A00-A04</label></li>
<li class="radio-inline"><input id="A05-A14" type="radio" name="optradio"><label for="A05-A14">A05-A14</label></li>
<li class="radio-inline"><input id="A15-A34" type="radio" name="optradio"><label for="A15-A34">A15-A34</label></li>
<li class="radio-inline"><input id="A35-A59" type="radio" name="optradio"><label for="A35-A59">A35-A59</label></li>
<li class="radio-inline"><input id="A60-A79" type="radio" name="optradio"><label for="A60-A79">A60-A79</label></li>
<li class="radio-inline"><input id="A80+" type="radio" name="optradio"><label for="A80+">A80+</label></li>
</ul>
<input type="checkbox" id="change_color" style="border-radius: 0px;"><label for="change_color">Alte Farben</label>
<hr style="margin-top: 0.25em; margin-bottom: 0.25em;">
<h5>Datum</h5>
<input type="range" step="86400" style="display: inline-block;" id="slider_days">
<div id="date"></div>
<hr style="margin-top: 0.25em; margin-bottom: 0.25em;">
<a href="./legacy.html">Alte Website</a><br>
<a href="https://github.com/semohr/risikogebiete_deutschland">Source-code & Daten</a>
</div>
</div>
<div id="legend" style="width max-content;">
<div class="accordion active" collapsed-header="Legende">
<h5>Fälle/100.000 EW<br>in der jeweiligen<br>Altersgruppe</h5> <img class="pm" style="height: 1em; margin: auto 3px auto 3px;"src="assets/img/minus.png" alt="">
</div>
<div id="legend-items" class="grid-container panel">
</div>
</div>
</body>
<script>
/* ------------------------------------------------------------
* Global variables
* ------------------------------------------------------------ */
// Colormap
var classes = [25,50,100,200,500,1000,2000,3000]
var colors = ['#15b01a', '#f0f921', '#fdb42f', '#ed7953', '#cb4679', '#9c179e', '#5c01a6',"#0d0887"];
var old_colors = ["#15b01a","#fac205","#f97c0e","#c50000","#8c53d1","#4d00fe","#1f00cc","#00009c","#303030"];
var color = d3.scaleLinear()
.domain(classes)
.range(colors);
// Add colorbar and annotations to legend
window.addEventListener("DOMContentLoaded", function() {
add_colorbar_and_annotations("legend-items", colors, classes);
});
// Data
var svg, strides, incidence, weekly_cases, index, geojson, date, dt_idx, path;
var age_group = "total";
var formatDate = d3.timeFormat("%d.%m.%g");
var age_groups = ["A00-A04","A05-A14","A15-A34","A35-A59","A60-A79","A80+"];
// Setup tooltip div
var tooltip = d3.select("#map__container")
.append("div")
.style("visibility", "hidden")
.attr("class", "data_tooltip")
.html("");
/* Draw the map to an div container with id "map__container"
*/
function setup_svgmap(){
// Get window size
let height = window.innerHeight;
var width = window.innerWidth;
// Create svg inside map container
svg = d3
.select("#map__container")
.append("svg")
.style("display", "block")
.attr("width", width)
.attr("height", height);
// Define identity projection
// Own projection is done beforehand (see data loading)
let padding = 10;
var projection = d3.geoIdentity().reflectY(true).fitHeight(height-padding*2,geojson);
let translate = projection.translate();
projection.translate([translate[0], translate[1] + padding]);
path = d3.geoPath(projection);
// Draw the map as svg
let map = svg.append("g")
map.selectAll("path")
.data(geojson.features)
.enter()
.append("path")
.attr("d",path)
.attr("fill", "white")
.attr("stroke", "gray")
.attr("stroke-width","0.8")
.on("mouseover",mouseoverHandler)
.on("mouseout",mouseoutHandler)
.on("mousemove",mousemoveHandler)
// Select initial data
dates = Object.keys(index.date)
start = dates[0]
end = dates[dates.length - 1]
d3.select("#slider_days").attr('min', start).attr('max',end).property("value",end).dispatch("input");
// Setup inputs to trigger transitions
var inputs = document.querySelectorAll("input[type='radio']")
for (input of inputs){
input.onclick = function(e){
age_group = e.target.id;
triggerTransition();
}
}
//Set viewbox
svg.attr("viewBox", [0-width/2+svg.node().getBBox().width/2,0,width,height])
// Set zoom handler
const handleZoom = (e) => map.attr('transform', e.transform);
const zoom = d3.zoom().on('zoom', handleZoom);
zoom
.scaleExtent([0.5, 8])
.extent([[0, 0], [width, height]]);
d3.select('svg').call(zoom);
// Change color to selected box
onclick_color_change();
}
function mouseoverHandler(d){
let this_ele = d3.select(this)
// Save old color
this_ele.attr("old_color",this_ele.attr("fill"));
// Change color to lighter hue
this_ele.attr("fill",pSBC(0.3,this_ele.attr("old_color")));
tooltip.interrupt();
tooltip.style("visibility", "visible");
tooltip.html(tooltipText(this));
}
function mouseoutHandler(d){
// Restore old color
d3.select(this).attr("fill",d3.select(this).attr("old_color"));
tooltip.transition().delay(1000).style("visibility", "hidden");
}
function mousemoveHandler(d){
r = tooltip.node().getBoundingClientRect();
tooltip.style("left", (svg.node().getBoundingClientRect().left+d3.pointer(d,window)[0])+"px")
.style("top", (Math.max(svg.node().getBoundingClientRect().top+d3.pointer(d,window)[1]-r.height,0))+"px");
}
function tooltipText(element){
str = `
<h5> ${element.__data__.properties["BEZ"]} ${element.__data__.properties["GEN"]} </h5>
<span style="display:flex;">${formatDate(date*1000)}</span>
<hr style="margin:0">
<table>
<tr>
<th style="text-align: left; padding-right:10px;">Altersgruppe</th>
<th style="text-align: right;padding-left: 10px">Fälle der letzte Woche</th>
<th style="text-align: right;padding-left: 10px">pro 100.000 EW</th>
</tr>
`;
lk_idx = index["IdLandkreis"][parseInt(element.__data__.properties.AGS)]
ag_idx = index["Altersgruppe"]["total"];
i = dt_idx * strides[0] + lk_idx * strides[1] + ag_idx * strides[2];
str +=`
<tr>
<td>Alle</td>
<td style="text-align: right;">${weekly_cases[i]}</td>
<td style="text-align: right;">${incidence[i].toFixed(0)}</td>
</tr>
`;
for (a of age_groups) {
ag_idx = index["Altersgruppe"][a];
i = dt_idx * strides[0] + lk_idx * strides[1] + ag_idx * strides[2];
str +=`
<tr>
<td>${a}</td>
<td style="text-align: right;">${weekly_cases[i]}</td>
<td style="text-align: right;">${incidence[i].toFixed(0)}</td>
</tr>
`;
}
str += "</table>";
return str;
}
/* Update geojsons data for timeseries plotting
*/
function update_geojson(date, age_group){
dt_idx = index["date"][date];
ag_idx = index["Altersgruppe"][age_group];
i = dt_idx*strides[0]+ag_idx*strides[2]
geojson.features.forEach(el => {
const linkedData = incidence[index["IdLandkreis"][parseInt(el.properties.AGS)]*strides[1]+i];
el.properties.index_data = linkedData;
});
}
function triggerTransition(){
update_geojson(date,age_group);
d3.select("#date").html(formatDate(date*1000));
let test = d3.select("svg")
.selectAll("path")
.attr("fill", d => {
return color(d.properties.index_data)
});
}
//Add event to slider
document.getElementById("slider_days").addEventListener("input", throttle(function() {
date = document.getElementById("slider_days").value;
triggerTransition();
},0));
function change_color(co,cl){
color = d3.scaleLinear()
.domain(cl)
.range(co);
// Update legend
var linearGradient = d3.select("#linear-gradient");
//Set the color for the start (0%)
linearGradient.selectAll("stop")
.data(co)
.attr("offset",(d,i)=>{
return i/co.length*100 + 1/co.length*50+"%";
})
.attr("stop-color",d=>{return d});
triggerTransition();
}
function onclick_color_change(){
var input = document.getElementById("change_color");
if (input.checked){
change_color(old_colors,classes)
}
else{
change_color(colors,classes)
}
}
//Add event to recolor
document.getElementById("change_color").addEventListener("click",onclick_color_change)
// Load geojson file and fetch timeseries
Promise.all([
d3.json("assets/data/landkreise_edited.geojson")
.then((r)=>{
// Own projection
project(
r,
'+proj=merc +lat_1=33 +lat_2=45 +lat_0=0 +lon_0=0'
);
return r
}),
d3.json("assets/data/array_index.json")
.then((r)=>{
index = r;
return index
}),
fetch("assets/data/timeseries_nd.hdf5")
.then((r)=>{
return r.arrayBuffer()
})
.then((r)=>{
return new hdf5.File(r)
})
.then((f)=>{
incidence = f.get('data/incidence').value;
weekly_cases = f.get('data/weekly_cases').value;
strides = []
s = 1
f.get('data/incidence').shape.reverse().forEach(function(item) {strides.push(s);s=s*item;});
strides = strides.reverse();
})
]).then((data)=>{
// Plotting setup
geojson = data[0];
setup_svgmap();
document.getElementById("loader").style.display = "none";
//Get active age group
document.querySelectorAll("input[type='radio']:checked")[0].click();
});
window.addEventListener("resize", function(){
let width_container = document.getElementById("map__container").clientWidth;
let height_container = document.getElementById("map__container").clientHeight;
d3.select("svg")
.attr("width",width_container)
.attr("height",(d) => height_container);
});
</script>
</html>