-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathlinked_small_mults_d3pure.html
434 lines (357 loc) · 10.6 KB
/
linked_small_mults_d3pure.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
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<style>
.container {
width: 1050px;
}
h1 {
font-size: 26px;
padding: 0px 0 5px 0;
line-height: 1.2em;
color: #000;
margin-bottom: 0px;
}
#lead {
clear: both;
width: 600px;
}
#lead p {
color: #444;
font-family: Georgia,Palatino,serif;
font-size: 1.0em;
line-height: 1.4em;
}
#vis {
position: relative;
display: block;
}
.chart {
position: absolute;
padding-right: 5px;
padding-bottom: 5px;
padding-top:0;
padding-left:0;
}
svg {
font-size: 9px;
}
.background {
fill: none;
}
.area {
fill: #99CCFF;
}
.line {
fill: none;
stroke: #334C80;
stroke-width: 1.4px;
}
.title {
font-size: 12px;
}
.axis line, .grid line {
stroke-width: .5px;
shape-rendering: crispEdges;
stroke: #fff;
opacity: 0.5;
pointer-events: none;
}
.axis path {
display: none;
}
.hidden {
display:none;
opacity: 0.0;
}
#button-wrap {
width: 400px;
height: 50px;
margin-left: auto;
margin-right: auto;
margin-bottom: 10px;
padding-left: 5px;
}
.button {
cursor: pointer;
float: left;
height: 30px;
text-align: center;
background: #fcfcfc;
border: 1px solid #cecece;
display: block;
padding: 5px 15px;
border-radius: 5px 5px 5px 5px;
}
.button.active {
box-shadow: inset 0px 0px 7px rgba(0,0,0,0.18);
pointer-events: none;
background-color: #e6e6e6;
}
#count {
border-radius: 5px 0px 0px 5px;
}
#name {
border-radius: 0px 5px 5px 0px;
}
</style>
</head>
<body>
<div class="container">
<h1>Deaths Due to HIV/AIDS for African Children Under 4 Years Old</h1>
<div id="lead">
<p>Data Source: WHO, selected African countries. Based on <a href="https://flowingdata.com/2014/10/15/linked-small-multiples/">tutorial code by Jim Vallandingham.</a> and an example at <a href="http://britains-diet.labs.theodi.org/?es_p=1359956">Britain's Diet.</a>.</p>
</div>
<div id="main">
<div id="button-wrap">
<div id="count" class="button active">WORST</div>
<div id="decline" class="button">BIGGEST DROP</div>
<div id="country" class="button">COUNTRY</div>
</div>
<div id="vis"></div>
</div>
</div> <!--! end of #container -->
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js"></script>
<script type="text/javascript">
var fullwidth = 150,
fullheight = 120,
margin = {
top: 15,
right: 20,
bottom: 40,
left: 40
};
var width = fullwidth - margin.left - margin.right;
var height = fullheight - margin.top - margin.bottom;
var illness= "HIV_Aids";
//var illness = "Malaria";
var data = [],
circle = null,
caption = null,
curYear = null;
var bisect = d3.bisector(function(d) {
return d.date;
}).left;
var format = d3.time.format("%Y");
var xScale = d3.time.scale().range([0, width]).clamp(true);
var yScale = d3.scale.linear().range([height, 0]).clamp(true);
var xValue = function(d) {
return d.date;
};
var yValue = function(d) {
return d.count;
};
var yAxis = d3.svg.axis()
.scale(yScale)
.orient("left")
.ticks(3)
.outerTickSize(0)
.tickFormat(d3.format("s")); // adds the "k" for thousands
var area = d3.svg.area().x(function(d) {
return xScale(xValue(d));
}).y0(height).y1(function(d) {
return yScale(yValue(d));
});
// line on top of the area plot
var line = d3.svg.line().x(function(d) {
return xScale(xValue(d));
}).y(function(d) {
return yScale(yValue(d));
});
function setupScales(data) {
var extentX, maxY;
maxY = d3.max(data, function(c) {
return d3.max(c.values, function(d) {
return yValue(d);
});
});
maxY = maxY + (maxY * 1 / 4);
yScale.domain([0, maxY]);
extentX = d3.extent(data[0].values, function(d) {
return xValue(d);
});
return xScale.domain(extentX);
}
function transformData(rawData) {
var format, nest;
format = d3.time.format("Year %Y");
rawData.forEach(function(d) {
d.date = format.parse(d.Year);
d.count = +d[illness]; // variable set above, a global
});
var nest = d3.nest().key(function(d) {
return d.Country;
}).sortValues(function(a, b) {
return d3.ascending(a.date, b.date);
}).entries(rawData);
nest = nest.filter(function(d) {return d.values.length == 14;});
return nest;
}
// borrowed from british cooking example: http://britains-diet.labs.theodi.org/?es_p=1359956
var cols, margin_left;
function calibrate() {
cols = Math.floor(window.innerWidth/fullwidth),
margin_left = window.innerWidth%fullwidth/2;
console.log("cols", cols);
}
function getLeft(i) {
return margin_left + fullwidth * (i%cols) + "px";
}
function getTop(i) {
return fullheight * Math.floor(i/cols) + "px";
}
function setChartDivHeight(data_to_plot) {
d3.select("#vis").style("height", fullheight * Math.ceil(data_to_plot.length/cols) + "px")
}
function layoutCharts(data_to_plot) {
calibrate();
setChartDivHeight(data_to_plot);
setupScales(data_to_plot);
var charts = d3.select("#vis").selectAll(".chart").data(data_to_plot, function(d) { return d.key; });
charts.enter().append("div")
.attr("class", "chart")
.attr("id", function(d) { return "chart-" + d.key; })
.style("left", function(d, i) { return getLeft(i); })
.style("top", function(d, i) { return getTop(i); })
.each(appendChart);
charts
.transition().duration(750).delay(function(d, i) { return i * 10; })
.style("left", function(d, i) { return getLeft(i); })
.style("top", function(d, i) { return getTop(i); });
charts.exit().remove();
} // end layout charts
var data;
d3.csv("data/deaths_04yearsold_excerpt.csv", drawPlots);
function drawPlots(error, rawData) {
if (error) {
console.log(error);
};
data = transformData(rawData);
// default sort order
data.sort(function(a, b) {
return d3.descending(+a.values[b.values.length-1].count, +b.values[a.values.length-1].count);
});
layoutCharts(data);
} // end drawPlots
function appendChart(data, i) {
var svg = d3.select(this).append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
svg.append("rect")
.attr("class", "background")
.style("pointer-events", "all")
.attr("width", width + margin.right) // extra space for labels that appear
.attr("height", height).on("mouseover", mouseover)
.on("mousemove", mousemove)
.on("mouseout", mouseout);
var lines = svg.append("g");
lines.append("path")
.attr("class", "area")
.style("pointer-events", "none")
.attr("d", function(c) {
return area(c.values);
});
lines.append("path")
.attr("class", "line")
.style("pointer-events", "none")
.attr("d", function(c) {
return line(c.values);
});
lines.append("text")
.attr("class", "title")
.attr("text-anchor", "middle")
.attr("y", height)
.attr("dy", margin.bottom / 2 + 5)
.attr("x", width / 2).text(function(c) {
return c.key;
});
lines.append("text")
.attr("class", "static_year")
.attr("text-anchor", "start")
.style("pointer-events", "none")
.attr("dy", 13).attr("y", height)
.attr("x", 0).text(function(c) {
return xValue(c.values[0]).getFullYear();
});
lines.append("text")
.attr("class", "static_year")
.attr("text-anchor", "end")
.style("pointer-events", "none").attr("dy", 13)
.attr("y", height).attr("x", width).text(function(c) {
return xValue(c.values[c.values.length - 1]).getFullYear();
});
circle = lines.append("circle")
.attr("r", 2.2)
.attr("opacity", 0)
.style("pointer-events", "none");
caption = lines.append("text")
.attr("class", "caption")
.attr("text-anchor", "middle")
.style("pointer-events", "none")
.attr("dy", -8);
curYear = lines.append("text")
.attr("class", "year")
.attr("text-anchor", "middle")
.style("pointer-events", "none")
.attr("dy", 13)
.attr("y", height);
svg.append("g").attr("class", "y axis").call(yAxis);
} // end of append chart
d3.select("#button-wrap").selectAll("div").on("click", function() {
var id;
id = d3.select(this).attr("id");
d3.select("#button-wrap").selectAll("div").classed("active", false);
d3.select("#" + id).classed("active", true);
// sort methods
if (id == "country") {
data.sort(function(a, b) { return d3.ascending(a.key, b.key);}); // key is country
}
if (id == "count") {
data.sort(function(a, b) {
return d3.descending(+a.values[a.values.length-1].count, +b.values[b.values.length-1].count);
});
}
if (id == "decline") {
data.sort(function(a, b) {
return d3.descending(a.values[0].count - a.values[a.values.length-1].count,
b.values[0].count - b.values[b.values.length-1].count);
});
}
layoutCharts(data);
}); // end button setup
function mouseover() {
d3.selectAll("circle").attr("opacity", 1.0);
d3.selectAll(".static_year").classed("hidden", true);
return mousemove.call(this); // current graph base
};
function mousemove() {
var date, index, year;
year = xScale.invert(d3.mouse(this)[0]).getFullYear();
date = format.parse('' + year);
index = 0;
d3.selectAll("circle")
.attr("cx", xScale(date))
.attr("cy", function(c) {
index = bisect(c.values, date, 0, c.values.length - 1);
return yScale(yValue(c.values[index]));
});
d3.selectAll("text.caption").attr("x", xScale(date))
.attr("y", function(c) {
return yScale(yValue(c.values[index]));
}).text(function(c) {
return yValue(c.values[index]);
});
d3.selectAll("text.year").attr("x", xScale(date)).text(year);
};
function mouseout() {
d3.selectAll(".static_year").classed("hidden", false);
d3.selectAll("circle").attr("opacity", 0);
d3.selectAll("text.caption").text("");
d3.selectAll("text.year").text("");
};
</script>
</body>
</html>