-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgeo.html
542 lines (464 loc) · 18.7 KB
/
geo.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<style type="text/css">
#wrapper {
padding-top: 35px;
}
#map-canvas {
height: 600px;
width: 1000px;
margin: 0 auto;
}
#loading {
padding-top: 15px;
width: 200px;
margin: 0 auto;
}
.states {
fill: #ccc;
stroke: #fff;
stroke-linejoin: round;
}
.feature {
stroke: #37C3BC;
cursor: pointer;
}
/* On mouse hover, lighten state color */
path:hover {
fill-opacity: .7;
}
.active .feature {
fill: yellow;
opacity: 0.75;
}
.city-label {
font: 11px "Helvetica Neue", Helvetica, Arial, sans-serif;
pointer-events: none;
fill: #444;
fill-opacity: .75;
}
.state-label {
fill: #777;
fill-opacity: .5;
font-size: 10px;
font-weight: 300;
}
/* http://colorbrewer2.org/ */
.q0-9 {
fill: rgb(247,251,255);
}
.q1-9 {
fill: rgb(222,235,247);
}
.q2-9 {
fill: rgb(198,219,239);
}
.q3-9 {
fill: rgb(158,202,225);
}
.q4-9 {
fill: rgb(107,174,214);
}
.q5-9 {
fill: rgb(66,146,198);
}
.q6-9 {
fill: rgb(33,113,181);
}
.q7-9 {
fill: rgb(8,81,156);
}
.q8-9 {
fill: rgb(8,48,107);
}
.boundary {
fill: none;
stroke: #fff;
stroke-linejoin: round;
stroke-linecap: round;
vector-effect: non-scaling-stroke;
}
.overlay {
fill: none;
pointer-events: all;
}
.fa-spinner {
-webkit-animation: spin 1300ms infinite linear;
-moz-animation: spin 1300ms infinite linear;
-ms-animation: spin 1300ms infinite linear;
-o-animation: spin 1300ms infinite linear;
animation: spin 1300ms infinite linear;
font-size: 41px;
text-decoration: none;
color: #C0C0C0;
}
.hidden {
display: none;
visibility: hidden;
}
div.tooltip {
position: absolute;
text-align: center;
width: 60px;
height: 28px;
padding: 2px;
font: 12px sans-serif;
background: lightsteelblue;
border: 0px;
border-radius: 8px;
pointer-events: none;
}
@-webkit-keyframes spin {
to {
-webkit-transform: rotate(360deg);
}
}
@-moz-keyframes spin {
to {
-moz-transform: rotate(360deg);
}
}
@-ms-keyframes spin {
to {
-ms-transform: rotate(360deg);
}
}
@-o-keyframes spin {
to {
-o-transform: rotate(360deg);
}
}
@keyframes spin {
to {
transform: rotate(360deg);
}
}
</style>
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="https://d3js.org/d3.v3.min.js"></script>
<script src="https://d3js.org/topojson.v1.min.js"></script>
<script>
var width = 960,
height = 600;
var active = d3.select(null);
// We create a quantize scale to categorize the values in 5 groups.
// The domain is static and has a minimum/maximum of population/density.
// The scale returns text values which can be used for the color CSS
// classes (q0-9, q1-9 ... q8-9)
var quantize = d3.scale.quantize()
.range(d3.range(9).map(function (i) { return 'q' + i + '-9'; }));
//var clr = d3.scale.linear()
// //.range(['beige', 'red']);
// .range(['#FFF7EC', '#7F0000']);
var svg, g, centered, path, zoom, projection;
$(document).ready(function () {
// Area, population and population density in 2011 by province
// get from https://www.gso.gov.vn
// Average population (Thous. pers.)/Area (Km2)/Population density (Person/km2)
// Load in popuplation data with D3 (or jQuery)
d3.select('#loading').classed('hidden', false);
d3.csv('vn-population-2011.csv', function (error, rows) {
if (error) {
return console.warn(error);
}
loadGeoJson(rows);
});
});
function loadGeoJson(data) {
// @see http://www.gadm.org/
// GADM is a spatial database of the location of the world's
// administrative areas (or adminstrative boundaries) for use in GIS and similar software.
// @see http://mapshaper.org/ for simplify
// A tool for topologically aware shape simplification. Reads and
// writes Shapefile, GeoJSON and TopoJSON formats.
d3.json('vn-states-geo.json', function (error, json) {
if (error) {
return console.warn(error);
}
d3.select('#loading').classed('hidden', true);
// Merge the ag. data and GeoJSON
// Loop through once for each ag. data value
for (var i = 0; i < data.length; i++) {
// Grab state name
var dataIso = data[i].iso;
// Grab data value, and convert from string to float
var density = parseFloat(data[i].density);
var population = parseFloat(data[i].population);
//Find the corresponding state inside the GeoJSON
for (var j = 0; j < json.features.length; j++) {
var jsonIso = json.features[j].properties.iso;
if (dataIso == jsonIso) {
// Copy the data value into the JSON
json.features[j].properties.density = density;
json.features[j].properties.population = population;
// Stop looking through the JSON
break;
}
}
}
// Set the domain of the values (the minimum and maximum values of
// all values of the current key) to the quantize scale.
quantize.domain([
d3.min(json.features, function (f) { return +f.properties.density; }),
d3.max(json.features, function (f) { return +f.properties.density; })
]);
drawMap(json);
});
}
function drawMap(json) {
// @see http://geojson.org/
// Create a first guess for the projection
var center = d3.geo.centroid(json);
var scale = 4000;
var offset = [width / 2, height / 2 - 300];
// The projection function takes a location [longitude, latitude]
// and returns a Cartesian coordinates [x,y] (in pixels).
//
// D3 has several built-in projections. Albers USA is a composite projection
// that nicely tucks Alaska and Hawaii beneath the Southwest.
//
// Albers USA (albersUsa) is actually the default projection for d3.path.geo()
// The default scale value is 1,000. Anything smaller will shrink the map;
// anything larger will expand it.
//
// Add a scale() method with 800 to our projection in order to shrink things down a bit
// var projection = d3.geo.albersUsa()
// .translate([w/2, h/2]).scale([800]);
projection = d3.geo.mercator()
.translate(offset)
.scale([scale])
.center(center);
// We define our first path generator for translating that
// mess of GeoJSON coordinates into even messier messes of SVG path codes.
// Tell the path generator explicitly that it should reference our customized
// projection when generating all those paths
path = d3.geo.path()
.projection(projection);
zoom = d3.behavior.zoom()
.translate([0, 0])
.scale(1)
.scaleExtent([1, 8])
.on('zoom', zoomed);
svg = d3.select('#map-canvas').append('svg')
.attr('width', width)
.attr('height', height)
// Not process on click event and stop propagation
.on('click', stopped, true);
// Append div for tooltip to SVG
var div = d3.select('body')
.append('div')
.attr('class', 'tooltip')
.style('opacity', 0);
svg.append('rect')
.attr('class', 'overlay')
.attr('width', width)
.attr('height', height);
g = svg.append('g');
// Create g before call zoom
svg
.call(zoom) // delete this line to disable free zooming
.call(zoom.event)
.on('dblclick.zoom', null);
g
.attr('class', 'states')
.selectAll('path') // select all the current path nodes
.data(json.features) // bind these to the features array in json
.enter().append('g') // if not enough elements create a new group
.on('click', clicked)
.attr('class', function (d) {
return 'state state-' + d.properties.iso;
})
.append('path')
.attr('class', function (d) {
// Use the quantized value for the class
return 'feature ' + quantize(d.properties.density);
}) // add attribute class and fill with result from quantize
//.attr('class', 'feature')
.attr('d', path);
// State names
//g.selectAll('text')
// .data(json.features)
// .enter().append('text')
// .text(function (d) {
// return d.properties.name;
// })
// .attr('class', 'state-label')
// // Using transform equivalent to x, y
// .attr('transform', function (d) { return 'translate(' + path.centroid(d) + ')'; })
// //.attr('x', function (d) {
// // return path.centroid(d)[0];
// //})
// //.attr('y', function (d) {
// // return path.centroid(d)[1];
// //})
// .style('font-size', function (d) { return 6 / this.getComputedTextLength() * 12 + 'px'; })
// // The dy attribute indicates a shift along the y-axis on the position
// // of an element or its content. What exactly is shifted
// // depends on the element for which this attribute is set.
// .attr('dy', '.35em');
drawCities();
d3.select(self.frameElement).style('height', height + 'px');
}
function drawCities() {
d3.csv('vn-cities.csv', function (error, rows) {
if (error) {
return console.warn(error);
}
// Cities
svg.selectAll('circle')
.data(rows)
.enter().append('circle')
.attr('data-id', function (d) {
return d.code;
})
.attr('class', function (d) {
return 'city level-' + d.level;
})
.attr('visibility', function (d) {
return d.level < 3 ? 'visible' : 'hidden';
})
.attr('cx', function (d) {
return projection([d.lng, d.lat])[0];
})
.attr('cy', function (d) {
return projection([d.lng, d.lat])[1];
})
.attr('r', 2)
.style('fill', 'white')
.style('stroke', 'black')
.style('stroke-width', 2)
.style('opacity', 0.85)
// Modification of custom tooltip code provided by Malcolm Maclean, "D3 Tips and Tricks"
// http://www.d3noob.org/2013/01/adding-tooltips-to-d3js-graph.html
.on('mouseover', function (d) {
//div.transition()
// .duration(200)
// .style('opacity', .9);
})
// fade out
.on('mouseout', function (d) {
//div.transition()
// .duration(500)
// .style('opacity', 0);
});
svg.selectAll('text')
.data(rows)
.enter().append('text')
.attr('data-id', function (d) {
return d.code;
})
.attr('class', function (d) {
return 'city-label level-' + d.level;
})
.text(function (d) {
return d.name;
})
.attr('visibility', function (d) {
return d.level < 2 ? 'visible' : 'hidden';
})
.attr('x', function (d) {
return projection([d.lng, d.lat])[0];
})
.attr('y', function (d) {
return projection([d.lng, d.lat])[1];
})
.attr('text-anchor', function (d) {
return d.lng > 105.5 ? 'start' : 'end';
})
.attr('dx', function (d) {
return (d.lng > 105.5 ? 1 : -1) * 0.7 + 'em';
})
// The dy attribute indicates a shift along the y-axis on the position
// of an element or its content. What exactly is shifted
// depends on the element for which this attribute is set.
.attr('dy', '.35em');
});
}
function zoomed() {
g.selectAll('.feature').style('stroke-width', 1 / d3.event.scale + 'px');
g.attr('transform', 'translate(' + d3.event.translate + ')scale(' + d3.event.scale + ')');
svg.selectAll('.city')
.attr('transform', 'translate(' + d3.event.translate + ')scale(' + d3.event.scale + ')')
.style('r', 1 / d3.event.scale + 1)
.style('stroke-width', 2 / d3.event.scale + 'px');
svg.selectAll('.city-label')
.attr('transform', 'translate(' + d3.event.translate + ')scale(' + d3.event.scale + ')')
.style('font-size', 12 / d3.event.scale + 2 + 'px')
.attr('dy', d3.event.scale == 1 ? '0.35em' : (((1 / d3.event.scale) * 0.35 + 0.2) + 'em'));
if (d3.event.scale < 2) {
svg.selectAll('.city-label')
.filter('.level-2').attr('visibility', 'hidden');
} else {
svg.selectAll('.city-label')
.filter('.level-2').attr('visibility', 'visible');
}
if (d3.event.scale < 3.5) {
svg.selectAll('.level-3').attr('visibility', 'hidden');
svg.selectAll('.level-4').attr('visibility', 'hidden');
} else {
svg.selectAll('.level-3').attr('visibility', 'visible');
svg.selectAll('.level-4').attr('visibility', 'visible');
}
}
// If the drag behavior prevents the default click,
// also stop propagation so we dont click-to-zoom.
function stopped() {
if (d3.event.defaultPrevented) {
d3.event.stopPropagation();
}
}
function reset() {
active.classed('active', false);
active = d3.select(null);
svg.transition()
.duration(750)
.call(zoom.translate([0, 0]).scale(1).event);
}
function clicked(d) {
if (active.node() === this) {
return reset();
}
active.classed('active', false);
active = d3.select(this).classed('active', true);
var bounds = path.bounds(d),
dx = bounds[1][0] - bounds[0][0],
dy = bounds[1][1] - bounds[0][1],
x = (bounds[0][0] + bounds[1][0]) / 2,
y = (bounds[0][1] + bounds[1][1]) / 2,
scale = Math.max(1, Math.min(8, 0.9 / Math.max(dx / width, dy / height))),
translate = [width / 2 - scale * x, height / 2 - scale * y];
svg.transition()
.duration(750)
.call(zoom.translate(translate).scale(scale).event);
}
function starPoints(centerX, centerY, arms, outerRadius, innerRadius) {
var results = '';
var angle = Math.PI / arms;
for (var i = 0; i < 2 * arms; i++) {
// Use outer or inner radius depending on what iteration we are in.
var r = (i & 1) == 0 ? outerRadius : innerRadius;
var currX = centerX + Math.cos(i * angle) * r;
var currY = centerY + Math.sin(i * angle) * r;
// Our first time we simply append the coordinates, subsequet times
// we append a ", " to distinguish each coordinate pair.
if (i == 0) {
results = currX + ',' + currY;
}
else {
results += ', ' + currX + ',' + currY;
}
}
return results;
}
</script>
</head>
<body>
<div id="wrapper">
<div id="map-canvas"></div>
<div id="loading" class="hidden"><a class="fa fa-spinner"></a></div>
</div>
</body>
</html>