-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzip_map.js
304 lines (251 loc) · 8.73 KB
/
zip_map.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/**
* Visualization of Slovakian zip codes.
*
* Data downloaded from Wikipedia (https://sk.wikipedia.org/wiki/Zoznam_slovensk%C3%BDch_obc%C3%AD_a_vojensk%C3%BDch_obvodov)
* Visualization uses Mercator projection; zoom adapted from bl.ocks.org/mbostock/eec4a6cda2f573574a11
*
* @author lucyia ([email protected])
*/
'use strict';
(function ( window, document, undefined ) {
data2Vis('/data/slovakia_cities.tsv');
/**
* Function firstly parses the given file and then visualizes it.
*
* @param {string} file tab separated file
*/
function data2Vis( file ) {
d3.tsv( file, function ( error, data ) {
if ( error ) { return error; }
visualize( parseData( data ) );
});
};
/**
* Parses all elements of given data and creates an array of city objects from it.
*
* @param {array} data array of objects representing each line from file
* @return {array} cities array of objects representing each city
*/
function parseData( data ) {
return data.map( createCity );
/**
* Creates an object representing a city with given properties.
*
* @param {object} cityInfo object with parameters of a city
* @return {object} city object representing a city
*/
function createCity( cityInfo ) {
var zip = cityInfo.zip.replace(/\s/g, '');
var population = parseInt( cityInfo.population.replace(/\s/g, '') );
var coordinates = /(.+)°N (.+)°E/g.exec( cityInfo.gps );
var lat = parseFloat( coordinates[1].replace(/ /g, '') );
var lon = parseFloat( coordinates[2].replace(/ /g, '') );
return {
name: cityInfo.name,
zip: zip,
population: population,
lat: lat,
lon: lon
};
}
}
/**
* Creates a map visualization from given data.
* Each city is rendered as a circle either coloured or gray and has mouse listeners for tooltip.
*
* @param {array} data array of objects representing cities
*/
function visualize( data ) {
// vis variables
var margin = 12;
var width = window.innerWidth - (margin * 2);
var height = window.innerHeight - (margin * 2);
// initial scale for projection
var scaleInitial = 6000;
// scale value for mouse event
var zoomScale = 1;
var hoverColor = '#00bcd4';
var matchColor = '#ffffff';
var nonMatchColor = '#3a3a3a';
var numberColors = [
'#2ca02c', '#bcbd22', '#ff7f0e', '#d62728', '#8c564b', '#b1b1b1', '#17becf', '#1f77b4', '#9467bd', '#e377c2'
];
// scales for circle size and color
var radius = d3.scaleSqrt()
.domain( d3.extent( data, function(d) { return d.population; } ) )
.range( [1, 20] );
var cityColor = d3.scaleOrdinal()
.domain( d3.range( 10 ) )
.range( numberColors );
// projection - mercator - translated to center the map
var projection = d3.geoMercator()
.translate( [width/2 - scaleInitial/3 - 50, height/2 + scaleInitial - 150] )
.scale( scaleInitial );
// zoom for handling the mouse events
var zoom = d3.zoom()
.scaleExtent( [1, 15] )
.on('zoom', zoomed);
// draw SVG panel
var svg = d3.select('.vis')
.append('svg')
.attr('width', width)
.attr('height', height);
// create main group holding all elements
var vis = svg.append('g');
// invoke zoom behaviour
svg.call( zoom );
// initialize tooltip
var tip = d3.tip()
.attr('class', 'd3-tip')
.offset( [-10, 0] )
.html(function(d) {
// create user friendly format for population numbers
var number = d.population.toString().split('').reverse().join('');
number = number.replace(/(\d{3})/g, '$1 ');
number = number.split('').reverse().join('');
// create elements for each row showing info
var name = '<div class="cityname">'+ d.name +'</div>';
var zip = '<div>'+ d.zip +'</div>';
var citizens = '<div>'+ number +' citizens</div>';
return name + zip + citizens;
});
// invoke the tip in the vis context
svg.call( tip );
// draw cities
update();
// add listener on window size change
window.onresize = function(event) {
// update the variables
width = window.innerWidth - (margin * 2);
height = window.innerHeight - (margin * 2);
// update the svg size
svg.attr('width', width)
.attr('height', height);
}
// add listeners to input fields
d3.select('#populationCheck').on('change', function() {
update();
});
d3.select('#colorCheck').on('change', function() {
update();
// color legend only when checked
d3.selectAll('.color-box')
.style('background-color', function(d, i) {
return document.getElementById('colorCheck').checked ? cityColor( i ) : nonMatchColor;
});
});
d3.select('#zipInput').on('input', function() {
// ignore invalid input for ZIP - valid only numbers between 0 and 99999
if ( !isNaN( document.getElementById('zipInput').value ) ) {
// valid input
update();
}
});
/**
* Update function for the whole visualization creating cities as circles with given attributes.
* (Data don't change, only their attributes)
*/
function update() {
var populationCheck = document.getElementById('populationCheck').checked;
var colorCheck = document.getElementById('colorCheck').checked;
var input = document.getElementById('zipInput').value;
var cities = vis.selectAll('circle')
.data( data );
cities.exit()
.transition()
.duration( 700 )
.attr('r', 0)
.remove();
cities.transition()
.attr('r', circleSize)
.attr('fill', function(d) {
if (input.length === 5 && d.zip.startsWith( input )) {
tip.show(d, d3.select(this).node());
d3.select('.d3-tip')
.transition()
.delay( 2000 )
.duration( 1000 )
.style('opacity', 0);
}
return color( input, d.zip, colorCheck );
});
cities.enter()
.append('circle')
.attr('class', 'city')
.attr('cx', function(d) { return projection( [d.lon, d.lat] )[0]; })
.attr('cy', function(d) { return projection( [d.lon, d.lat] )[1]; })
.on('mouseover', mouseover)
.on('mouseout', mouseout)
.transition()
// .delay(function (d, i) { return Math.max(i, 50) + i; })
.attr('r', circleSize)
.attr('fill', function(d) { return color( input, d.zip, colorCheck ); });
function circleSize(d) {
return populationCheck ? radius( d.population ) : 2;
}
}
/**
* Determines what colour should the city circle have and returns it.
*
* @param {string} input numbers from input form
* @param {string} zip full five letter zip code
* @param {boolean} show indicator whether the city should be rendered with colour
* @return {string} color
*/
function color( input, zip, show ) {
if ( zip.startsWith( input ) ) {
return show ? nextNumberColor() : matchColor;
} else {
return nonMatchColor;
}
/**
* Determines what's the colour of the next number in zip code and returns it.
*
* @return {string} color
*/
function nextNumberColor() {
if ( input.length < 5 ) {
return cityColor( zip.charAt( input.length ) );
} else {
// no next number, the whole input is the same as zip
return matchColor;
}
}
}
/**
* Creates a tooltip over the circle representing a city
* and creates a stroke around the circle to highlight it.
*
* @param {object} city
*/
function mouseover( city ){
var colorCheck = document.getElementById('colorCheck').checked;
var circle = d3.select( this )
.attr('stroke', colorCheck ? matchColor : hoverColor)
.attr('stroke-width', 5 / zoomScale);
tip.show( city, d3.select(this).node() );
}
/**
* Hides the tooltip and resets the stroke of a circle.
*/
function mouseout() {
d3.select( this )
.attr('stroke', 'none');
tip.hide();
}
/**
* Updates the projection acoording to updated zoom scale
* and renderes only those cities in the extent of the zoom scale.
*/
function zoomed(event) {
// update the zoom scale value
zoomScale = d3.event.transform.k;
// translate the whole group holding all circles
vis.attr('transform', d3.event.transform);
// when having more than hundreds/thousands of elements:
// do not translate each element individually
// vis.selectAll('circle')
// .attr('transform', d3.event.transform);
}
}
})( this, document );