-
Notifications
You must be signed in to change notification settings - Fork 40
/
world_comparisons.html
338 lines (284 loc) · 11.2 KB
/
world_comparisons.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
<!DOCTYPE html>
<!-- This map code and design were inspired by the tutorial in Flowing Data: http://flowingdata.com/2015/02/19/make-an-interactive-map-with-category-filters/ -->
<head>
<title>Stunted Growth By Income Level</title>
<link rel="stylesheet" href="css/world_comparisons.css" type="text/css" media="screen" />
</head>
<div id="main-wrapper">
<h2>Stunted Growth in Children</h2>
<p>Source: <a href="http://data.unicef.org/nutrition/malnutrition.html">UNICEF</a>. Lack of proper nutrients leads to stunted growth in children. This problem is worse among rural and poor families. For more info: <a href="http://www.unicef.org/media/media_68734.html">"Progress Shows that Stunting in Children Can Be Defeated"</a>.</p>
<div id="metrics">
<div id="income">
<h3>Stunting by Income Group</h3>
<ul class="metrics_button">
<li data-metric="richest" class="selected richest">Richest</li>
<li data-metric="poorest" class="poorest">Poorest</li>
<li data-metric="differenceIncome" class="differenceInc">Difference</li>
</ul>
</div>
<div id="location">
<h3>Stunting by Location</h3>
<ul class="metrics_button">
<li data-metric="urban" class="urban">Urban</li>
<li data-metric="rural" class="rural">Rural</li>
<li data-metric="differenceLoc" class="differenceLoc">Difference</li>
</ul>
</div>
</div>
<div class="clr"></div>
<div id="tooltip" class="tooltip">
<h3 class="name"></h3>
<div data-metric="richest" class="line">
<div class="richest symbol"></div>Richest
<div class="richest val"></div>
</div>
<div data-metric="poorest" class="line">
<div class="poorest symbol"></div>Poorest
<div class="poorest val"></div>
</div>
<div data-metric="differenceIncome" class="line">
<div class="differenceIncome symbol"></div>Difference
<div class="differenceIncome val"></div>
</div>
<hr>
<div data-metric="urban" class="line">
<div class="urban symbol"></div>Urban
<div class="urban val"></div>
</div>
<div data-metric="rural" class="line">
<div class="rural symbol"></div>Rural
<div class="rural val"></div>
</div>
<div data-metric="differenceLoc" class="line">
<div class="differenceLoc symbol"></div>Difference
<div class="differenceLoc val"></div>
</div>
</div>
<div id="vis"></div>
</div><!-- @end #main-wrapper -->
<script src="https://d3js.org/d3.v3.min.js"></script>
<script src="https://d3js.org/queue.v1.min.js"></script>
<script src="https://d3js.org/topojson.v1.min.js"></script>
<script>
var width = 1000,
height = 700,
center = [width / 2, height / 2],
defaultFill = "#e0e0e0";
var baseColors = {
"richest": d3.scale.linear().range(["#ffffff", "#8dd3c7"]).interpolate(d3.interpolateLab),
"poorest": d3.scale.linear().range(["#ffffff", "#bc80bd"]).interpolate(d3.interpolateLab),
"differenceIncome": d3.scale.linear().range(["#ffffff", "#bebada"]).interpolate(d3.interpolateLab),
"urban": d3.scale.linear().range(["#ffffff", "#80b1d3"]).interpolate(d3.interpolateLab),
"rural": d3.scale.linear().range(["#ffffff", "#fdb462"]).interpolate(d3.interpolateLab),
"differenceLoc": d3.scale.linear().range(["#ffffff", "#b3de69"]).interpolate(d3.interpolateLab)
};
var countryById = d3.map(); // will have id's as keys for countries; see typeAndSet()
var projection = d3.geo.mercator()
.scale(200)
.translate([width / 2, height / 2]);
var path = d3.geo.path()
.projection(projection);
var zoom = d3.behavior.zoom()
.scaleExtent([1, 8])
.on("zoom", move);
var svg = d3.select("#vis").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.call(zoom);
svg.on("wheel.zoom", null);
svg.on("mousewheel.zoom", null);
svg.append("rect")
.attr("class", "overlay")
.attr("width", width)
.attr("height", height);
svg.append("image")
.attr("xlink:href", "img/progress-anim.gif")
.attr("id", "progress-image")
.attr("width", 43)
.attr("height", 11)
.attr("x", width / 2)
.attr("y", height / 2);
var g = svg.append("g");
var tooltip = d3.select("#tooltip")
.attr("class", "tooltip")
.style("opacity", 0);
var CURR_SELECT = "richest";
queue()
.defer(d3.json, "data/topojson/countries.json")
.defer(d3.csv, "data/stunting_by_wealth.csv", typeAndSet)
.await(ready);
function ready(error, world, stunting) {
console.log(error, world, stunting);
var domRich = d3.extent(stunting, function(d) {return d.richest;});
var domPoor = d3.extent(stunting, function(d) {return d.poorest;});
var domIncDiff = d3.extent(stunting, function(d) {return d.differenceIncome;});
var domUrban = d3.extent(stunting, function(d) {return d.urban;});
var domRural = d3.extent(stunting, function(d) {return d.rural;});
var domLocDiff = d3.extent(stunting, function(d) {return d.differenceLoc;});
baseColors["richest"].domain(domRich);
baseColors["poorest"].domain(domPoor);
baseColors["differenceIncome"].domain(domIncDiff);
baseColors["urban"].domain(domUrban);
baseColors["rural"].domain(domRural);
baseColors["differenceLoc"].domain(domLocDiff);
g.append("g")
.attr("class", "countries")
.selectAll("path")
.data(topojson.feature(world, world.objects.units).features)
.enter().append("path")
.attr("d", path)
.on("mouseover", mouseover)
.on("mouseout", function() {
d3.select(this).classed("selected", false);
tooltip.transition().duration(300)
.style("opacity", 0);
});
updateFill();
d3.selectAll("#metrics li")
.on("click", function() {
var item = d3.select(this);
d3.selectAll(".metrics_button li").classed("selected", false);
CURR_SELECT = item.attr("data-metric");
item.classed("selected", true);
item.classed(item.attr("data-metric"), true);
updateFill();
});
make_buttons(); // create the zoom buttons
svg.select("#progress-image").remove(); // remove animation for loading
} // end function ready
function make_buttons() {
// Zoom buttons actually manually constructed, not images
svg.selectAll(".scalebutton")
.data(['zoom_in', 'zoom_out'])
.enter()
.append("g")
.attr("id", function(d){return d;}) // id is the zoom_in and zoom_out
.attr("class", "scalebutton")
.attr({x: 20, width: 20, height: 20})
.append("rect")
.attr("y", function(d,i) { return 20 + 25*i })
.attr({x: 20, width: 20, height: 20})
// Plus button
svg.select("#zoom_in")
.append("line")
.attr({x1: 25, y1: 30, x2: 35, y2: 30 })
.attr("stroke", "#fff")
.attr("stroke-width", "2px");
svg.select("#zoom_in")
.append("line")
.attr({x1: 30, y1: 25, x2: 30, y2: 35 })
.attr("stroke", "#fff")
.attr("stroke-width", "2px");
// Minus button
svg.select("#zoom_out")
.append("line")
.attr({x1: 25, y1: 55, x2: 35, y2: 55 })
.attr("stroke", "#fff")
.attr("stroke-width", "2px");
svg.selectAll(".scalebutton")
.on("click", function() {
d3.event.preventDefault();
var scale = zoom.scale(),
extent = zoom.scaleExtent(),
translate = zoom.translate(),
x = translate[0], y = translate[1],
factor = (this.id === 'zoom_in') ? 2 : 1/2,
target_scale = scale * factor;
var clamped_target_scale = Math.max(extent[0], Math.min(extent[1], target_scale));
if (clamped_target_scale != target_scale){
target_scale = clamped_target_scale;
factor = target_scale / scale;
}
// Center each vector, stretch, then put back
x = (x - center[0]) * factor + center[0];
y = (y - center[1]) * factor + center[1];
// Transition to the new view over 350ms
d3.transition().duration(350).tween("zoom", function () {
var interpolate_scale = d3.interpolate(scale, target_scale),
interpolate_trans = d3.interpolate(translate, [x,y]);
return function (t) {
zoom.scale(interpolate_scale(t))
.translate(interpolate_trans(t));
svg.call(zoom.event);
};
});
});
}
function mouseover(d){
d3.select(this).classed("selected", true);
d3.select(this).moveToFront();
tooltip.transition().duration(100)
.style("opacity", 1);
tooltip
.style("top", (d3.event.pageY - 10) + "px" )
.style("left", (d3.event.pageX + 10) + "px");
tooltip.selectAll(".symbol").style("opacity", "0");
tooltip.selectAll(".val").style("font-weight", "normal");
tooltip.selectAll(".val").style("color", "darkgray");
tooltip.select(".symbol." + CURR_SELECT).style("opacity", "1");
tooltip.select(".val." + CURR_SELECT).style({
"font-weight": "bolder",
"color": "black"
});
if (countryById.get(d.id) && countryById.get(d.id)[CURR_SELECT]) {
tooltip.select(".name").text(countryById.get(d.id)['Country']);
tooltip.select(".richest.val").text(d3.round(countryById.get(d.id)["richest"]) + "%");
tooltip.select(".poorest.val").text(d3.round(countryById.get(d.id)["poorest"]) + "%");
tooltip.select(".differenceIncome.val").text(d3.round(countryById.get(d.id)["differenceIncome"]) + "%");
tooltip.select(".urban.val").text(d3.round(countryById.get(d.id)["urban"]) + "%");
tooltip.select(".rural.val").text(d3.round(countryById.get(d.id)["rural"]) + "%");
tooltip.select(".differenceLoc.val").text(d3.round(countryById.get(d.id)["differenceLoc"]) + "%");
} else {
tooltip.select(".name").text("No data for " + d.properties.name);
tooltip.select(".richest.val").text("NA");
tooltip.select(".poorest.val").text("NA");
tooltip.select(".differenceIncome.val").text("NA");
tooltip.select(".urban.val").text("NA");
tooltip.select(".rural.val").text("NA");
tooltip.select(".differenceLoc.val").text("NA");
}
} // end mouseover
function typeAndSet(d) {
d.richest = +d.Richest;
d.poorest = +d.Poorest;
d.differenceIncome = d.Poorest - d.Richest;
d.urban = +d.Urban;
d.rural = +d.Rural;
d.differenceLoc = d.Rural - d.Urban;
countryById.set(d.ISO, d); // this is a d3.map, being given a key, value pair.
return d;
}
function updateFill() {
svg.selectAll(".countries path")
.attr("fill", function(d) { return colorByCurrent(d.id) });
}
function colorByCurrent(FIPS) {
var countryObject = countryById.get(FIPS);
if ((typeof(countryObject) !== "undefined") && (countryObject[CURR_SELECT])) {
return baseColors[CURR_SELECT](countryObject[CURR_SELECT]);
}
else if (typeof(countryObject) == "undefined") {
return "#ccc";
} else if (!countryObject[CURR_SELECT]) {
return "#ccc";
}
}
d3.selection.prototype.moveToFront = function() {
return this.each(function(){
this.parentNode.appendChild(this);
});
};
function zoomIn() {
zoom.scale(zoom.scale()*2);
move();
}
function move() {
var t = d3.event.translate,
s = d3.event.scale;
t[0] = Math.min(width * (s - 1), Math.max(width * (1 - s), t[0]));
t[1] = Math.min(height * (s - 1), Math.max(height * (1 - s), t[1]));
zoom.translate(t);
g.style("stroke-width", 1 / s).attr("transform", "translate(" + t + ")scale(" + s + ")");
}
</script>