-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
352 lines (307 loc) · 11.3 KB
/
index.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
<!DOCTYPE html>
<meta charset="utf-8">
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://d3js.org/topojson.v1.min.js"></script>
<script src="http://d3js.org/queue.v1.min.js"></script>
<script src="http://d3js.org/colorbrewer.v1.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.2.min.js"></script>
<script type="text/javascript" src="tipsy/jquery.tipsy.js"></script>
<link href="css/usmig.css" rel="stylesheet" type="text/css" />
<link href="css/tipsy.css" rel="stylesheet" type="text/css" />
<body>
<title>United States Migration Explorer</title>
<h1>United States Migration Explorer</h1>
<script>
var width = 960,
height = 500;
var projection = d3.geo.mercator()
.translate([0, 0])
.scale(width / 2 / Math.PI);
var zoom = d3.behavior.zoom()
.scaleExtent([1, 8])
.on("zoom", move);
var path = d3.geo.path()
.projection(projection);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")")
.call(zoom);
var modes = ["Source", "Destination"], selected = 0;
var form = d3.select("body").append("form");
var labels = form.selectAll("label")
.data(modes)
.enter()
.append("label")
.attr("class", "modeButton")
.text(function(d) {return d + " Mode";})
.insert("input")
.attr({
type: "radio",
name: "mode",
id: function(d) { return d + "Button"; },
value: function(d) { return d; }
})
.property("checked", function(d, i) {return i===selected;});
var behind = svg.append("g");
behind.append("rect")
.attr("class", "overlay")
.attr("x", -width / 2)
.attr("y", -height / 2)
.attr("width", width)
.attr("height", height);
var zoomable = svg.append("g");
var ontop = svg.append("g");
var legend = ontop.append("g")
.attr("id", "legend")
.attr("class", "colors")
.attr("visibility", "hidden");
legend.selectAll(".colorbar")
.data([0,1,2,3,4,5,6])
.enter().append("svg:rect")
.attr("y", function(d) { return d*20; })
.attr("height", "20px")
.attr("width", "20px")
.attr("x", -width / 2)
.attr("transform", "translate(0," + ((height / 2) - 150) + ")")
.attr("class", function(d) { return "q" + d + "-7"; })
.attr("stroke", "none");
var legendLabels = ontop.append("g")
.attr("id", "laxis")
.attr("class", "y axis")
.attr("transform", "translate(" + ((-width / 2) + 20) + "," +
((height / 2) - 150) + ")")
queue()
.defer(d3.json, "json/world.json")
.defer(d3.json, "json/us.json")
.defer(d3.csv, "data/links.csv")
.defer(d3.csv, "data/bpld_latlon.csv")
.defer(d3.csv, "data/state_bpld.csv")
.defer(d3.csv, "data/country_bpld.csv")
.defer(d3.csv, "data/bpld_names.csv")
.await(ready);
function ready(error, world, us, links, location, stcodes, cocodes, bpldNames) {
var bpldByCode = d3.map(),
stateToBpld = d3.map(),
bpldToPoly = d3.map(),
countryToBpld = d3.map(),
bpldToName = d3.map(),
bpldToPoly = d3.map();
// Process various csv files to link polygons, codes, and names
stcodes.forEach(function(d) {
stateToBpld.set(d.jsonstate, d.bpl);
});
cocodes.forEach(function(d) {
countryToBpld.set(d.iso, d.bpld);
});
bpldNames.forEach(function(d) {
bpldToName.set(d.bpld, d.name);
})
location.forEach(function(d) {
bpldByCode.set(d.bpl, d);
d.emigrants = d3.map(); // blpd code to number of people leaving from HERE
d.immigrants = d3.map(); // blpd code to number of people coming to HERE
d.name = bpldToName.get(d.bpl);
});
// Manually map countries as necessary when one polygon corresponds to
// multiple census codes
bpldByCode.set(41000, bpldByCode.get(41300)); // map england to UK
bpldByCode.set(41100, bpldByCode.get(41300)); // map scotland to UK
bpldByCode.set(41410, bpldByCode.get(41300)); // map NI to UK
bpldByCode.set(43610, bpldByCode.get(43600)); // map azores to portugal
bpldByCode.set(45200, bpldByCode.get(45213)); // map czechoslovakia to czech republic
bpldByCode.set(45700, bpldByCode.get(45710)); // map yugoslavia to croatia
bpldByCode.set(46590, bpldByCode.get(46500)); // map USSR to russia
bpldByCode.set(53440, bpldByCode.get(53400)); // map israel to israel/palestine
// Process census data
links.forEach(function(link) {
var source = bpldByCode.get(link.Birthplace),
target = bpldByCode.get(link.State),
weight = Math.round(parseFloat(link.Weight) * 100),
old;
if (weight > 0) {
old = source.emigrants.get(target.bpl);
if (old !== undefined) {
source.emigrants.set(target.bpl, old + weight);
}
else { source.emigrants.set(target.bpl, weight); }
old = target.immigrants.get(source.bpl);
if (old !== undefined) {
target.immigrants.set(source.bpl, old + weight);
}
else { target.immigrants.set(source.bpl, weight); }
}
});
// Extract polygons from topojson
var spolygons = topojson.feature(us, us.objects.layer1).features;
var cpolygons = topojson.feature(world, world.objects.countries).features;
// Count max number of incoming or outgoing people ("edges")
location.forEach(function(d) {
d.count = Math.max(d.immigrants.size(), d.emigrants.size());
});
// Filter those countries with no edges and class them as static
staticpolys = cpolygons.filter(function(d) {
d.bpld = countryToBpld.get(d.id);
if (d.bpld < 95000 && bpldByCode.get(d.bpld).count > 0) {
bpldToPoly.set(d.bpld, d);
}
else { return true; }
});
// All states should have edges so no need to filter
spolygons.forEach(function(d) {
bpldToPoly.set(stateToBpld.get(d.id), d);
});
// Filter objects down to those with both polygons and edges
var bpl_states = location.filter(function(d) {
if (d.bpl <= 11500 && bpldToPoly.get(d.bpl)) {
d.pop = d3.sum(d.immigrants.values())
d.totalnatives = d3.sum(d.emigrants.values())
return true;
}
});
var bpl_countries = location.filter(function(d) {
if (d.bpl > 11500 && d.bpl < 95000 && bpldToPoly.get(d.bpl)) {
d.totalnatives = d3.sum(d.emigrants.values())
return true;
}
});
// Create containers to hold each country
var countries = zoomable.append("g")
.attr("class", "countries");
var country = countries.selectAll("g")
.data(bpl_countries)
.enter().append("g")
.attr("class", "country")
.attr("id", function(d) { return "bpld-" + d.bpl; })
.attr("title", function(d) { return d.name; });
var statics = zoomable.append("g")
.attr("class", "statics")
.selectAll("path")
.data(staticpolys)
.enter().append("path")
.attr("class", "static")
.attr("d", path);
// Draw the borders between countries
zoomable.append("path")
.datum(topojson.mesh(world, world.objects.countries, function(a, b) { return a !== b; }))
.attr("class", "boundary")
.attr("d", path);
// Create state containers
var states = zoomable.append("g")
.attr("class", "states");
var state = states.selectAll("g")
.data(bpl_states)
.enter().append("g")
.attr("class", "state")
.attr("id", function(d) { return "bpld-" + d.bpl; })
.attr("title", function(d) { return d.name; });
// and draw state borders
zoomable.append("path")
.datum(topojson.mesh(us, us.objects.layer1, function(a, b) { return a !== b; }))
.attr("class", "boundary")
.attr("d", path);
// Draw the polygons themselves
d3.selectAll(".country, .state").append("path")
.datum(function(d) {
return bpldToPoly.get(d.bpl);
})
.attr("class", "polygon")
.attr("d", path);
// Add tipsy tooltips to each reguib
$(".country, .state").tipsy({
gravity: function() {
var name = this.__data__.name;
// Have tooltips for the northernmost regions display on the
// south side to avoid going offscreen
if (name == "Russia" || name == "Norway" || name == "Canada" || name == "Alaska")
{ return 'n'; }
else { return 's'; }},
html: true
});
// Add the principal interaction - clicking to explore migration
d3.selectAll(".state, .country").on("click", function() {
isState = d3.select(this).classed("state");
sourceMode = document.getElementById("SourceButton").checked;
// We don't have data on migration to countries - take no action
if (!isState && !sourceMode) { return; }
clearAll();
d = this.__data__;
domain = d3.map();
// Select appropriate array based on mode
if (sourceMode) { d.emigrants.entries().forEach(function(dest) { domain.set(dest.key, dest.value / bpldByCode.get(dest.key).pop *100); }); }
else { d.immigrants.entries().forEach(function(src) { domain.set(src.key, src.value / d.pop * 100); }); }
// Color the map
colorScale(domain);
// Rewrite the tooltips with details on demand
domain.keys().forEach(function(bpl) {
region = bpldByCode.get(bpl);
if (sourceMode) {
tooltip = region.name + "<br>" + (domain.get(bpl)).toFixed(3) +
"% " + d.name + " natives<br>" + d.emigrants.get(bpl) +
"/" + region.pop + " residents";
}
else {
tooltip = region.name + "<br>" + (domain.get(bpl)).toFixed(3) +
"% of " + d.name + " residents<br>= " + d.immigrants.get(bpl) +
"/" + d.pop + " people";
}
d3.select("#bpld-" + bpl)
.attr("original-title", tooltip);
});
// Color the selected country to keep visual state
d3.select(this).style("fill", "#F58F1B");
});
// On switching modes, clear coloring/scale. In destination mode, do
// not highlight countries on hover to show they cannot be
// interacted with
d3.select("#SourceButton")
.on("click", function() {
clearAll();
country
.attr("class", "country")
});
d3.select("#DestinationButton")
.on("click", function() {
clearAll();
country
.attr("class", "country_destmode")
});
}
function clearAll() {
d3.selectAll(".state, .country, .country_destmode")
.attr("original-title", function (d) { return d.name; })
.style("fill", null);
d3.selectAll("#legend, #laxis")
.style("visibility", "hidden");
}
function move() {
var t = d3.event.translate,
s = d3.event.scale;
t[0] = Math.min(width / 2 * (s - 1), Math.max(width / 2 * (1 - s), t[0]));
t[1] = Math.min(height / 2 * (s - 1) + 230 * s, Math.max(height / 2 * (1 - s) - 230 * s, t[1]));
zoom.translate(t);
zoomable.style("stroke-width", 1 / s).attr("transform", "translate(" + t + ")scale(" + s + ")");
}
function colorScale(linkmap) {
q = d3.scale.quantile().domain(linkmap.values()).range(colorbrewer.PuBu[7])
quantiles = q.quantiles().map(function(item) { return item; });
quantiles.unshift(0);
quantiles.push(d3.max(domain.values()));
qscale = d3.scale.quantile().domain(quantiles).range([0,20,40,60,80,100,120,140]);
var qaxis = d3.svg.axis()
.scale(qscale)
.orient("right")
.tickSize(6)
.ticks(8)
.tickFormat(d3.format(".3f"));
legendLabels
.call(qaxis);
linkmap.keys().forEach(function(bpl) {
d3.select("#bpld-" + bpl)
.style("fill", q(domain.get(bpl)));
d3.selectAll("#legend, #laxis")
.style("visibility", "visible");
});
}
</script>