-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd3.html
192 lines (164 loc) · 6.13 KB
/
d3.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
<!DOCTYPE html>
<html>
<meta charset="utf-8">
<style>
body {
font-family: sans-serif;
}
.border {
fill: none;
stroke: #222;
stroke-width: 2px;
}
.regions.selected {
fill: #fff;
stroke: none;
transition: all 0.2s ease;
}
.regions.selected:hover, .regions.active {
fill: #ff8b17;
stroke: none;
stroke-width: 0px;
}
.container {
margin: 0 auto;
max-width: 1260px;
display: flex;
}
.item {
flex: 1
}
.item.map {
flex: 3
}
.legend {
color: #CCC;
text-align: center;
}
</style>
<body>
<div class="container">
<div class="item legend">
<ul>
</ul>
</div>
<div class="item map">
<div class="view" id="map"></div>
</div>
</div>
<div class="legend"></div>
<script src="bower_components/d3/d3.min.js"></script>
<script src="bower_components/topojson/topojson.js"></script>
<script>
d3.select(window).on("resize", resize);
var width = document.querySelector('#map').offsetWidth;
var mapRatio = 0.7;
var height = width * mapRatio;
var projection = d3.geo.mercator().scale(width / 2 / Math.PI)
.rotate([-11.25, 0])
.translate([(width) / 2, height * 1.35 / 2])
.precision(.1);
var path = d3.geo.path().projection(projection);
var svg = d3.select("#map")
.append("svg")
.attr("preserveAspectRatio", "xMinYMin")
.attr("width", width)
.attr("height", height)
.append("g");
var sets = [
{
name: 'Central Europe',
set: d3.set(['BEL', 'CHE', 'DEU', 'AUT', 'ESP', 'FRA', 'ATF', 'GBR', 'GGY', 'JEY', 'FLK', 'SGS', 'GRC', 'MLT', 'IRL', 'ITA', 'LUX', 'NLD', 'AND', 'POL', 'PRT', 'TUR', 'CYP', 'CYN', 'MON', 'ALD', 'IMN', 'LTU', 'LVA', 'EST', 'BLR', 'UKR', 'MDA', 'ROU', 'HUN', 'SVK', 'SVN', 'HRV', 'BIH', 'CZE', 'BGR', 'KOS', 'MKD', 'ALB', 'MNE', 'SRB']),
},
{
name: 'Northern Europe',
set: d3.set(['DNK', 'FRO', 'FIN', 'GRL', 'ISL', 'NOR', 'SWE'])
},
{
name: 'North America',
set: d3.set(['CAN', 'MEX', 'USA'])
},
{
name: 'Caribbean',
set: d3.set(['BLZ', 'CRI', 'CUB', 'GTM', 'HND', 'NIC', 'PAN', 'SLV', 'HTI', 'JAM', 'DOM', 'PRI', 'BHS', 'TCA', 'ATG', 'DMA', 'BRB', 'GRD'])
},
{
name: 'South America',
set: d3.set(['ARG', 'BOL', 'BRA', 'CHL', 'COL', 'ECU', 'FLK', 'GUY', 'PRY', 'PER', 'SUR', 'URY', 'VEN', 'TTO'])
},
{
name: 'Africa',
set: d3.set(['AGO', 'BDI', 'BEN', 'BFA', 'BWA', 'CAF', 'CIV', 'CMR', 'COD', 'COD', 'COG', 'COM', 'CPV', 'DJI', 'DZA', 'EGY', 'ERI', 'ETH', 'GAB', 'GHA', 'GIN', 'GMB', 'GNB', 'GNQ', 'KEN', 'LBR', 'LBY', 'LSO', 'MAR', 'MDG', 'MLI', 'MOZ', 'MRT', 'MUS', 'MWI', 'MYT', 'NAM', 'NER', 'NGA', 'REU', 'RWA', 'ESH', 'SDN', 'SDS', 'SEN', 'SHN', 'SHN', 'SLE', 'SOM', 'SOL', 'SSD', 'STP', 'STP', 'SWZ', 'SYC', 'TCD', 'TGO', 'TUN', 'TZA', 'TZA', 'UGA', 'ZAF', 'ZMB', 'ZWE'])
},
{
name: 'South Pacific',
set: d3.set(['AUS', 'NZL'])
},
{
name: 'India',
set: d3.set(['IND', 'BGD', 'LKA'])
},
{
name: 'Orient',
set: d3.set(['AZE', 'ARE', 'QAT', 'IRN', 'AFG', 'PAK', 'BHR', 'SAU', 'YEM', 'OMN', 'SYR', 'JOR', 'IRQ', 'KWT', 'ISR', 'LBN', 'PSX', 'PSR', 'GEO', 'ARM'])
},
{
name: 'Russia',
set: d3.set(['RUS', 'KAZ', 'UZB', 'TKM', 'KGZ', 'TJK'])
},
{
name: 'Asia',
set: d3.set(['BTN', 'CHN', 'JPN', 'IDN', 'MNG', 'NPL', 'MMR', 'THA', 'KHM', 'LAO', 'VNM', 'PRK', 'KOR', 'TWN', 'MYS', 'PNG', 'SLB', 'VUT', 'NCL', 'BRN', 'PHL', 'TLS', 'HKG', 'FJI', 'GUM', 'PLW', 'FSM', 'MNP', 'KAS'])
}
];
d3.json("lib/countries.json", function (error, w) {
if (error) throw error;
svg.append("path").datum(topojson.merge(w, w.objects.units.geometries.filter(function (d) {
return d.id !== 'ATA';
})))
.attr("class", "border")
.attr("d", path);
for (var i = 0; i < sets.length; i++) {
svg.append("path").datum(topojson.merge(w, w.objects.units.geometries.filter(function (d) {
return sets[i].set.has(d.id); // properties.iso_a3
})))
.attr('class', "regions selected")
.attr("d", path)
.attr({'data-name': sets[i].name})
.attr({'data-id': 'p' + i});
var li = document.createElement('li');
var a = document.createElement('a');
a.setAttribute('href', '#');
a.setAttribute('data-id', 'p' + i);
a.innerText = sets[i].name;
li.appendChild(a);
document.querySelector('.item.legend ul').appendChild(li);
}
var divs = d3.select('.legend').selectAll("a");
divs.on('mouseover', function () {
var region = d3.select('path[data-id=' + this.getAttribute('data-id') + ']');
region.attr('class', 'regions active');
});
divs.on('mouseout', function () {
var region = d3.select('path[data-id=' + this.getAttribute('data-id') + ']');
region.attr('class', 'regions selected');
});
});
function resize() {
// adjust things when the window size changes
width = document.querySelector('#map').offsetWidth;
// width = width - margin.left - margin.right;
height = width * mapRatio;
// update projection
projection.scale(width / 2 / Math.PI)
.translate([(width) / 2, height * 1.35 / 2])
.precision(.1);
// resize the map container
document.querySelector('svg').setAttribute('width', width);
document.querySelector('svg').setAttribute('height', height);
// resize the map
svg.selectAll('.regions, .border').attr('d', path);
}
</script>
</body>
</html>