-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplz.html
174 lines (152 loc) · 5.29 KB
/
plz.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Corona-Fälle im Kanton ZH nach PLZ</title>
<!-- Load d3.js -->
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="js/d3.v4.js"></script>
<script src="js/d3-scale-chromatic.v1.min.js"></script>
<script src="js/d3-geo-projection.v2.min.js"></script>
<script src="js/d3-tip.js"></script>
<style>
.d3-tip {
line-height: 1;
padding: 12px;
background: rgba(43,43,43, 0.8);
color: #fff;
border-radius: 2px;
}
.plz:hover{
opacity: .6;
}
</style>
</head>
<body>
<!-- Create an element where the map will take place -->
<h2 id="toptitle">Neue Covid-Fälle im Kanton Zürich nach PLZ (letzte 7 Tage)</h2>
<svg id="my_dataviz" width="800" height="800"></svg>
</body>
<script>
var tip = d3.tip()
.attr('class', 'd3-tip')
.html(function(d) {
var plz = ""+d.properties.postleitzahl;
var filtered = data.filter(function(d) { if(d.PLZ==plz) return d});
var cases = '';
var population = '';
if(filtered.length>0) {
var lastFiltered = filtered[filtered.length-1];
cases = `${lastFiltered.NewConfCases_7days}`;
population = lastFiltered.Population.toString().replace(/\B(?=(\d{3})+(?!\d))/g, "’");;
}
else cases = '0';
return "<strong>"+d.properties.postleitzahl+" " + d.properties.ortbez27 + "</strong><br/>Neue Fälle: "+cases+"<br/>Einwohnerzahl: "+population;
});
// The svg
var svg = d3.select("svg"),//.style("background-color", 'red'),
width = +svg.attr("width"),
height = +svg.attr("height");
svg.call(tip);
var smaller = width<height ? width : height;
// Map and projection
var projection = d3.geoMercator()
.center([8.675, 47.43]) // GPS of location to zoom on
.scale(40000*(smaller/600)) // This is like the zoom
.translate([ width/2, height/2 ])
// Load external data and boot
//d3.json("https://raw.githubusercontent.com/holtzy/D3-graph-gallery/master/DATA/world.geojson", function(data){
var topodata, data;
d3.queue()
.defer(d3.json, "plz.geojson")
.defer(d3.csv, "https://raw.githubusercontent.com/openZH/covid_19/master/fallzahlen_plz/fallzahlen_kanton_ZH_plz.csv")
//.defer(d3.csv, "https://raw.githubusercontent.com/openZH/covid_19/master/fallzahlen_bezirke/fallzahlen_kanton_ZH_bezirk.csv")
.await(ready);
function ready(error, topo, csvdata) {
// Filter data
//data.features = data.features.filter(function(d){console.log(d.properties.name) ; return d.properties.name=="Switzerland"})
data = csvdata;
// console.log("Old length: "+topo.features.length);
// var filteredTopo = topo.features.filter(function(d) {if(d.geometry!=null) return d});
// topo.features = filteredTopo;
// console.log("New length: "+topo.features.length);
var lastDate = csvdata[csvdata.length-1].Date;
var h2 = document.getElementById("toptitle");
h2.innerHTML = h2.innerHTML + " ("+lastDate+")";
topodata = topo;
console.log(topo);
draw();
}
var week = 22;
const path = d3.geoPath().projection(projection);
function draw() {
svg.selectAll("*").remove();
// Draw the map
svg.append("g")
.selectAll("path")
.data(topodata.features)
.enter()
.append("path")
.attr('fill', function(d,i) {
var plz = ""+d.properties.postleitzahl;
var filtered = data.filter(function(d) { if(d.PLZ==plz) return d});
if(filtered.length>0 && filtered[filtered.length-1].NewConfCases_7days != "0-3") return "green";
return "grey";
})
.attr("class", "plz")
.attr("d", path)
.style("stroke", "white")
.on('mouseover', tip.show)
.on('mouseout', tip.hide)
// .on("mouseover", mouseOverHandler)
// .on("mouseout", mouseOutHandler)
//.on("click", clickHandler);
svg.append("g")
.selectAll("text")
.data(topodata.features)
.enter()
.append("text")
.attr("transform", d => `translate(${path.centroid(d)})`)
.attr("text-anchor", "middle")
.attr("font-size", 15)
// .text(function(d) {
// var plz = ""+d.properties.postleitzahl;
// var filtered = data.filter(function(d) { if(d.PLZ==plz) return d});
// if(filtered.length>0) {
// var lastFiltered = filtered[filtered.length-1];
// return `${lastFiltered.NewConfCases_7days}`;
// }
// else return '0';
// //return `<tspan x='0' dy='1.2em'>Woche ${lastFiltered.Week}:</tspan><tspan x='0' dy='1.2em'>${d.properties.NAME}: ${lastFiltered.NewConfCases}</tspan>`;
// })
.attr("dy", 15)
.attr("dx", -5);
};
function mouseOverHandler(d, i) {
d3.select(this).attr("fill", "green")
}
function mouseOutHandler(d, i) {
d3.select(this).attr("fill", "grey")
}
function clickHandler(d, i) {
//d3.select("#maptext").text(`${d.properties.postleitzahl}: ${d.properties.ortbez27}`)
}
function formatDate(date) {
var dd = date.getDate();
var mm = date.getMonth()+1;
if(dd<10) dd='0'+dd;
if(mm<10) mm='0'+mm;
return dd+"."+mm+"."
}
function getDateOfISOWeek(w, y) {
var simple = new Date(y, 0, 1 + (w - 1) * 7);
var dow = simple.getDay();
var ISOweekStart = simple;
if (dow <= 4)
ISOweekStart.setDate(simple.getDate() - simple.getDay() + 1);
else
ISOweekStart.setDate(simple.getDate() + 8 - simple.getDay());
return ISOweekStart;
}
</script>
</html>