-
Notifications
You must be signed in to change notification settings - Fork 3
/
wahlkarte.js
233 lines (195 loc) · 7.4 KB
/
wahlkarte.js
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
var KA_LAT = 49.00921;
var KA_LNG = 8.45003951;
var elemSvg = null;
var GEOJSON = null;
var TILES_URL = 'https://cartodb-basemaps-{s}.global.ssl.fastly.net/light_all/{z}/{x}/{y}@2x.png';
var MAP_ATTRIBUTION = 'Map data © <a href="//openstreetmap.org">' +
'OpenStreetMap</a> contributors | Tiles © ' +
'<a href="//carto.com/attribution">Carto</a>';
var map;
var svg;
var wahlbezirke;
var stadtteile;
/**
* Adapter to use Leaflet's projection in D3.
*/
function projectPoint(x, y) {
var point = map.latLngToLayerPoint(new L.LatLng(y, x));
this.stream.point(point.x, point.y);
}
var transform = d3.geo.transform({ point: projectPoint }),
path = d3.geo.path().projection(transform);
/**
* Create SVG paths from a GeoJSON file.
*
* Returns a promise that resolves to a D3 selection of the created
* paths.
*/
function pathsFromGeoJSON(filename, group, setGeoJson) {
var loadJson = Promise.promisify(d3.json);
return loadJson(filename)
.then(function(collection) {
if (GEOJSON === null && setGeoJson){
GEOJSON = collection
}
var feature = group.selectAll("path")
.data(collection.features)
.enter().append("path");
map.on("viewreset", reset);
reset();
return feature;
// Reposition the SVG to cover the features.
function reset() {
var bounds = path.bounds(collection);
var topLeft = bounds[0];
var bottomRight = bounds[1];
svg.attr("width", bottomRight[0] - topLeft[0])
.attr("height", bottomRight[1] - topLeft[1])
.style("left", topLeft[0] + "px")
.style("top", topLeft[1] + "px");
group.attr("transform", "translate(" + -topLeft[0] + "," +
-topLeft[1] + ")");
feature.attr("d", path);
}
});
}
/**
* Aktualisiert die Twitter und Facebook Links.
*/
function updateSharingLinks() {
var param = encodeURIComponent(window.location.href);
$('#facebookButton').attr('href', 'https://www.facebook.com/sharer/sharer.php?u=' + param);
$('#twitterButton').attr('href', 'https://twitter.com/home?status=' + param);
$('#whatsappButton').attr('href', 'whatsapp://send?text=' + param);
}
/**
* Springt zum Szenario mit der angegebenen ID.
*/
function setScenario(scenarioId) {
if (window.location.hash.slice(1) !== scenarioId) {
window.location.hash = scenarioId;
return; // Function will be called a second time from onHashChange
}
closeInfoPanel();
var currentSlideIndex = $('#szenarien-carousel div.active').index();
for (var i = 0; i < SZENARIEN.length; i++) {
if (SZENARIEN[i].id === scenarioId) {
var szenario = SZENARIEN[i];
if (i !== currentSlideIndex) {
$("#szenarien-carousel").carousel(i);
}
break;
}
}
colorMapWinDistrict(szenario);
updateSharingLinks();
}
function colorMapNeutrally() {
// Karte Reference setzten wenn nicht vorhanden
if(elemSvg === null){
getSVGMap();
} // end if
if (GEOJSON !== null) {
elemSvg.getElementById(item.properties.wahlbezirksnummer).style.fill = '#fff';
} else {
console.error("GEOJSON null!")
}
}
/**
* Faerbt die Karte nach dem Gewinner fuer jeden Wahlbezirk. Falls keine
* Farbe fuer die Partei gesetzt, so wird eine 'DefaultColor' gewaehlt.
*/
function colorMapWinDistrict(szenario) {
// Karte Reference setzten wenn nicht vorhanden
if(elemSvg === null){
getSVGMap();
} // end if
if (GEOJSON !== null){
for(var item of GEOJSON.features){
var analyse = szenario.getAnalyse(item.properties);
currentAnalysis[item.properties.wahlbezirksnummer] = analyse;
var color = analyse.color;
if (typeof color !== 'undefined'){
elemSvg.getElementById(item.properties.wahlbezirksnummer).style.fill = color
}
}
} else {
console.error("GEOJSON null!")
}
}
/**
* Initialisierung wenn die Seite vollständig geladen ist.
*/
$(function() {
map = new L.Map("map", {center: [KA_LAT, KA_LNG], zoom: 12})
.addLayer(new L.TileLayer(TILES_URL, {attribution: MAP_ATTRIBUTION}));
svg = d3.select(map.getPanes().overlayPane).append("svg");
var g = svg.append("g").attr("class", "leaflet-zoom-hide");
svg.attr("id", "karte")
stadtteile = g.append('g')
.classed('stadtteile', true);
wahlbezirke = g.append('g')
.classed('wahlbezirke', true);
var stadtteilePromise = pathsFromGeoJSON("ka_stadtteile.geojson", stadtteile, false)
.then(function (paths) {
paths
.attr("id", function (d) { return d.properties.Stadtteilnummer })
.attr('class', 'district')
.style('fill', 'rgba(255, 255, 255, 0.7)')
.style('stroke', '#000')
.style('stroke-width', 2);
});
var wahlbezirkePromise = pathsFromGeoJSON("wahlbezirke.geojson", wahlbezirke, true)
.then(function (paths) {
paths
.attr("id", function (d) { return d.properties.wahlbezirksnummer })
.attr('class', 'wahlbezirk')
.on('mousemove', onMouseOverWahlbezirk)
.on('mouseleave', onMouseLeaveWahlbezirk)
.style('fill', '#fff')
.style('stroke', '#000')
.style('stroke-width', 1)
.on('click', selectDistrict);
});
innerCarousel = document.getElementById('innerCarousel');
indicators = document.getElementById('indicators');
createSzenarien();
$('#szenarien-carousel').bind('slid.bs.carousel', function (e) {
setScenario(e.relatedTarget.id);
});
toolTip = document.getElementById('tool-tip'),
currentConstituencyNumber = null,
currentConstituencyObject = null,
xpos = 0,
ypos = 0;
/**
* Listener der schaut wo der User gerade mit der Maus ist
*/
document.body.addEventListener('mousemove',function(mouseEvent){
// Prüfen ob der Brwoser das Objekt untersützt
if (mouseEvent) {
xpos = mouseEvent.clientX;
ypos = mouseEvent.clientY;
} else {
//IE
xpos = window.event.clientX;
ypos = window.event.clientY;
} // end if else
// Position von der Maus dem Div übergeben
toolTip.style.top = (ypos - (toolTip.offsetHeight / 2))+'px';
toolTip.style.left = (xpos + 25)+'px';
},false);
infoPanelDistrictName = document.getElementById('extended-tooltip-district-name');
infoPanel = document.getElementById('extended-tooltip');
extendedTooltipDetailDistrictInfo = document.getElementById('extended-tooltip-detail-district-info');
extendedTooltipDetailDistrictInfoErststimme = document.getElementById('pills-erststimme');
extendedTooltipDetailDistrictInfoZweitstimme = document.getElementById('pills-zweitstimme');
Promise.all([stadtteilePromise, wahlbezirkePromise])
.then(function() {
function onHashChange(e) {
setScenario(getScenarioIdFromUrl());
}
$(window).on('hashchange', onHashChange);
onHashChange();
});
});