-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
282 lines (236 loc) · 8.83 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
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<!-- Import d3.js -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.8/d3.min.js" type="text/JavaScript"></script>
<style>
circle {
fill: green;
stroke: black;
stroke-width: 0.7;
opacity: 0.7;
}
circle:hover {
fill: yellow;
stroke: black;
stroke-width: 0.7;
opacity: 0.7;
}
h2 {
text-align: center;
}
div.years_buttons {
text-align: center;
background-color: black;
}
#legend{
background-color: #000000;
}
</style>
<script type="text/javascript">
// Import world from json and trigger dataViz function
d3.json("countries.json", dataViz);
function dataViz(geo_data) {
//Assigning height and width
var width = 1400;
var height = 600;
// Format for date time
var format = d3.timeParse("%d-%m-%Y (%H:%M h)");
//Import data from wc_geo.tsv
d3.tsv("wc_geo.tsv", function(d) {
// Taking out value of attendance and formating date and calling function plotter
d['attendance'] = +d['attendance'];
d['date'] = format(d['date']);
return d;
}, plotter);
// appendinng map svg with height and width
var svg = d3.select("body")
.append("svg")
.attr("width", width)
.attr("height", height)
.append('g')
.attr('class', 'map');
// appending h2
d3.select("body")
.append("h2")
.text("Fifa World Cup");
// year arry to store years between 1930 and 2015 excluding 1942 and 1946
// 1942 and 1946 does not have any data in tsv file
var years = [];
for (var i = 1930; i < 2015; i += 4) {
if(i !== 1942 && i !== 1946) {
years.push(i);
}
}
// Scaling map size
var projection = d3.geoMercator()
.scale(130)
.translate([width / 2, height / 1.5]);
var path = d3.geoPath()
.projection(projection);
// Plotting the map with colors, path and features
var map = svg.selectAll('path')
.data(geo_data.features)
.enter()
.append('path')
.attr('d', path)
.style('fill', '#000000')
.style('stroke', 'white')
.style('stroke-width', 1);
// Plotter function to plot data in map
function plotter(data) {
function year(data) {
// console.log(data);
//Calculating total attendence
var total = d3.sum(data, function(d) {
return d['attendance'];
});
// console.log(total);
//Projecting countries coordinates
var coords = data.map(function(d) {
return projection([+d.long, d.lat]);
});
// console.log(coords);
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();
//Looping through tsv files team1 and team2 and adding them to teams
data.forEach(function(d) {
teams.add(d['team1']);
teams.add(d['team2']);
});
return {
'attendance': total,
'x': center_x,
'y': center_y,
'teams': teams.values()
};
};
//draw circles logic
var nested = d3.nest()
.key(function(d) {
return d['date'].getUTCFullYear();
})
.rollup(year)
.entries(data);
// finding out max attendence from nesed object
var attendance_max = d3.max(nested, function(d) {
return d.value['attendance'];
});
// Circle radius based on attendence max
var radius = d3.scaleSqrt()
.domain([0, attendance_max])
.range([0,15]);
function key_func(d) {
return d['key']
}
// Sorting, Setting circle radius based on attendence
svg.append('g')
.selectAll('circle')
.data(nested.sort(function(a, b) {
return b.value['attendance'] - a.value['attendance'];
}), key_func)
.enter()
.append('circle')
.attr('cx', function(d) { return d.value['x']; })
.attr('cy', function(d) { return d.value['y']; })
.attr('r', function(d) {
return radius(d.value['attendance']); }
)
// Funnction to update h2 with year on click
function update(year) {
console.log(year);
var filtered = nested.filter(function(d) {
return new Date(d['key']).getUTCFullYear() == year;
});
d3.select("h2")
.text(year + " - Fifa World Cup ");
var circles = svg.selectAll('circle')
.data(filtered, key_func);
// Remove circle on exit
circles.exit().remove();
// Creating circle on click and radius based on attendance
circles.enter()
.append('circle')
.style("opacity", 0)
.transition()
.duration(666)
.ease(d3.easeLinear)
.style("opacity", 1)
.attr('cx', function(d) { return d.value['x']; })
.attr('cy', function(d) { return d.value['y']; })
.attr('r', function(d) { return radius(d.value['attendance'])})
var countries = filtered[0].value['teams'];
// Resetting colors to black and red
function update_countries(d) {
if(countries.indexOf(d.properties.name) !== -1) {
return "red";
} else {
return "black";
}
}
svg.selectAll('path')
.style('fill', update_countries)
.style('stroke', "white");
};
var year_idx = 0;
// Funciton to add yars and buttons with 10ms interval
var year_interval = setInterval(function() {
year_idx++;
if(year_idx >= years.length) {
clearInterval(year_interval)
// Appending button with year
var buttons = d3.select("body")
.append("div")
.attr("class", "years_buttons")
.selectAll("div")
.data(years)
.enter()
.append("button")
.text(function(d) {
return d;
});
// Button on click button style change
buttons.on("click", function(d) {
d3.select(".years_buttons")
.selectAll("button")
.transition()
.duration(500)
.style("color", "white")
.style("background", "black");
d3.select(this)
.transition()
.duration(500)
.style("background", "red")
.style("color", "white")
update(d);
})
}
}, 10);
};
};
</script>
</head>
<body>
<div>
<svg id="legend" height=95 width=1400></svg>
</div>
<script>
// select the svg area for legend
var svg = d3.select("#legend")
// Handmade legend
svg.append("text").attr("x",100).attr("y",35).text("Legend: ").style("text-decoration", "underline").style('fill', 'white')
svg.append("circle").attr("cx",200).attr("cy",30).attr("r", 6).style("fill", "green")
svg.append("circle").attr("cx",200).attr("cy",50).attr("r", 6).style("fill", "red")
svg.append("circle").attr("cx",200).attr("cy",70).attr("r", 6).style("fill", "black").style('stroke', 'white')
svg.append("text").attr("x", 220).attr("y", 35).text("Places with most attendence, circles radius is based on mazimum attendence number").style("font-size", "15px").attr("alignment-baseline","middle").style('fill', 'white')
svg.append("text").attr("x", 220).attr("y", 55).text("Countries that participited in world cup").style("font-size", "15px").attr("alignment-baseline","middle").style('fill', 'white')
svg.append("text").attr("x", 220).attr("y", 75).text("Countries that did not participited in world cup").style("font-size", "15px").attr("alignment-baseline","middle").style('fill', 'white')
</script>
</body>
</html>