-
Notifications
You must be signed in to change notification settings - Fork 35
/
35_map_zoom_buttons.html
230 lines (169 loc) · 4.84 KB
/
35_map_zoom_buttons.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>D3: Zoom buttons</title>
<script type="text/javascript" src="d3.js"></script>
<style type="text/css">
body {
background-color: gray;
}
svg {
background-color: black;
}
</style>
</head>
<body>
<div id="mapContainer"></div>
<button id="zoomIn">Zoom In</button>
<button id="zoomOut">Zoom Out</button>
<script type="text/javascript">
//Globals for use later
var countries;
var cities;
//Year of CO2 data to map
var year = "2010";
//Width and height
var w = 500;
var h = 300;
//Define map projection
var projection = d3.geo.mercator()
.center([ 0, 40 ])
.translate([ w/2, h/2 ])
.scale([ w/7 ]);
//Define path generator
var path = d3.geo.path()
.projection(projection);
//
//Configure zoom behavior
//
var maxZoomIn = w*2;
var maxZoomOut = w/7;
var zoom = d3.behavior.zoom()
.translate( projection.translate() )
.scale( projection.scale() )
.scaleExtent([ maxZoomOut, maxZoomIn ]);
//Define zoom in and out functions that we can call easily later
var zoomIn = function() {
var newScale = Math.min(projection.scale() * 2, maxZoomIn);
zoomTo(newScale);
};
var zoomOut = function() {
var newScale = Math.max(projection.scale() / 2, maxZoomOut);
zoomTo(newScale);
};
//Define zoomTo function to which we can pass a new scale value
zoomTo = function(newScale) {
var t = projection.translate(),
s = projection.scale();
t[0] -= w / 2;
t[0] *= newScale / s;
t[0] += w / 2;
t[1] -= h * 0.55;
t[1] *= newScale / s;
t[1] += h * 0.55;
zoom.translate(t).scale(newScale);
projection.translate(t).scale(newScale);
//Finally, transition paths and circles into place
countries.transition()
.ease("linear")
.delay(50)
.duration(500)
.attr("d", path);
cities.transition()
.ease("linear")
.duration(500)
.attr("cx", function(d) {
return projection([d.longitude, d.latitude])[0];
})
.attr("cy", function(d) {
return projection([d.longitude, d.latitude])[1];
});
};
//Assign zoom button behavior
d3.select("#zoomIn")
.on("click", function() {
zoomIn();
});
d3.select("#zoomOut")
.on("click", function() {
zoomOut();
});
//Define quantize scale to sort data values into buckets of color
//Colors by Cynthia Brewer (colorbrewer2.org), YlOrRd
var color = d3.scale.quantize()
.range([ "#ffffb2", "#fecc5c", "#fd8d3c", "#f03b20", "#bd0026" ]);
//Create SVG
var svg = d3.select("#mapContainer")
.append("svg")
.attr("width", w)
.attr("height", h);
//Load in CO2 data
d3.csv("data/co2/co2_emissions.csv", function(data) {
//Set input domain for color scale
color.domain([
d3.min(data, function(d) { return +d[year]; }),
d3.max(data, function(d) { return +d[year]; })
]);
//Load in GeoJSON data
d3.json("data/geo/mapshaper_output.json", function(json) {
//Merge the CO2 data and GeoJSON into a single array
//
//Loop through once for each CO2 data value
for (var i = 0; i < data.length; i++) {
//Grab country name
var dataCountryCode = data[i].countryCode;
//Grab data value, and convert from string to float
var dataValue = +data[i][year];
//Find the corresponding country inside the GeoJSON
for (var j = 0; j < json.features.length; j++) {
//We'll check the official ISO country code
var jsonCountryCode = json.features[j].properties.iso_a3;
if (dataCountryCode == jsonCountryCode) {
//Copy the data value into the GeoJSON
json.features[j].properties.co2 = dataValue;
//Stop looking through the JSON
break;
}
}
}
//Bind data and create one path per GeoJSON feature
countries = svg.selectAll("path")
.data(json.features)
.enter()
.append("path")
.attr("d", path)
.style("fill", function(d) {
//Get data value
var value = d.properties.co2;
if (value) {
//If value exists…
return color(value);
} else {
//If value is undefined…
return "#ccc";
}
});
//Load in cities data
d3.csv("data/cities/cities.csv", function(data) {
cities = svg.selectAll("circle")
.data(data)
.enter()
.append("circle")
.attr("cx", function(d) {
return projection([d.longitude, d.latitude])[0];
})
.attr("cy", function(d) {
return projection([d.longitude, d.latitude])[1];
})
.attr("r", function(d) {
return Math.sqrt(+d.population / w * 0.001);
})
.style("fill", "blue")
.style("opacity", 0.75);
});
}); //End d3.json()
}); //End d3.csv()
</script>
</body>
</html>