-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmucmap.js
285 lines (231 loc) · 6.75 KB
/
mucmap.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
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
var Mucmap = (function(App, d3, window, document, undefined) {
var svg,
rect,
g,
data,
color,
projection,
path,
effectLayer,
mapLayer,
features,
dummyText,
bigText,
centered,
districts,
margin = {},
width,
height,
div;
// sets width and height, necessary for mapLayer
updateDimensions();
// Load halts data
d3.json('data/district_halts.json', function(error, districtData) {
districts = districtData.districts;
});
// Load map data
d3.json('data/bezirke.geojson', init);
function init(error, heatData) {
features = heatData.features;
div = d3.select('.toolTip');
// Define color scale
// Update color scale domain based on data
color = d3
.scaleLinear()
.domain([1, 100, 1000, 3000, d3.max(features, nameLength)])
// .domain([0, 100, 1000, 3000, 6000])
.clamp(true)
.range(['#f0fdfe', '#86ccd1', '#17b0da', '#6265ac', '#3a2a85']);
// Define color scale
// Update color scale domain based on data
// var color = d3.scaleSequential(d3.interpolateWarm)
// .domain([6000, 0]);
// color.domain([d3.max(features, nameLength), 0]);
// color.domain([0, d3.max(features, nameLength)]);
// Create the legend to illustrate the color scale being divergent
var legendEntries = ['5500', '3000', '1000', '100', '0'];
var legend = d3
.select('.heatmapLegend')
.selectAll('.legend-entry')
.data(legendEntries)
.enter()
.append('div')
.attr('class', 'legend-entry');
legend
.append('div')
.attr('class', 'color-tile')
.style('background-color', function(d, i) {
return color(d);
});
legend
.append('div')
.attr('class', 'description')
.text(function(d) {
if (d === '0') {
return d + ' Fahrten oder mehr';
} else {
return d;
}
});
// Set svg width & height
svg = d3.select('#heatmap').select('svg');
// Add background
rect = svg
.append('rect')
.attr('class', 'background')
.on('click', clicked);
g = svg.append('g');
effectLayer = g.append('g').classed('effect-layer', true);
mapLayer = g.append('g').classed('map-layer', true);
dummyText = g
.append('text')
.classed('dummy-text', true)
.attr('x', 10)
.attr('y', 30)
.style('opacity', 0);
bigText = g
.append('text')
.classed('big-text', true)
.attr('x', 20)
.attr('y', 45);
projection = d3
.geoMercator()
.scale(width * 150)
// Center the Map in Munich
.center([11.542, 48.155])
.translate([width / 2, height / 2]);
path = d3.geoPath().projection(projection);
// Draw each province as a path
mapLayer
.selectAll('path')
.data(features)
.enter()
.append('path')
.attr('d', path)
.attr('vector-effect', 'non-scaling-stroke')
.style('fill', fillFn)
.style('opacity', 0.9)
.on('click', clicked)
.on('touchstart', clicked)
.on('mouseover', mouseover)
.on('mouseout', mouseout)
.on('mousemove', mousemove);
// .on('mousedown', clicked)
render();
}
function render() {
//get dimensions based on window size
updateDimensions();
//update svg elements to new dimensions
svg.attr('width', width).attr('height', height);
rect.attr('width', width).attr('height', height);
projection.scale(width * 150).translate([width / 2, height / 2]);
d3.selectAll('path').attr('d', path);
}
function updateDimensions() {
width = parseInt(d3.select('#heatmap').style('width'));
// width = width;
height = 0.78 * width; //aspect ratio is 0.78
}
// Get province name
function nameFn(d) {
return d && d.properties ? d.properties.NAME : null;
}
// Get province name length
function getDistrict(d) {
var n = nameFn(d);
var index = districts.findIndex(function(district) {
return district.name === n;
});
return districts[index];
}
// Todo rename getTotalCount
function nameLength(d) {
return getDistrict(d).meanMonth;
}
// Get province color
function fillFn(d) {
return color(nameLength(d));
}
// When clicked, zoom in
function clicked(d) {
console.log({ type: 'clicked', d: d, context: this });
var x, y, k;
// Compute centroid of the selected path
if (d && centered !== d) {
var centroid = path.centroid(d);
x = centroid[0];
y = centroid[1];
k = 4;
centered = d;
} else {
x = width / 2;
y = height / 2;
k = 1;
centered = null;
}
// textArt(getDistrict(d));
// Highlight the clicked province
mapLayer.selectAll('path').style('fill', function(d) {
return centered && d === centered ? '#ffc484' : fillFn(d);
});
if (centered === null) {
App.selectNewDistrict(0);
} else {
App.selectNewDistrict(Number(d.properties.SB_NUMMER));
}
// // Zoom
// g.transition()
// .duration(750)
// .attr('transform', 'translate(' + width / 2 + ',' + height / 2 + ')scale(' + k + ')translate(' + -x + ',' + -y + ')');
}
// $('#hexmapCollapse').on('shown.bs.collapse', function () {
// // Hexmap.init();
// Hexmap.loadDistrict(d.properties.cartodb_id);
// });
function mouseover(d) {
console.log({ type: 'mouseover', d: d, context: this });
d3.select(this).style('cursor', 'pointer');
// Highlight hovered province
d3.select(this).style('fill', '#ffc484');
// Draw effects
// textArt(getDistrict(d));
}
function mouseout(d) {
console.log({ type: 'mouseout', d: d, context: this });
d3.select(this).style('cursor', 'default');
// Reset province color
mapLayer.selectAll('path').style('fill', function(d) {
return centered && d === centered ? '#ffc484' : fillFn(d);
});
// Remove effect text
effectLayer
.selectAll('text')
.transition()
.style('opacity', 0)
.remove();
// Clear province name
bigText.text('');
div.style('display', 'none');
}
function mousemove(d) {
// console.log({ type: 'mousemove', d: d, context: this });
var district = getDistrict(d);
div.style('left', d3.event.pageX + 10 + 'px');
div.style('top', d3.event.pageY - 25 + 'px');
div.style('display', 'inline-block');
div.html(
district.name + '<br>' + Math.round(district.meanMonth) + ' Fahrten'
);
}
// Gimmick
// Just me playing around.
// You won't need this for a regular map.
function textArt(district) {
bigText.text(district.name);
// .text(district.name + ': ' + Math.round(district.meanMonth) + ' Fahrten enden im Monat hier');
}
return {
render: render
};
})(App, d3, window, document);