-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
425 lines (370 loc) · 10.4 KB
/
app.js
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
// set the dimensions and margins of the graph
const margin = {
top: 60,
right: 120,
bottom: 160,
left: 80
}
let width = 900 - margin.left - margin.right;
let height = 500 - margin.top - margin.bottom;
//will hold all the data from the .tsv file
let teamsArr = [];
let teamsObj = {};
let playerArr = [];
let optionHtml = '';
let optionArr = [];
let chartId;
let teamName = '';
let area = '';
let season = '';
let totalTeamWins;
let totalTeamLoss;
let totalTeamPlays;
let teamWinPercent;
let teamLossPercent;
let tooltip;
let yMax;
let xScale;
let yScale;
let zScale;
let keys;
let g;
/******************FUNCTIONS*********************/
const newTeamId = chartId => {
playerArr = teamsObj[chartId];
teamName = playerArr[0]['Team Name'];
area = playerArr[0]['Area'];
season = playerArr[0]['Season'];
totalTeamWins = playerArr.reduce((acc, el) => acc + Number(el.Won), 0);
totalTeamLoss = playerArr.reduce((acc, el) => acc + Number(el.Lost), 0);
totalTeamPlays = totalTeamWins + totalTeamLoss;
teamWinPercent = Math.round(totalTeamWins / totalTeamPlays * 100);
teamLossPercent = Math.round(totalTeamLoss / totalTeamPlays * 100);
//this converts some of the '-' fields to '0'
playerArr.map(el => {
if (el.Defaults === '-') {
el.Defaults = '0';
}
if (el['Win %'] !== '-') {
el['Win %'] = parseInt(el['Win %']);
}
if (el.Doubles === '-') {
el.Doubles = '0';
}
if (el.Singles === '-') {
el.Singles = '0';
}
return el;
});
tooltip = d3.select('body')
.append('div')
.classed('tooltip', true)
.style('opacity',0);
//array of all the keys in the player array (e.g., Player, City, etc)
keys = teamsArr.columns.slice(1);
d3.select('.svgChart')
.attr('width', width + margin.left + margin.right)
.attr('height', height + margin.top + margin.bottom)
.classed('svgClass', true);
g = d3.select('.svgChart')
.append('g')
.attr('transform', `translate(${margin.left}, ${margin.top})`);
//ON FIRST LOAD CALL REMOVE CHART
//TO first remove any previous chart & draw new one
removeChart('Matches');
};
const drawChart = type => {
let colorArr = [];
let keysArr = [];
let chartTitle = '# ';
//type is based on the select values (event listener)
if (type === 'Matches') {
colorArr = ['#A8927B', '#564036', '#EF7D5A'];
//Default, Loss, Win
keysArr = [keys[8], keys[17], keys[16]];
chartTitle += 'Matches Won / Lost (by Player)';
} else if (type === 'Won') {
colorArr = ['#EF7D5A'];
keysArr = [keys[16]];
chartTitle += 'Matches Won (by Player)';
} else if (type === 'Lost') {
colorArr = ['#564036'];
keysArr = [keys[17]];
chartTitle += 'Matches Lost (by Player)';
} else if (type === 'Singles') {
colorArr = ['#516EBA'];
keysArr = [keys[10]];
chartTitle += 'Singles Matches Played (by Player)';
} else if (type === 'Doubles') {
colorArr = ['#E3C247'];
keysArr = [keys[11]];
chartTitle += 'Doubles Matches Played (by Player)';
} else if (type === 'SinglesDoubles') {
colorArr = ['#516EBA', '#E3C247'];
keysArr = [keys[10], keys[11]];
chartTitle += 'Singles / Doubles Matches Played (by Player)';
} else if (type === 'WinPercentage') {
colorArr = ['#EF7D5A'];
keysArr = [keys[9]];
chartTitle = 'Match Win Percentage (by Player)';
}
chartTitle += '*';
drawTitle(chartTitle);
//DRAW TEAM NAME
drawTeamName();
drawChartFooter();
//sort by y-axis value (and if same values, then sort by last name alphabetically)
if (type === 'SinglesDoubles') {
playerArr.sort(function(a,b) {
return Number(a.Matches) - Number(b.Matches) ||
a.Player.localeCompare(b.Player);
});
yMax = d3.max(playerArr.map(d => {
str_data = d['Matches'];
num_data = Number(d['Matches']);
return num_data;
}));
} else if (type === 'WinPercentage') {
playerArr.sort(function(a,b) {
return a['Win %'] - b['Win %'] ||
a.Player.localeCompare(b.Player);
});
yMax = 100;
} else {
playerArr.sort(function(a,b) {
return Number(a[type]) - Number(b[type]) ||
a.Player.localeCompare(b.Player);
});
yMax = d3.max(playerArr.map(d => {
str_data = d[type];
num_data = Number(d[type]);
return num_data;
}));
}
xScale = d3.scaleBand()
.rangeRound([0, width])
.paddingInner(0.2)
.align(0.1);
xScale.domain(playerArr.map(d => d.Player));
yScale = d3.scaleLinear()
.rangeRound([height, 0])
.domain([0, yMax]);
zScale = d3.scaleOrdinal()
.range(colorArr);
g.append('g')
.attr('class', 'chart')
.selectAll('g')
.data(d3.stack().keys(keysArr)(playerArr))
.enter()
.append('g')
.attr('fill', d => zScale(d.key))
.selectAll('rect')
.data(d => d)
.enter()
.append('rect')
.style('opacity', 0)
.attr('class', 'bar')
.attr('x', d => xScale(d.data.Player))
.attr('y', d => yScale(d[1]))
.attr('height', d => yScale(d[0]) - yScale(d[1]))
.attr('width', xScale.bandwidth())
.on('mouseenter', d => drawTooltipText(d))
.on('mouseout', () => tooltip.style('opacity', 0));
/*add transition effects to fade in*/
g.selectAll('rect')
.transition()
.duration(800)
.ease(d3.easeLinear)
.style('opacity', 1);
//call to functions
drawLegend(keysArr);
drawXAxis();
drawYAxis();
};
const drawLegend = keys => {
let legend = g
.append('g')
.attr('class', 'legend')
.attr('font-family', 'sans-serif')
.attr('font-size', 10)
.attr('text-anchor', 'end')
.selectAll('g')
.data(keys.slice().reverse())
.enter()
.append('g')
.attr('transform', (d, i) => `translate(0, ${i * 20})`);
legend.append('rect')
.attr('x', width + 85)
.attr('width', 19)
.attr('height', 19)
.attr('fill', zScale);
legend.append('text')
.attr('x', width + 80)
.attr('y', 9.5)
.attr('dy', '0.32em')
.text(d => d);
};
const drawTooltipText = d => {
tooltip.html(`
${d.data.Player}<br>
${d.data['Win %']}% Win<br>
Won: ${d.data.Won}; Lost: ${d.data.Lost}<br>
${d.data.Singles} Singles Played<br>
${d.data.Doubles} Doubles Played<br>
City: ${d.data.City}<br>
Rating: ${d.data.Rating}`
)
.style('opacity', 1)
.style('left', d3.event.pageX + 'px')
.style('top', d3.event.pageY + 'px');
};
const drawTeamName = () => {
//first remove old team name
d3.select('.teamName')
.remove();
//then draw the new team name
d3.select('.svgChart')
.append('text')
.attr('class', 'teamName')
.attr('x', (width + margin.left + margin.right) / 2)
.attr('y', margin.top / 2)
.attr('text-anchor', 'middle')
.text(teamName);
};
const drawChartFooter = () => {
//first remove old footer
d3.select('.chartFooter')
.remove();
//then draw the new one
d3.select('.svgChart')
.append('text')
.attr('class', 'chartFooter')
.attr('x', margin.left + 10)
.attr('y', height + margin.top + margin.bottom - 5)
.attr('text-anchor', 'middle')
.text('*Data from USTA NorCal');
};
const drawTitle = title => {
//first remove old title
d3.select('.chartTitle')
.remove();
//then draw the new chart title
d3.select('.svgChart')
.append('text')
.attr('class', 'chartTitle')
.attr('x', (width + margin.left + margin.right) / 2)
.attr('y', margin.top - 5)
.attr('text-anchor', 'middle')
.text(title);
}
const drawXAxis = () => {
let xAxis = d3.axisBottom(xScale);
//add the x Axis
d3.select('.svgChart')
.append('g')
.attr('class', 'xAxis')
.attr('transform', `translate(${margin.left},`
+ `${height + margin.top})`)
.call(xAxis)
.selectAll('text')
//ensures end of label is attached to the axis tick
.style('text-anchor', 'end')
.attr('dx', '-.8em')
.attr('dy', '-.15em')
.attr('transform', 'rotate(-50)');
};
const drawYAxis = () => {
let numTicks = yMax;
if (yMax === 100) {
numTicks = 10;
}
let yAxis = d3.axisRight(yScale)
.ticks(numTicks);
//add the y Axis
d3.select('.svgChart')
.append('g')
.attr('class', 'yAxis')
.attr('transform', `translate(${width + margin.left}`
+ `,${margin.top})`) //shifts axis
.call(yAxis);
};
const removeChart = newVal => {
let svg = d3.select('.svgChart');
svg.select('.chart')
.style('opacity', 1)
/*add transition effects to fade out*/
.transition()
.duration(300)
.ease(d3.easeLinear)
.style('opacity', 0)
.remove();
setTimeout(() => {
removeAxes();
removeLegend();
drawChart(newVal)
}, 300);
};
const removeAxes = () => {
d3.select('.svgChart')
.select('.xAxis')
.remove();
d3.select('.svgChart')
.select('.yAxis')
.remove();
};
const removeLegend = () => {
d3.select('.svgChart')
.select('.legend')
.remove();
};
/******************D3*********************/
d3.tsv('./player_data.tsv', file => {
teamsArr = file;
teamsObj = teamsArr.reduce((acc, el) => {
let currTeamId = el['Team ID'];
if (acc[currTeamId] === undefined) {
acc[currTeamId] = [el];
} else {
acc[currTeamId].push(el);
}
return acc;
}, {});
for (let prop in teamsObj) {
let currTeamName = teamsObj[prop][0]['Team Name'];
optionArr.push([currTeamName, prop]);
}
//sort teams alphabetically
optionArr.sort((a,b) => (a[0] < b[0]) ? -1 : 1);
for (let i = 0; i < optionArr.length; i++) {
//for each unique team, add id & team name
//into option tags for select
optionHtml += `
<option value='${optionArr[i][1]}'>
${optionArr[i][0]}</option>
`;
}
d3.select('#selectTeams')
.html(optionHtml);
d3.select('#selectTeams')
.classed('hidden', false);
d3.select('#selectTeamsLabel')
.classed('hidden', false);
newTeamId(optionArr[0][1]);
/******************EVENT LISTENERS*********************/
//when change select dropdown option
//initiate a new team to draw the chart
d3.select('#selectTeams').on('change', () => {
let optionVal = d3.select('#selectTeams').property('value');
newTeamId(optionVal);
});
//draw different charts based on which button is clicked
d3.selectAll('.btnCustom').on('click', () => {
d3.selectAll('.btnCustom')
.classed('active', false);
d3.event.preventDefault();
d3.select(event.currentTarget)
.classed('active', true);
let newVal = d3.select(event.currentTarget).attr('data-val');
removeChart(newVal);
});
});