forked from devsar/d3talk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
population.html
179 lines (149 loc) · 4.58 KB
/
population.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
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<script type="text/javascript" src="d3/d3.js"></script>
<script type="text/javascript" src="d3/d3.csv.js"></script>
<link type="text/css" rel="stylesheet" href="style.css"/>
<style type="text/css">
svg {
font-size: 11px;
}
.rule line {
stroke: #fff;
stroke-opacity: .4;
stroke-width: .5px;
shape-rendering: crispEdges;
}
.rule:first-child line {
stroke: #000;
stroke-opacity: 1;
}
rect {
fill: none;
pointer-events: all;
}
.year rect {
fill-opacity: .6;
fill: #e377c2;
}
.year rect:first-child {
fill: #1f77b4;
}
</style>
</head>
<body>
<div id="body">
<div id="footer">
U.S. Population by Age, <span>…</span>
<div class="hint">click to change year</div>
</div>
</div>
<script type="text/javascript">
var m = [60, 120, 160, 120],
w = 1280 - m[1] - m[3],
h = 800 - m[0] - m[2],
x = d3.scale.linear().range([0, w]),
y = d3.scale.linear().range([0, h]);
// An SVG element with a bottom-right origin.
var svg = d3.select("#body").append("svg:svg")
.attr("width", w + m[1] + m[3])
.attr("height", h + m[0] + m[2])
.append("svg:g")
.attr("transform", "translate(" + (w + m[3]) + "," + (h + m[0]) + ")scale(-1,-1)");
// A sliding container to hold the bars.
var body = svg.append("svg:g")
.attr("transform", "translate(0,0)");
// A container to hold the y-axis rules.
var rules = svg.append("svg:g");
d3.csv("population.csv", function(data) {
// Convert strings to numbers.
data.forEach(function(d) {
d.people = +d.people;
d.year = +d.year;
d.age = +d.age;
});
// Compute the extent of the data set in age and years.
var age0 = 0,
age1 = d3.max(data, function(d) { return d.age; }),
year0 = d3.min(data, function(d) { return d.year; }),
year1 = d3.max(data, function(d) { return d.year; }),
year = 1930;
// Update the scale domains.
x.domain([0, age1 + 5]);
y.domain([0, d3.max(data, function(d) { return d.people; })]);
// Add rules to show the population values.
rules = rules.selectAll(".rule")
.data(y.ticks(10))
.enter().append("svg:g")
.attr("class", "rule")
.attr("transform", function(d) { return "translate(0," + y(d) + ")"; });
rules.append("svg:line")
.attr("x2", w);
rules.append("svg:text")
.attr("x", 6)
.attr("dy", ".35em")
.attr("transform", "rotate(180)")
.text(function(d) { return Math.round(d / 1e6) + "M"; });
// Add labeled rects for each birthyear.
var years = body.selectAll("g")
.data(d3.range(year0 - age1, year1 + 5, 5))
.enter().append("svg:g")
.attr("class", "year")
.attr("transform", function(d) { return "translate(" + x(year1 - d) + ",0)"; });
years.selectAll("rect")
.data(d3.range(2))
.enter().append("svg:rect")
.attr("x", 1)
.attr("width", x(5) - 2)
.attr("height", 1e-6);
years.append("svg:text")
.attr("y", -6)
.attr("x", -x(5) / 2)
.attr("transform", "rotate(180)")
.attr("text-anchor", "middle")
.style("fill", "#fff")
.text(String);
// Add labels to show the age.
svg.append("svg:g").selectAll("text")
.data(d3.range(0, age1 + 5, 5))
.enter().append("svg:text")
.attr("text-anchor", "middle")
.attr("transform", function(d) { return "translate(" + (x(d) + x(5) / 2) + ",-4)scale(-1,-1)"; })
.attr("dy", ".71em")
.text(String);
// Nest by year then birthyear.
data = d3.nest()
.key(function(d) { return d.year; })
.key(function(d) { return d.year - d.age; })
.rollup(function(v) { return v.map(function(d) { return d.people; }); })
.map(data);
redraw();
svg.append("svg:rect")
.attr("x", 0)
.attr("width", w / 2)
.attr("height", h)
.style("cursor", "e-resize")
.on("click", function() { year = Math.min(year1, year + 10); redraw(); });
svg.append("svg:rect")
.attr("x", w / 2)
.attr("width", w / 2)
.attr("height", h)
.style("cursor", "w-resize")
.on("click", function() { year = Math.max(year0, year - 10); redraw(); });
function redraw() {
if (!(year in data)) return;
d3.select("#footer span").text(year);
body.transition()
.duration(750)
.attr("transform", function(d) { return "translate(" + x(year - year1) + ",0)"; });
years.selectAll("rect")
.data(function(d) { return data[year][d] || [0, 0]; })
.transition()
.duration(750)
.attr("height", y);
}
});
</script>
</body>
</html>