-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathscatter_data_transition_toggle.html
More file actions
286 lines (224 loc) · 6.85 KB
/
scatter_data_transition_toggle.html
File metadata and controls
286 lines (224 loc) · 6.85 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
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
<!DOCTYPE html>
<!-- A modified example from Scott Murray's Knight d3 course. -->
<html lang="en">
<head>
<meta charset="utf-8">
<title>Scatter Transition</title>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js"></script>
<style type="text/css">
body {
background-color: white;
font-family: Helvetica, Arial, sans-serif;
}
h1 {
font-size: 24px;
margin: 0;
}
p {
font-size: 14px;
margin: 10px 0 0 0;
}
.clicker {
font-weight: bolder;
color: black;
cursor: pointer;
}
.buttons {
margin-left: 500px;
padding: 20px;
}
.selected {
background-color: #eee;
}
.chart-title {
font-size: 14pt;
font-weight: bolder;
color: black;
margin-left: 300px;
}
svg {
background-color: white;
}
.dots {
fill: steelblue;
}
.highlighted {
fill: orange;
}
.axis path,
.axis line {
fill: none;
stroke: black;
shape-rendering: crispEdges;
}
.axis text {
font-family: sans-serif;
font-size: 11px;
}
.xlabel{
font-size: 15px;
fill: gray;
}
.ylabel{
font-size: 15px;
fill: gray;
}
</style>
</head>
<body>
<h1>Books and Toys at Home by Richest 20% and Poorest 20%</h1>
<p>Source: World Development Indicators, <a href="http://datacatalog.worldbank.org">World Bank SOWC All Countries Update 214</a>, latest data 2013</p>
<div class="buttons">
<button class="clicker" id="toys">Show Toys!</button>
<button class="clicker" id="books">Show Books!</button>
</div>
<p class="chart-title">Books at Home</p>
<script type="text/javascript">
var fullwidth = 700;
var fullheight = 600;
var margin = { top: 20, right: 10, bottom: 50, left: 50 };
var width = fullwidth - margin.left - margin.right;
var height = fullheight - margin.top - margin.bottom;
// redo this with an object for the margin settings: var margin = {top...}
var dotRadius = 3; // fix this to a nice value.
// fill in the margin values here. Also, we set domain to 0 to 100 since it's percents,
// plus some padding room!
var xScale = d3.scale.linear()
.range([ 0, width ])
.domain([-2,100]);
// top to bottom, padding just in case
var yScale = d3.scale.linear()
.range([ height, 0 ])
.domain([-2,100]);
// Custom tick count if you want it.
// Create your axes here.
var xAxis = d3.svg.axis()
.scale(xScale)
.orient("bottom")
.ticks(10); // fix this to a good number of ticks for your scale later.
var yAxis = d3.svg.axis()
.scale(yScale)
.orient("left");
var svg = d3.select("body")
.append("svg")
.attr("width", fullwidth)
.attr("height", fullheight)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
d3.csv("data/books_toys_by_rich_poor2013.csv", draw);
function draw(error, data) {
if (error) {
console.log("error reading data");
}
var circles = svg.selectAll("circle")
.data(data)
.enter()
.append("circle");
// Circles in SVG have cx, cy, and r attributes. x location, y location, radius.
circles.attr("cx", function(d) {
return xScale(+d.poorBooks);
// return the value to use for your x scale here
})
.attr("cy", function(d) {
return yScale(+d.richBooks);
})
.attr("class", function(d){
// highlighting some interesting outliers
if (d.country === "Ukraine" || d.country === "Mali" || d.country == "Morocco") {
return "highlighted";
}
else {
return "dots";
}
})
.append("title")
.text(function(d) {
return d.country + " reports books for " + d.poorBooks + "% kids in poor areas and " + d.richBooks + "% in rich areas.";
});
// adding a silly intro animation to catch the eye -- using transition:
circles.sort(function(a, b) {
return d3.ascending(+a.urban, +b.urban);
})
.transition()
.delay(function(d, i) {
return i * 10;
})
.duration(500) // milliseconds, so this is 1 second.
.attr("r", dotRadius);
// fix these translates so they use your margin and height width as needed
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
svg.append("text")
.attr("class", "xlabel")
.attr("transform", "translate(" + (width / 2) + " ," +
(height + 20) + ")")
.style("text-anchor", "middle")
.attr("dy", "12")
.text("Poorest 20%");
svg.append("text")
.attr("class", "ylabel")
.attr("transform","rotate(-90) translate(" + (-height/2) + ",0)")
.style("text-anchor", "middle")
.attr("dy", -25)
.text("Richest 20%");
// make the default data button look selected
d3.select("button#books").classed("selected", true);
// handle the click events on the 2 buttons:
d3.selectAll("button.clicker").on("click", function() {
// we use the id attr on each to see which data set to use.
var whichbutton = d3.select(this).attr("id");
// style the buttons to show which one was clicked:
d3.select(this).classed("selected", true);
if (whichbutton === "toys") {
d3.select("button#books").classed("selected", false);
} else {
d3.select("button#toys").classed("selected", false);
}
// The id is either toys or books. We just check which
// one and return the right data and text below:
circles
.transition()
.duration(2000)
.attr("cx", function(d) {
if (whichbutton === "toys") {
return xScale(+d.poorToys);
}
else { // then it's books:
return xScale(+d.poorBooks);
}
// return the value to use for your x scale here
})
.attr("cy", function(d) {
if (whichbutton === "toys") {
return yScale(+d.richToys);
} else {
return yScale(+d.richBooks);
}
});
// We can't transition the new title text -- must remove and rewrite.
circles.selectAll("title").remove();
circles
.append("title")
.text(function(d) {
if (whichbutton === "toys") {
return d.country + " reports toys for " + d.poorToys + "% kids in poor areas and " + d.richToys + "% in rich areas.";
} else {
return d.country + " reports books for " + d.poorBooks + "% kids in poor areas and " + d.richBooks + "% in rich areas.";
}
});
// change the visible title!
if (whichbutton === "toys") {
d3.select("p.chart-title").text("Toys at Home");
} else {
d3.select("p.chart-title").text("Books at Home");
}
}); // end clicker function
};
</script>
</body>
</html>