-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathbostock_bar_updates_simpler.html
More file actions
234 lines (177 loc) · 6.03 KB
/
bostock_bar_updates_simpler.html
File metadata and controls
234 lines (177 loc) · 6.03 KB
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
<html lang="en">
<head>
<meta charset="utf-8">
<title>Object Constancy</title>
<style>
body {
background: #fcfcfa;
color: #333;
margin: 1em auto 4em auto;
position: relative;
width: 960px;
}
svg {
font: 10px sans-serif;
}
.bar rect {
fill: steelblue;
}
.bar rect:hover {
fill: orange;
}
.value {
fill: white;
}
.axis {
shape-rendering: crispEdges;
}
.axis path {
fill: none;
stroke: none;
}
.x.axis path {
fill: none;
stroke: white;
}
.x.axis line {
stroke: #fff;
stroke-opacity: .8;
}
.y.axis path {
stroke: black;
}
</style>
</head>
<body>
<h2>Slightly Simplified Code from M. Bostock's Object Constancy Example</h2>
<p>Source of original: <a href="http://bost.ocks.org/mike/constancy/">http://bost.ocks.org/mike/constancy/</a></p>
<p id="menu"><b>Top States by Age Bracket, 2008</b><br>Age: <select></select>
<p id="chart"></p>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js"></script>
<script>
var fullwidth = 960,
fullheight = 280;
var margin = {top: 20, right: 40, bottom: 10, left: 40},
width = fullwidth - margin.right - margin.left;
height = fullheight - margin.top - margin.bottom;
var percentFormat = d3.format(".1%"),
states,
prevAge;
var xScale = d3.scale.linear()
.range([0, width]);
var yScale = d3.scale.ordinal()
.rangeRoundBands([0, height], .1);
var xAxis = d3.svg.axis()
.scale(xScale)
.orient("top")
.tickFormat(percentFormat);
var svg = d3.select("#chart").append("svg")
.attr("width", fullwidth)
.attr("height", fullheight)
.style("margin-left", -margin.left + "px")
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
svg.append("g")
.attr("class", "x axis");
svg.append("g")
.attr("class", "y axis")
.append("line")
.attr("class", "domain")
.attr("y2", height);
var menu = d3.select("#menu select")
.on("change", redraw);
d3.csv("data/states-age.csv", function(data) {
states = data;
// remember d3.keys gets us the column names from these objects, using first row:
var ages = d3.keys(states[0]).filter(function(key) {
return key != "State" && key != "Total"; // this will return columns NOT MATCHING those
});
console.log(ages);
// do some calculations for each percentage
states.forEach(function(state) {
ages.forEach(function(age) {
state[age] = state[age] / state.Total;
});
});
// build menu from the data; could have been done by hand.
menu.selectAll("option")
.data(ages)
.enter().append("option")
.text(function(d) { return d; });
menu.property("value", "18 to 24 Years");
// redraw in this case draws the UI - it uses a variable to determine data column.
redraw();
}); // end load csv
function redraw() {
// get the age for the data access off the menu:
var currentAge = menu.property("value");
// sort by it and then take the top 10 using slice!
var top10 = states.sort(function(a, b) {
return b[currentAge] - a[currentAge];
})
.slice(0, 10);
yScale.domain(top10.map(function(d) { return d.State; })); // the y scale is ordinal, all the state names
var bar = svg.selectAll(".bar")
.data(top10, function(d) { return d.State; }); // key function!
var barCreate = bar.enter().append("g")
.attr("class", "bar")
.attr("transform", function(d) { return "translate(0," + (yScale(d.State) + height) + ")"; })
.style("fill-opacity", 0);
console.log("age", prevAge); // this is undefined the first time thru.
barCreate.append("rect")
//.attr("width", age && function(d) { return x(d[age]); })
.attr("width", function(d) {
if (prevAge) {
return xScale(d[prevAge]);
}
})
.attr("height", yScale.rangeBand());
barCreate.append("text")
.attr("class", "label")
.attr("x", -3)
.attr("y", yScale.rangeBand() / 2)
.attr("dy", ".35em")
.attr("text-anchor", "end")
.text(function(d) { return d.State; });
barCreate.append("text")
.attr("class", "value")
//.attr("x", age && function(d) { return x(d[age]) - 3; })
.attr("x", function(d) {
if (prevAge) {
return xScale(d[prevAge]) - 3;
}
})
.attr("y", yScale.rangeBand() / 2)
.attr("dy", ".35em")
.attr("text-anchor", "end");
prevAge = currentAge;
// they are sorted, so take the top value for the max on the domain here:
xScale.domain([0, top10[0][currentAge]]);
// sets the d.yLocation for the bar that's used in the exit transition.
var barUpdate = bar.transition()
.attr("transform", function(d) {
// set the value of the current y location for the exit move when it goes.
d.yLocation = yScale(d.State);
return "translate(0," + yScale(d.State) + ")"; })
.style("fill-opacity", 1);
barUpdate.select("rect")
.attr("width", function(d) { return xScale(d[currentAge]); });
barUpdate.select(".value")
.attr("x", function(d) { return xScale(d[currentAge]) - 3; })
.text(function(d) { return percentFormat(d[currentAge]); });
var barExit = bar.exit().transition()
.attr("transform", function(d) { return "translate(0," + (d.yLocation + height) + ")"; })
.style("fill-opacity", 0)
.remove();
barExit.select("rect")
.attr("width", function(d) { return xScale(d[currentAge]); });
barExit.select(".value")
.attr("x", function(d) { return xScale(d[currentAge]) - 3; })
.text(function(d) { return percentFormat(d[currentAge]); });
// transition the axis, so easy if you fixed the domain!
svg.transition().select(".x.axis")
.call(xAxis);
}
</script>
</body>
</html>