-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathafrica.html
103 lines (79 loc) · 2.38 KB
/
africa.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
<!DOCTYPE html>
<meta charset="utf-8">
<head>
<title>geoPath measures</title>
</head>
<style>
body {
font-family: "Helvetica Neue", Helvetica, sans-serif;
font-size: 14px;
color: #333;
}
#content .info {
height: 20px;
}
#content .map path {
fill: #aaa;
stroke: #fff;
}
#content .bounding-box rect {
fill: none;
stroke: #333;
stroke-dasharray: 2,1;
}
#content .centroid {
display: none;
}
#content .centroid circle {
fill: red;
}
</style>
<body>
<div id="content">
<div class="info">Hover over a country</div>
<svg width="620px" height="600px">
<g class="map"></g>
<g class="bounding-box"><rect></rect></g>
<g class="centroid"><circle r="4"></circle></g>
</svg>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.9.1/d3.min.js"></script>
<script>
var projection = d3.geoMercator()
.scale(400)
.translate([200, 280])
.center([0, 5]);
var geoGenerator = d3.geoPath()
.projection(projection);
function handleMouseover(d) {
var pixelArea = geoGenerator.area(d);
var bounds = geoGenerator.bounds(d);
var centroid = geoGenerator.centroid(d);
var measure = geoGenerator.measure(d);
d3.select('#content .info')
.text(d.properties.id + ' (path.area = ' + pixelArea.toFixed(1) + ' path.measure = ' + measure.toFixed(1) + ')');
d3.select('#content .bounding-box rect')
.attr('x', bounds[0][0])
.attr('y', bounds[0][1])
.attr('width', bounds[1][0] - bounds[0][0])
.attr('height', bounds[1][1] - bounds[0][1]);
d3.select('#content .centroid')
.style('display', 'inline')
.attr('transform', 'translate(' + centroid + ')');
}
function update(geojson) {
var u = d3.select('#content g.map')
.selectAll('path')
.data(geojson.features);
u.enter()
.append('path')
.attr('d', geoGenerator)
.on('mouseover', handleMouseover);
}
// REQUEST DATA
d3.json('https://gist.githubusercontent.com/d3indepth/3ccd770923a61f26f55156657e2f51e8/raw/9a33b25b8d6cfece3bc7ea5379c733709b00fbe1/africa.json', function(err, json) {
update(json)
})
</script>
</body>
</html>