forked from arnicas/interactive-vis-course
-
Notifications
You must be signed in to change notification settings - Fork 0
/
africa_map_tooltips_graph.html
444 lines (354 loc) · 10.2 KB
/
africa_map_tooltips_graph.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
<!DOCTYPE html>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<style>
body {
font-family: Helvetica, sans-serif;
padding: 40px;
}
.countries {
stroke: #fff;
stroke-width: 1px;
}
.legendLinear text {
font-size: 9px;
}
.chroniton text {
font-size: 10px;
color: gray;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.x.axis, .slider text {
-webkit-user-select:none;
-moz-user-select:none;
user-select:none;
}
.x.axis path.domain {
stroke-width:10;
stroke-linecap:round;
stroke:#ddd;
}
.x.axis path.halo {
stroke-width:12;
stroke-linecap:round;
stroke:#ccc;
}
.tick line {
stroke:#d0d0d0;
stroke-width:1;
transform:translate(0px, -5px);
}
.slider .handle {
fill: #fff;
stroke-width:1;
stroke:#333;
}
.slider .handle, .slider text {
cursor: move;
cursor: -webkit-grab;
transition:fill 200ms ease-in-out;
}
.handle:active, .slider text:active {
cursor: move;
cursor: -moz-grabbing;
cursor: -webkit-grabbing;
}
.slider.brushing.handle {
fill: #eee;
}
.label {
font:bold 12px sans-serif;
}
.tooltip {
position: absolute;
z-index: 10;
border: 1px gray solid;
background-color: white;
}
.tooltip p {
background-color: white;
padding: 2px;
margin-left: 10px;
max-width: 180px;
font-size: 11px;
color: maroon;
}
.tooltip svg {
background-color: white;
font-size: 9px;
}
.tooltip svg path.line {
fill:none;
stroke: maroon;
stroke-width: 1px;
}
.tooltip .x.axis path.domain {
fill: none;
stroke: gray;
stroke-width: 1;
}
.tooltip .y.axis path.domain {
fill: none;
stroke: gray;
stroke-width: 1;
}
.countries#selected {
stroke: gray;
stroke-width: 2px;
}
circle.dot {
fill: red;
stroke: none;
}
</style>
<body>
<h2>Fertility Rates in Africa</h2>
<p>Source: World Bank, truncated. Roll over African countries to see the curve in a line chart. Also push play to see the choropleth change over time.</p>
<div id="slider"></div>
<div id="vis"></div>
<script src="https://d3js.org/d3.v3.min.js"></script>
<script src="https://d3js.org/queue.v1.min.js"></script>
<script src="js/d3-legend.min.js"></script>
<script src="js/chroniton-only.js"></script>
<script src="https://d3js.org/topojson.v1.min.js"></script>
<script>
var width = 1000,
height = 600;
var svg = d3.select("#vis").append("svg")
.attr("width", width)
.attr("height", height);
var dateFormat = d3.time.format("%Y");
var projection = d3.geo.mercator()
.scale(400) // mess with this if you want
.translate([width / 2, height / 2]);
var path = d3.geo.path()
.projection(projection);
var colorScale = d3.scale.linear().range(["#fee0d2", "#de2d26"]).interpolate(d3.interpolateLab);
var countryById = d3.map();
var data = [];
var filterValue = "1960"; // starting year
var xScale, yScale, tooltipsvg, line, xAxis, yAxis;
var tooltip = d3.select("body").append("div").attr("class", "tooltip").style("display", "none");
// see examples in http://www.macwright.org/chroniton/example/
var slider = chroniton()
.domain([dateFormat.parse("1960"),dateFormat.parse("2013")])
.labelFormat(d3.time.format('%Y'))
.width(500)
.height(50)
.playButton(true) // can also be set to loop
.on("change", function(d) {
filterValue = dateFormat(d3.time.year(d));
redraw();
});
function typeFertility(d) {
d.fertility = +d.Fertility;
d.year = dateFormat.parse(d.Year);
d.country = d.Country;
return d;
}
function makeLookup(d) {
countryById.set(d.ISO3, d);
return d;
}
function getLineData(d) {
var dataRow = countryById.get(d.properties.adm0_a3_is); // best match
var dataVals;
if (dataRow) {
dataVals = data.get(dataRow.ShortName);
}
return dataVals;
}
function getData(d) {
var dataRow = countryById.get(d.properties.adm0_a3_is); // best match
var dataVal = null;
// must be a more elegant way to do this with all the checks, but i'm tired
if (dataRow) {
dataVal = data.get(dataRow.ShortName);
}
if (dataVal) {
//console.log("dataVal shortname", dataVal[0].Country);
dataVal = dataVal.filter(function (d) {
return d.Year == filterValue;
});
}
if (dataVal) {
dataVal = dataVal[0];
}
return dataVal;
}
function getColor(d) {
var dataVal = getData(d);
if (dataVal) {
return colorScale(dataVal.fertility);
}
// if we fall through, i.e., no match
//console.log("no dataRow", d);
return "#ccc";
}
function getText(d) {
var dataVal = getData(d);
if (dataVal) {
return dataVal.country + ": " + dataVal.fertility + " in " + filterValue;
} else {
return d.properties.name + ": No data.";
}
}
function loaded(error, africa, codes, fertility) {
data = d3.nest()
.key(function(d) {
return d.Country;
})
.map(fertility, d3.map); // this makes a map from the data for looking up data rows
colorScale.domain(d3.extent(fertility, function(d) { return +d.fertility;}));
var countries = topojson.feature(africa, africa.objects.collection).features;
svg.selectAll("path.countries")
.data(countries)
.enter()
.append("path")
.attr("class", "countries")
.attr("d", path)
.attr("fill", function(d,i) {
return getColor(d);
})
.on("mouseover", mouseover)
.on("mousemove", mousemove)
.on("mouseout", mouseout);
// The d3-legend component is called here:
svg.append("g")
.attr("class", "legendLinear")
.attr("transform", "translate(20,20)");
var legendLinear = d3.legend.color()
.shapeWidth(30)
.orient("horizontal")
.scale(colorScale);
svg.select(".legendLinear")
.call(legendLinear);
setupTooltipChart();
}
function mouseover(d) {
d3.select(this).attr("id", "selected");
d3.select(this).moveToFront();
tooltip.style("display", null);
drawLine(d); // updates the linechart
}
function mousemove(d) {
tooltip
.style("top", (d3.event.pageY - 10) + "px" )
.style("left", (d3.event.pageX + 10) + "px");
}
function mouseout(d) {
tooltip.style("display", "none");
d3.selectAll("path.countries").attr("id", null);
}
function setupTooltipChart() {
var fullwidth = 150;
var fullheight = 100;
var margin = {top: 10, right: 5, bottom: 20, left: 30};
var width = fullwidth - margin.left - margin.right;
var height = fullheight - margin.top - margin.bottom;
var outputFormat = d3.time.format("'%y");
// My shortcut for the scale is to list the first and last only - will set the extents.
// Also, I set the earlier year to 1999 to get a little spacing on the X axis.
var years = [dateFormat.parse("1960"), dateFormat.parse("2013")];
//Set up scales - I already know the start and end years, not using data for it.
xScale = d3.time.scale()
.range([ 0, width ])
.domain(d3.extent(years));
// don't know the yScale domain yet. Will set it with the data.
yScale = d3.scale.linear()
.range([ height, 0 ]);
//Configure axis generators
xAxis = d3.svg.axis()
.scale(xScale)
.orient("bottom")
.ticks(5)
.tickFormat(function(d) {
return outputFormat(d);
})
.innerTickSize([0])
.outerTickSize([0]);
yAxis = d3.svg.axis()
.scale(yScale)
.ticks(5)
.orient("left")
.innerTickSize([0])
.outerTickSize([0]);
//Configure line generator
line = d3.svg.line()
.x(function(d) {
return xScale(d.year);
})
.y(function(d) {
return yScale(+d.fertility);
});
tooltip.append("p"); // we'll put some text in it
//Create the empty SVG in the tooltip under the p
tooltipsvg = tooltip
.append("svg")
.attr("width", fullwidth)
.attr("height", fullheight)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
tooltipsvg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
tooltipsvg.append("g")
.attr("class", "y axis")
.call(yAxis);
tooltipsvg
.append("circle")
.classed("dot", true)
.attr("cy", yScale(0))
.attr("cx", xScale(dateFormat.parse(filterValue)))
.attr("r", 5);
}
function drawLine(d) {
tooltip.select("p").text(getText(d)); // adds title text to tooltip
var data = getLineData(d) || []; // if it's undefined, its an empty list
yScale.domain(d3.extent(data, function(d) { return +d.fertility; }));
var linechart = tooltipsvg.selectAll("path.line")
.data( [data] );
linechart
.enter()
.append("path")
.attr("class", "line")
.attr("d", line);
linechart.transition().attr("d", line);
linechart.exit().remove();
console.log(getData(d));
tooltipsvg.select("circle.dot").transition()
.attr("cy", yScale(+getData(d).Fertility))
.attr("cx", xScale(dateFormat.parse(filterValue)));
tooltipsvg.select("circle.dot").moveToFront();
tooltipsvg.selectAll(".x.axis").transition().call(xAxis);
tooltipsvg.selectAll(".y.axis").transition().call(yAxis);
} // end of draw lines
function redraw() {
// we might want to redo the color scale and legend here, not sure.
svg.selectAll("path.countries")
.transition()
.attr("fill", function(d,i) {
return getColor(d); // the filtervalue is a global in the func.
});
}
d3.selection.prototype.moveToFront = function() {
return this.each(function(){
this.parentNode.appendChild(this);
});
};
d3.select("#slider")
.call(slider);
// we use queue because we have 2 data files to load.
queue()
.defer(d3.json, "data/africa.topojson")
.defer(d3.csv, "data/World_Development_Indicators_Metadata_Countries.csv", makeLookup)
.defer(d3.csv, "data/fertility_long_africa.csv", typeFertility) // process
.await(loaded);
</script>
</body>
</html>