-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
302 lines (275 loc) · 10.5 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<!DOCTYPE html>
<html>
<head>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-108469136-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag('js', new Date());
gtag('config', 'UA-108469136-1');
</script>
<meta charset="utf-8">
<title>Pay to Play</title>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="javascripts/athletes.js" charset="utf-8"></script>
<link href="https://fonts.googleapis.com/css?family=Titillium+Web|Bungee+Inline|Cantarell:400i" rel="stylesheet">
<link rel="stylesheet" href="stylesheets/gridlines.css">
<link rel="stylesheet" href="stylesheets/circles.css">
</head>
<body>
<div class="title-div">
<h2 id="title">Pay to Play</h2>
<h2 id="subtitle">Visualizing the 51 Most Expensive Athletes In the Game</h2>
</div>
<div class="filter-div">
<ul class="filter-comp-list">
<li class="view-by" id="view-by-pay">View By Total Compensation:</li>
<li class="filter filter-comp all selected-comp" id="all">All</li>
<li class="filter filter-comp" id="below-30m">Below 30M</li>
<li class="filter filter-comp" id="30m-40m">30M - 40M</li>
<li class="filter filter-comp" id="40m-50m">40M - 50M</li>
<li class="filter filter-comp" id="50m-60m">50M - 60M</li>
<li class="filter filter-comp" id="above-60m">Above 60M</li>
</ul>
<ul class="filter-sport-list">
<li class="view-by" id="view-by-sport">View By Sport:</li>
<li class="filter filter-sport all selected-sport" id="all">All</li>
<li class="filter filter-sport soccer" id="soccer">Soccer</li>
<li class="filter filter-sport basketball" id="basketball">Basketball</li>
<li class="filter filter-sport tennis" id="tennis">Tennis</li>
<li class="filter filter-sport football" id="football">Football</li>
<li class="filter filter-sport golf" id="golf">Golf</li>
<li class="filter filter-sport racing" id="racing">Auto Racing</li>
<li class="filter filter-sport track" id="track">Track</li>
<li class="filter filter-sport mma" id="mma">Mixed Martial Arts</li>
<li class="filter filter-sport baseball" id="mma">Baseball</li>
<li class="filter filter-sport boxing" id="boxing">Boxing</li>
</ul>
</div>
<div class="example-div">
</div>
<script type="text/javascript">
var margin = {top: 10, right: 20, bottom: 75, left: 80},
width = 900 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var xScale = d3.scaleLinear().domain([0, 26]).range([0, width]);
var yScale = d3.scaleLinear().domain([0, 24]).range([height, 0]);
var valueline = d3.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.close); })
var svg = d3.select("body").append("svg")
.attr('class', 'grid')
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform",
"translate(" + margin.left + "," + margin.top + ")");
// x-gridlines
svg.append("g")
.attr("class", "gridlines")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(xScale)
.tickSize(-height)
.tickFormat("")
)
// x-axis
var xAxis = svg.append('g')
.attr('class', 'axis')
.attr('transform', 'translate(0,' + height + ')')
.call(d3.axisBottom(xScale)
.tickSize(25)
.tickPadding(10)
)
// x-axis label
svg.append('text')
.attr('class', 'axis-label')
.attr('x', width / 2)
.attr('y', height + margin.bottom - 5)
.style('text-anchor', 'middle')
.text('Years Competing Professionally')
// y gridlines
svg.append('g')
.attr('class', 'gridlines')
.call(d3.axisLeft(yScale)
.tickSize(-width)
.tickFormat("")
)
// y-axis
svg.append('g')
.attr('class', 'axis')
.call(d3.axisLeft(yScale)
.tickSize(25)
.tickPadding(10)
)
// y-axis label
svg.append("text")
.attr('class', 'axis-label')
.attr("transform", "rotate(-90)")
.attr("y", 0 - margin.left + 15)
.attr("x", 0 - (height / 2))
.style("text-anchor", "middle")
.text("Number of Major Championships Won");
// getting the data from athletes.js
var athletes = svg.append('g')
.attr('class', 'athletes')
// tooltips
var tooltipDiv = d3.select('.example-div')
.append('div')
.attr('class', 'tooltip')
.style('opacity', 0)
athletes.selectAll('.circle')
.data(athletesJson.athletes)
.enter()
.append('circle')
.attr('class', function(d) {
switch(d.Sport) {
case "Soccer":
return "circle soccer shown-comp shown-sport";
case "Basketball":
return "circle basketball shown-comp shown-sport";
case "Tennis":
return "circle tennis shown-comp shown-sport";
case "Football":
return "circle football shown-comp shown-sport";
case "Golf":
return "circle golf shown-comp shown-sport";
case "Auto Racing":
return "circle racing shown-comp shown-sport";
case "Track":
return "circle track shown-comp shown-sport";
case "Mixed Martial Arts":
return "circle mma shown-comp shown-sport";
case "Baseball":
return "circle baseball shown-comp shown-sport";
case "Boxing":
return "circle boxing shown-comp shown-sport";
}
})
.attr('r', function(d) {
return d.Pay / 3000000 * 1.2
})
.attr('transform', function(d) {
var xPos = d.Years_Competing_Professionally * width / 26;
var yPos = d.Championships * height / 24
return 'translate(' + xPos + ',' + (height - yPos) + ')'
})
.on('mousemove', function(d) {
let xTooltip = d.Years_Competing_Professionally * width / 26;
let yTooltip = height - (d.Championships * height / 24);
let r = this.r.baseVal.value;
d3.select(this)
.classed('active', true);
d3.select(this)
.attr('r', function(d) {
const prevRadius = d.Pay / 3000000 * 1.2;
return prevRadius * 1.1
})
tooltipDiv
.style('opacity', 0.8)
.style('z-index', 1000)
.style('left', d3.event.pageX + 0.5*r + 'px')
.style('top', d3.event.pageY - 3*r + 'px')
.html(
d.Name + '<br/>' +
'Major Championships Won: ' + d.Championships + '<br/>' +
'# of Pro Seasons: ' + d.Years_Competing_Professionally + '<br/>' +
'Total Compensation: ' + d.Pay + '<br/>' +
'Salary: ' + d.Salary + '<br/>' +
'Endorsements: ' + d.Endorsements
)
})
.on('mouseout', function(d) {
d3.select(this)
.classed('active', false);
d3.select(this)
.attr('r', function(d) {
return d.Pay / 3000000 * 1.2;
})
tooltipDiv
.style('opacity', 0)
})
let compSelectedCircles = (minComp, maxComp) => {
let compCircles = d3.selectAll('circle').filter(function(d, i) {
if (minComp && maxComp) {
return d.Pay > minComp && d.Pay <= maxComp;
} else if (minComp) {
return d.Pay > minComp;
} else if (maxComp) {
return d.Pay <= maxComp;
}
})
return compCircles;
}
const compFilterList = document.getElementsByClassName('filter-comp')
const compFilterArray = Array.from(compFilterList);
compFilterArray.forEach(function(filterEl) {
let filteredComp = filterEl.innerHTML;
filterEl.addEventListener('click', function() {
let all = d3.selectAll('circle');
d3.selectAll('.filter-comp').classed('selected-comp', false);
let selected;
switch(filteredComp) {
case 'All':
this.className += ' selected-comp';
break;
case 'Below 30M':
this.className += ' selected-comp';
selected = compSelectedCircles(null, 30000000);
break;
case '30M - 40M':
this.className += ' selected-comp';
selected = compSelectedCircles(30000000, 40000000);
break;
case '40M - 50M':
this.className += ' selected-comp';
selected = compSelectedCircles(40000000, 50000000);
break;
case '50M - 60M':
this.className += ' selected-comp';
selected = compSelectedCircles(50000000, 60000000);
break;
case 'Above 60M':
this.className += ' selected-comp';
selected = compSelectedCircles(60000000, null);
break;
}
if (selected) {
all.classed('shown-comp', false);
selected.classed('shown-comp', true);
} else {
all.classed('shown-comp', true);
}
})
})
const sportFilterList = document.getElementsByClassName('filter-sport');
const sportFilterArray = Array.from(sportFilterList);
sportFilterArray.forEach(function(filterEl) {
let filteredSport = filterEl.innerHTML;
filterEl.addEventListener('click', function() {
let all = d3.selectAll('circle');
d3.selectAll('.filter-sport').classed('selected-sport', false);
if (filteredSport === 'All') {
this.className += ' selected-sport';
all.classed('shown-sport', true)
} else {
this.className += ' selected-sport';
let selected = d3.selectAll('circle').filter(function(d, i) {
return d.Sport === filteredSport;
})
all.classed('shown-sport', false);
selected.classed('shown-sport', true);
}
})
})
</script>
</body>
<div class="credits">
<p>Source: Forbes</p>
<p>Number of Championships a & Years Competing gathered from:</p>
<p>Wikipedia, transfermarkt.com, landofbasketball.com, boxrec.com</p>
<p>Approximated equivalent championships across sports</p>
</div>
</html>