-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
221 lines (180 loc) · 5.14 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
<!DOCTYPE html>
<html>
<head>
<title>d3troit - the rise and fall of the population of Detroit City</title>
<script type="text/javascript" src="queue.min.js"></script>
<script type="text/javascript" src="d3.v2.min.js"></script>
<style type="text/css">
body {
font: 10px sans-serif;
}
h1 {
font: 300 32px Helvetica Neue;
}
text.title {
fill: #666666;
font: 300 32px Helvetica Neue;
}
#detroit rect {
fill: red;
}
.bar rect {
fill: steelblue;
}
.bar text.value {
fill: gray;
}
.axis {
shape-rendering: crispEdges;
}
.axis path {
fill: none;
}
.x.axis line {
stroke: #fff;
stroke-opacity: .8;
}
.y.axis path {
stroke: black;
}
</style>
</head>
<body>
<h1>The rise and fall of the population of Detroit City</h1>
<script type="text/javascript">
var n = 20; // number of cities to show
var margin = {top: 30, right: 10, bottom: 10, left: 100},
width = 960 - margin.right - margin.left,
height = 30 * n - margin.top - margin.bottom;
var format = d3.format(",.0f");
var x = d3.scale.linear()
.range([0, width]);
var y = d3.scale.ordinal()
.rangeRoundBands([0, height], .1);
var xAxis = d3.svg.axis()
.scale(x)
.orient("top")
.tickSize(-height);
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.tickSize(0);
var svg = d3.select("body").append("svg")
.attr("width", width + margin.right + margin.left)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var title = svg.append("text")
.attr("class", "title")
.attr("dy", ".71em")
.attr("transform", "translate(-100,-20)")
.text(1920);
var alldata; // full set of data
var data = {}; // data for year currently being displayed
var dates = []; // date labels
var tick = 0; // the index of the year currently being displayed
function csv(year, callback) {
d3.csv("data/" + year + ".csv", function(csv) {
csv.forEach(function(d) { d.value = +d.value; });
csv.sort(function(a, b) { return b.value - a.value; });
csv.length = n; // limit to top-n
dates.push(year);
csv ? callback(null, csv) : callback("error", null);
});
}
queue()
.defer(csv, "1860")
.defer(csv, "1870")
.defer(csv, "1880")
.defer(csv, "1890")
.defer(csv, "1900")
.defer(csv, "1910")
.defer(csv, "1920")
.defer(csv, "1930")
.defer(csv, "1940")
.defer(csv, "1950")
.defer(csv, "1960")
.defer(csv, "1970")
.defer(csv, "1980")
.defer(csv, "1990")
.defer(csv, "2000")
.defer(csv, "2010")
.await(function(error, results) {
dates.sort();
console.log(dates);
alldata = results;
data = alldata[tick];
// Set the scale domain using the largest value in the dataset.
x.domain([0, 1000000 + d3.max(results, function(result) {
return d3.max(result, function(d) { return +d.value; })
})]);
svg.append("g")
.attr("class", "x axis")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
redraw();
});
setInterval(function() {
if (tick < dates.length - 1) {
tick++;
redraw();
}
}, 1500);
function redraw() {
var transitionDuration = 1500;
data = alldata[tick];
title.text(dates[tick]);
y.domain(data.map(function(d) { return d.name; }));
svg.selectAll("rect")
.data(data, function(d) { return d.name; })
.transition()
.duration(transitionDuration)
.attr("width", function(d) { return x(d.value); })
.attr("height", y.rangeBand());
var bar = svg.selectAll("g.bar")
.data(data, function(d) { return d.name; })
var barEnter = bar.enter().append("g")
.attr("class", "bar")
.attr("id", function(d) { return d.name == "Detroit" ? "detroit" : null }) // Label Detroit bar
.attr("transform", function(d) { return "translate(0," + y(d.name) + ")"; });
barEnter.append("rect")
.attr("width", function(d) { return x(d.value); })
.attr("height", y.rangeBand());
barEnter.append("text")
.attr("class", "value")
.attr("x", function(d) { return x(d.value); })
.attr("y", y.rangeBand() / 2)
.attr("dx", 3)
.attr("dy", ".35em")
.attr("text-anchor", "start")
.text(function(d) { return format(d.value); });
d3.transition(bar.exit())
.remove();
bar.transition()
.duration(transitionDuration)
.attr("transform", function(d) { return "translate(0," + y(d.name) + ")"; });
svg.selectAll("text.value")
.data(data, function(d) { return d.name; })
.transition()
.duration(transitionDuration)
.attr("x", function(d) { return x(d.value); })
.attr("y", y.rangeBand() / 2)
.attr("dx", 3)
.attr("dy", ".35em")
.attr("text-anchor", "start")
.text(function(d) { return format(d.value); });
// update y axis label
svg.selectAll("g.y.axis")
.transition()
.duration(transitionDuration)
.attr("class", "y axis")
.call(yAxis);
}
</script>
<p>Data: <a href="http://www.census.gov/">US Census Bureau</a></p>
<p>Source: <a href="https://github.com/tomwhite/d3troit">github</a></p>
<p>Blog post: <a href="http://www.lexemetech.com/2012/12/d3troit.html">d3troit</a></p>
</body>
</html>