forked from arnicas/interactive-vis-course
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scatter_data_update.html
360 lines (285 loc) · 8.67 KB
/
scatter_data_update.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
<!DOCTYPE html>
<!-- A modified example from Scott Murray's Knight d3 course. -->
<html lang="en">
<head>
<meta charset="utf-8">
<title>Toys at Home: Update Data</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;
padding: 50px;
}
h1 {
font-size: 24px;
margin: 0;
}
p {
font-size: 14px;
margin: 10px 0 0 0;
}
.clicker {
font-weight: bolder;
color: red;
cursor: pointer;
}
svg {
background-color: white;
}
.dots {
fill: steelblue;
opacity: .7;
}
.dotlabels {
font-size: 10px;
color: black;
}
.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;
}
#menu {
padding-left: 500px;
font-weight: bolder;
}
</style>
</head>
<body>
<h1>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>
<p id="menu">
<select>
<option value="all">All Countries</option>
<option value="rich">10 Richest</option>
<option value="poor">10 Poorest</option>
</select>
</p>
<script type="text/javascript">
// All the global setup is here:
var fullwidth = 700;
var fullheight = 600;
var margin = { top: 20, right: 10, bottom: 50, left: 50 };
var width = fullwidth - margin.right - margin.left;
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([-1, 100]);
// top to bottom, padding just in case
var yScale = d3.scale.linear()
.range([ height, 0 ])
.domain([-1, 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.right + ")");
// utility for label placement jitter
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
d3.csv("data/books_toys_by_rich_poor2013.csv", function(data) {
var menu = d3.select("#menu select")
.on("change", filter);
// Set the intial value of drop-down when page loads
menu.property("value", "all");
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 + 25) + ")")
.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", -30)
.text("Richest 20%");
var curSelection = menu.property("value");
render(data); // do the full dataset render first.
// Functions for handling updates and drawing with data
function filter() {
// Handle the menu change -- filter the data set if needed, rerender:
curSelection = menu.property("value");
if (curSelection === "all") {
var newData = data; // set it equal to all the data
} else if (curSelection === "rich") { //poorest 10
var newData = data.sort(function(a,b) {
return b.poorToys - a.poorToys;
}).slice(0, 10);
} else if (curSelection === "poor") { // descending
var newData = data.sort(function(a,b) { // richest 10
return a.poorToys - b.poorToys;
}).slice(0, 10);
}
console.log(newData);
render(newData);
}
function render(mydata) {
// Here we set domains, and draw the circles based on what data set it is.
xScale.domain([
d3.min(mydata, function(d) {
return +d.poorToys;
}) - 2,
d3.max(mydata, function (d) {
return +d.poorToys;
}) + 2
]);
yScale.domain([
d3.min(mydata, function(d) {
return +d.richToys;
}) - 2,
d3.max(mydata, function (d) {
return +d.richToys;
}) + 2
]);
// data join
var circles = svg.selectAll("circle")
.data(mydata, function(d) {return d.country;}); // key function!
// enter and create new ones if needed
circles
.enter()
.append("circle")
// this section is to fix some of the animation direction problems
.attr("cx", function (d) {
if (curSelection == "rich") {
return width - margin.right;
}
else if (curSelection == "poor") {
return margin.left;
}
})
.attr("cy", function (d) {
if (curSelection == "rich") {
return margin.top;
}
else if (curSelection == "poor") {
return height - margin.bottom;
}
}) // */
.attr("class", "dots")
.append("title")
.text(function(d) {
return d.country + " reports toys for " + d.poorToys + "% kids in poor areas and " + d.richToys + "% in rich areas.";
});
// transition of them
circles
.transition()
.duration(2000)
.attr("cx", function(d) {
return xScale(+d.poorToys);
// return the value to use for your x scale here
})
.attr("cy", function(d) {
return yScale(+d.richToys);
})
.attr("r", function() {
if (curSelection !== "all") {
return dotRadius * 2;
}
else {
return dotRadius;
}
});
// fade out the ones that aren't in the current data set
circles
.exit()
.transition()
.duration(1000)
.style("opacity", 0)
.remove();
// Update the axes - also animated. this is really easy.
svg.select(".x.axis")
.transition()
.duration(1000)
.call(xAxis);
// Update Y Axis
svg.select(".y.axis")
.transition()
.duration(1000)
.call(yAxis);
// label the dots if you're only showing 10.
if (curSelection !== "all") {
// data join with a key
var labels = svg.selectAll("text.dotlabels")
.data(mydata, function(d) {
return d.country;
});
// enter and create any news ones we need. Put minimal stuff here.
labels
.enter()
.append("text")
.attr("transform", function(d) {
return "translate(" + xScale(+d.poorToys) + "," + yScale(+d.richToys) + ")";
})
.attr("dx", 5)
.attr("dy", function(d) {
// adding some space for the overlapping ones
if (d.country == "Costa Rica" || d.country == "Uzbekistan") {
return -12;
} else {
return -4;
}
})
.attr("class", "dotlabels")
.style("opacity", 0)
.text(function(d) {return d.country});
// transition them.
labels.transition()
.duration(2000)
.style("opacity", 1);
// remove ones that we don't have now
labels.exit().remove(); // these could have a transition too...
} else {
// if we're showing "all countries" - fade out any labels.
svg.selectAll("text.dotlabels")
.transition()
.duration(1000)
.style("opacity", 0)
.remove();
}
} // end of render
});
</script>
</body>
</html>