-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.html
executable file
·368 lines (312 loc) · 11.9 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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<script src="js/jquery-2.0.0.js"></script>
<link href="css/style.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="tooltip" class="hidden">
<div id="selection">
<p><span id="title">Laden...</span></p>
<p><span id="value">Lade Nutzerdaten</span></p>
</div>
<div>
<p><svg width="10px" height="10px">
<rect width="10" height="10" style="fill:#238b45" />
</svg> <span id="range3"></span></p>
</div>
<div>
<p><svg width="10px" height="10px">
<rect width="10" height="10" style="fill:#41ab5d" />
</svg> <span id="range2"></span></p>
</div>
<div>
<p><svg width="10px" height="10px">
<rect width="10" height="10" style="fill:#74c476" />
</svg> <span id="range1"></span></p>
</div>
<div>
<p><svg width="10px" height="10px">
<rect width="10" height="10" style="fill:#a1d99b" />
</svg> <span id="range0"></span></p>
</div>
</div>
<div id="map"></div>
<div style="font-size:10px; font-style:sans-serif">Quelle: «Bundesamt für Statistik (BFS), GEOSTAT» <a href="http://www.bfs.admin.ch/bfs/portal/de/index/dienstleistungen/geostat/datenbeschreibung/generalisierte_gemeindegrenzen.html" target="_blank">Generalisierte Gemeindegrenzen</a> 2014
<script src="js/d3.v3.min.js"></script>
<script src="js/topojson.v1.min.js"></script>
<script>
var m_width = $("#map").width(),
width = 938,
height = 500,
kantone, // Alle Kantone der Schweiz
bezirke, // Alle Bezirke der Schweiz
gemeinden, // Alle Gemeinden der Scheiz
kanton, // Ausgewählter Kanton
bezirk, // Ausgewählter Bezitk
dataset;
var highlight = function() {
d3.select(this)
.classed("highlighted", false);
}
var color = d3.scale.quantize()
.range(["#74c476","#41ab5d","#238b45"]);
var projection = d3.geo.conicConformal()
.scale(150)
.translate([width / 2, height / 1.5]);
var path = d3.geo.path()
.projection(projection);
var svg = d3.select("#map").append("svg")
.attr("preserveAspectRatio", "xMidYMid")
.attr("viewBox", "0 0 " + width + " " + height)
.attr("width", m_width)
.attr("height", m_width * height / width);
svg.append("rect")
.attr("class", "background")
.attr("width", width)
.attr("height", height)
.on("click", move_up)
.on("mouseover", function() {
if (!kanton) {
d3.select("#title").text("Wählen Sie einen Kanton");
d3.select("#value").text("Klicken Sie, um auf eine tiefere Ebene zu gelangen");
}
else if (!bezirk)
{
d3.select("#title").text("Wählen Sie einen Bezirk");
d3.select("#value").text("Klicken sie auf die weisse Fläche, um auf eine höhere Ebene zu gelangen");
}
else {
d3.select("#title").text("");
d3.select("#value").text("Klicken sie auf die weisse Fläche, um auf eine höhere Ebene zu gelangen");
}
});
var g = svg.append("g");
var get_place_color = function(d) {
//Get data value
if (d.entries) {
var value = d.entry.Visitors;
}
else {
var value = 0;
}
if (value ) {
//If value exists…
return color(value);
} else {
//If value is undefined…
return "#a1d99b";
}
};
var update_info = function(d) {
if (d.entries) {
var value = d.entry.Visitors;
}
else {
var value = 0;
}
d3.select(this).classed("highlighted", true);
var name;
if(d.properties.KTNAME) {
name = d.properties.KTNAME;
}
else if(d.properties.BZNAME) {
name = d.properties.BZNAME;
}
else if(d.properties.GMDNAME) {
name = d.properties.GMDNAME;
}
d3.select("#title").text(name);
d3.select("#value").text(value + " Besucher");
}
function set_colordomain(d) {
color.domain([
d3.min(d, function(d) {
if (d.entries) {
return d.entry.Visitors;
}
else {
return 0;
}
}),
d3.max(d, function(d) {
if (d.entries) {
return d.entry.Visitors;
}
else {
return 0;
}
})
]);
d3.select("#range0").text("0");
d3.select("#range1").text("0 - "+Math.round(color.invertExtent("#74c476")[1]));
d3.select("#range2").text(Math.round(color.invertExtent("#41ab5d")[1])+" - "+Math.round(color.invertExtent("#41ab5d")[1]));
d3.select("#range3").text(Math.round(color.invertExtent("#238b45")[1])+" - "+Math.round(color.invertExtent("#238b45")[1]));
}
function move_up() {
if (bezirk) {
kanton_clicked(kanton);
bezirk = null;
g.selectAll("#gemeinden").remove();
}
else if(kanton) {
start_demo();
kanton = null;
g.selectAll("#bezirke").remove();
}
if (!kanton) {
d3.select("#title").text("Wählen Sie einen Kanton");
d3.select("#value").text("Klicken Sie, um auf eine tiefere Ebene zu gelangen");
}
else if (!bezirk)
{
d3.select("#title").text("Wählen Sie einen Bezirk");
d3.select("#value").text("Klicken sie auf die weisse Fläche, um auf eine höhere Ebene zu gelangen");
}
else {
d3.select("#title").text("");
d3.select("#value").text("Klicken sie auf die weisse Fläche, um auf eine höhere Ebene zu gelangen");
}
}
function zoom(xyz) {
g.selectAll(["#kantone", "#bezirke", "#gemeinden"])
.style("stroke-width", 1.0 / xyz[2] + "px");
g.transition()
.duration(750)
.attr("transform", "translate(" + projection.translate() + ")scale(" + xyz[2] + ")translate(-" + xyz[0] + ",-" + xyz[1] + ")")
.selectAll(["#kantone", "#bezirke", "#gemeinden"])
.style("stroke-width", 1.0 / xyz[2] + "px")
.selectAll(".gemeinde")
.attr("d", path.pointRadius(20.0 / xyz[2]));
}
function get_xyz(d) {
var bounds = path.bounds(d);
var w_scale = (bounds[1][0] - bounds[0][0]) / width;
var h_scale = (bounds[1][1] - bounds[0][1]) / height;
var z = .96 / Math.max(w_scale, h_scale);
var x = (bounds[1][0] + bounds[0][0]) / 2;
var y = (bounds[1][1] + bounds[0][1]) / 2 + (height / z / 6);
return [x, y, z];
}
function get_sum_of_entries(all_entries) {
var entries = {Visitors:0};
all_entries.forEach(function(entry) {
entries.Visitors += parseInt(entry.Visitors);
})
return entries;
}
function kanton_clicked(d) {
var xyz = get_xyz(d);
kanton = d;
var contained_bezirke = bezirke.filter( function(bezirk) {
return bezirk.properties.KTNR == d.properties.KTNR;
});
contained_bezirke.forEach(function(d) {
d.entries = dataset.filter( function(data) {
return data.Bezirk == d.properties.BZNAME;
});
d.entry = get_sum_of_entries(d.entries);
});
set_colordomain(contained_bezirke);
g.append("g")
.attr("id", "bezirke")
.selectAll("path")
.data(contained_bezirke)
.enter()
.append("path")
.attr("id", function(d) { return "bezirke"; })
.attr("class", "active")
.attr("d", path)
.attr("fill", get_place_color)
.on("click", bezirk_clicked)
.on("mouseover", update_info)
.on("mouseout", function() {
d3.select(this).classed("highlighted", false);
});
zoom(xyz);
g.selectAll("#kantone").remove();
}
function bezirk_clicked(d) {
var xyz = get_xyz(d);
bezirk = d;
var contained_gemeinden = gemeinden.filter( function(gemeinde) {
return gemeinde.properties.BZNR == d.properties.BZNR;
});
contained_gemeinden.forEach(function(d) {
d.entries = dataset.filter( function(data) {
return data.Gemeinde == d.properties.GMDNAME;
});
d.entry = get_sum_of_entries(d.entries);
});
set_colordomain(contained_gemeinden);
g.append("g")
.attr("id", "gemeinden")
.selectAll("path")
.data(contained_gemeinden)
.enter()
.append("path")
.attr("id", function(d) { return d.properties.name; })
.attr("class", "gemeinde")
.attr("d", path.pointRadius(20 / xyz[2]))
.attr("fill", get_place_color)
.on("mouseover", update_info)
.on("mouseout", highlight);
zoom(xyz);
g.selectAll("#bezirke").remove();
}
function start_demo() {
set_colordomain(kantone);
g.append("g")
.attr("id", "kantone")
.selectAll("path")
.data(kantone)
.enter()
.append("path")
.attr("id", function(d) { return d.id; })
.attr("d", path)
.attr("fill", get_place_color)
.on("click", kanton_clicked)
.on("mouseover", update_info)
.on("mouseout", highlight);
d3.json("json/start.json", function(error, json) {
start = get_xyz((json.features)[0]);
zoom(start);
g.selectAll("#bezirke", "#gemeinden").remove();
});
};
$(window).resize(function() {
var w = $("#map").width();
svg.attr("width", w);
svg.attr("height", w * height / width);
});
d3.csv("csv/data.csv", function(data) {
dataset = data;
d3.select("#value").text("Lade Kantone");
d3.json("json/kantone.topo.json", function(error, json) {
kantone = topojson.feature(json, json.objects.kantone).features;
kantone.forEach(function(d) {
d.entries = dataset.filter( function(data) {
return data.Kanton == d.properties.KTNAME;
});
d.entry = get_sum_of_entries(d.entries);
});
d3.select("#value").text("Lade Bezirke");
//Lade Bezirke
d3.json("json/bezirke.topo.json", function(error, json) {
bezirke = topojson.feature(json, json.objects.bezirke).features;
d3.select("#value").text("Lade Gemeinden");
// Lade Gemeinden
d3.json("json/gemeinden.topo.json", function(error, json) {
gemeinden = topojson.feature(json, json.objects.gemeinden).features;
d3.select("#title").text("Wählen Sie einen Kanton");
d3.select("#value").text("Klicken um zu den Bezirken zu gelangen");
//Starte die Demonstration
start_demo();
});
});
});
});
</script>
</body>
</html>