-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworld_cup_attendance.html
executable file
·379 lines (299 loc) · 10.6 KB
/
world_cup_attendance.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://d3js.org/d3.v3.min.js"></script>
<style>
#chart {
float: top;
}
.title {
text-align: center;
}
div.years_buttons {
position: fixed;
top: 5px;
left: 50px;
cursor: pointer;
}
div.years_buttons div {
background-color: rgb(251, 201, 127);
padding: 3px;
margin: 7px;
}
.legend {
position:absolute;
width:100%;
height:100%;
top: 15px;
font-size: 1.35em;
display: block;
cursor: pointer;
}
.legend_container {
text-align: center;
position: fixed;
top: 5px;
right: 5px;
border: solid black 2px;
width:140;
height:100;
}
.legend_title {
text-align: center;
}
</style>
<script type="text/javascript">
function draw(geo_data) {
"use strict";
var margin = 75,
width = 1200 - margin,
height = 500 - margin;
var legendRectSize = 180;
var legendSpacing = 40;
//years of the games
var years = [];
for(var i = 1930; i < 2015; i += 4) {
if(i !== 1942 && i !== 1946) {
years.push(i);
};
}
//show a legend
var svg_legend = d3.select('body')
.append('svg');
//make a title
d3.select("#chart")
.append("h2")
.attr("class", "title")
.text("World Cup");
var svg = d3.select("#chart")
.append("svg")
.attr("width", width + margin)
.attr("height", height + margin)
.append('g')
.attr('class', 'map');
//scale / projection
var projection = d3.geo.mercator()
.scale(145)
.translate([width / 2, height / 1.19]); //translalte ( [] )
//the function that's passed the data, px = path(data)
var path = d3.geo.path().projection(projection);
//we create the svg elements representing the countries
var map = svg.selectAll('path')
.data(geo_data.features)
.enter()
.append('path')
.attr('d', path);
//we add some style so we can see them
map.attr('fill', 'steelblue')
.attr('stroke', 'black')
.attr('stroke-width', 0.5);
//now get the world cup data and draw the attendance circles
function plot_points(data){
//draw circles logic
var nested = d3.nest()
//group by
.key( function(d) {
return d['date'].getUTCFullYear(); //year of the game
})
//æggregate
.rollup( function(leaves) {
var total = d3.sum(leaves, function(d) {
return d['attendance'];
});
var coords = leaves.map(function(d) {
return projection( [+d.long, +d.lat] );
} );
var center_x = d3.mean(coords, function(d) {
return d[0];
});
var center_y = d3.mean(coords, function(d) {
return d[1];
});
var teams = d3.set();
leaves.forEach(function(d) {
teams.add(d['team1']);
teams.add(d['team2']);
});
var home = d3.set(); //same for all leaves
leaves.forEach(function(d) {
home.add(d['home']);
});
return {
'attendance': total,
'x': center_x,
'y': center_y,
'teams' : teams.values(),
'home' : home.values()
};
})
.entries(data)
//end nested
//create scale for the radius,as did the projection for the coords
//extent the data to work on a copy of the arrendance col
var attendance_extent = d3.extent(nested, function(d) {
return d.values['attendance'];
});
//create the scale for the circle radius
var radius = d3.scale.sqrt()
.domain(attendance_extent)
.range([0,12]);
//create the circles
svg.append('g')
.attr('class', 'bubble')
.selectAll('circle')
.data(nested.sort(function(a, b){ //make sure big circles dont hide small ones
return b.values['attendance'] - a.values['attendance'];
}), function(d) {
return d['key']; //year
})
.enter()
.append('circle')
.attr('cx', function(d) {return d.values['x']; })
.attr('cy', function(d) {return d.values['y']; })
.attr('r', function(d) {
return radius(d.values['attendance']);
})
.attr('fill', 'rgb(247, 148, 32)')
.attr('stroke', 'black')
.attr('stroke-width', 0.7)
.attr('opacity', 0.7);
//.attr('fill', 'brown')
function update(year) {
//get data for given year
var filtered = nested.filter(function(d) {
return new Date(d['key']).getUTCFullYear() === year;
});
//change the title
d3.select("h2")
.text("World Cup " + year);
//show the legend
var legend = svg_legend.attr('class', 'legend_container')
.selectAll('g')
.data(filtered, function(d) {
//key function
return d['key'];
});
legend.exit().remove();
var update_legend = legend.enter()
.append('g')
.attr("class", "legend")
.attr('transform', function(d, i) {
return 'translate(' + 200 + ',' + 50 + ')';
});
update_legend.append('circle')
.attr('float', 'left')
//.attr('cx', function(d) {return d.values['x']; })
//.attr('cy', function(d) {return d.values['y']; })
.attr('r', function(d) {
return radius(d.values['attendance']);
})
.attr('fill', 'rgb(247, 148, 32)')
.attr('stroke', 'black')
.attr('stroke-width', 0.7)
.attr('opacity', 0.7)
.attr('transform', function(d, i) {
return 'translate(' + -70 + ',' + 40 + ')';
});
update_legend.append('text')
.attr('float', 'right')
.text(function(d) {
//key function
return "Attendance: " + d.values['attendance'];
})
.attr('transform', function(d, i) {
return 'translate(' + -150 + ',' + -20 + ')';
});
//attendance circles
var circles = svg.selectAll('circle')
.data(filtered, function(d) {
//key function
return d['key']; //year
});
circles.exit().remove();
circles.enter()
.append('circle')
.transition()
.duration(500)
.attr('cx', function(d) {return d.values['x']; })
.attr('cy', function(d) {return d.values['y']; })
.attr('r', function(d) {
return radius(d.values['attendance']);
})
.attr('fill', 'rgb(247, 148, 32)')
.attr('stroke', 'black')
.attr('stroke-width', 0.7)
.attr('opacity', 0.7);
//show only the countries that participated in the world cup of the year
var countries = filtered[0].values['teams'];
function update_countries(d) {
if (countries.indexOf(d.properties.name) !== -1){
//return "lightblue" ;
return "steelblue" ;
}else {
return "white";
}
};
svg.selectAll('path')
.transition()
.duration(500)
.style('fill',update_countries)
//.style('stroke',update_countries) ;
.style('stroke','black') ;
};
var year_idx = 0;
var year_interval = setInterval(function() {
update(years[year_idx]);
year_idx++;
if(year_idx >= years.length) {
clearInterval(year_interval);
var buttons = d3.select("body")
.append("div")
.attr("class", "years_buttons")
.selectAll("div")
.data(years)
.enter()
.append("div")
.text(function(d) {
return d;
});
buttons.on("click", function(d) {
/*
.transition()
.duration(500)
*/
d3.select(".years_buttons")
.selectAll("div")
.style("color", "black")
.style("background", "rgb(251, 201, 127)");
d3.select(this)
.transition()
.duration(100)
.style("background", "lightblue")
.style("color", "white");
update(d);
});
}
}, 1500);
};
var format = d3.time.format("%d-%m-%Y (%H:%M h)");
d3.tsv("world_cup_geo.tsv", function (d)
{
d['attendance'] = +d['attendance'];
d['date'] = format.parse(d['date']);
return d;
}, plot_points);
//end function draw
};
</script>
</head>
<body>
<script type="text/javascript">
/*
Use D3 to load the GeoJSON file
*/
d3.json("world_countries.json", draw);
</script>
<div id="chart"></div>
</body>
</html>