-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex_noscore.html
More file actions
578 lines (494 loc) · 24.2 KB
/
index_noscore.html
File metadata and controls
578 lines (494 loc) · 24.2 KB
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v5.min.js"></script>
<script src="https://momentjs.com/downloads/moment.js"></script>
<style>
@font-face {
font-family: 'ITC Avant Garde Gothic Std Book';
font-style: normal;
font-weight: normal;
src: local('ITC Avant Garde Gothic Std Book'), url('fonts/ITCAvantGardeStdBk.woff') format('woff');
}
@font-face {
font-family: 'ITC Avant Garde Gothic Std Medium';
font-style: normal;
font-weight: normal;
src: local('ITC Avant Garde Gothic Std Medium'), url('fonts/ITCAvantGardeStd-Md_MOD.woff') format('woff');
}
@font-face {
font-family: 'ITC Avant Garde Gothic Std Bold';
font-style: normal;
font-weight: normal;
src: local('ITC Avant Garde Gothic Std Bold'), url('fonts/ITCAvantGardeStdBold.woff') format('woff');
}
@font-face {
font-family: 'ITC Avant Garde Gothic Std Bold Condensed';
font-style: normal;
font-weight: normal;
src: local('ITC Avant Garde Gothic Std Bold Condensed'), url('fonts/ITCAvantGardeStdBoldCn.woff') format('woff');
}
@font-face {
font-family: 'ITC Avant Garde Gothic Std Medium Condensed';
font-style: normal;
font-weight: normal;
src: local('ITC Avant Garde Gothic Std Medium Condensed'), url('fonts/ITCAvantGardeStdMdCn.woff') format('woff');
}
text{
font-size: 16px;
font-family: 'ITC Avant Garde Gothic Std Book';
}
text.title{
font-size: 48px;
font-weight: 700;
font-family: "ITC Avant Garde Gothic Std Bold";
fill: #005243;
}
text.subtitle{
font-weight: 500;
font-size: 20px;
fill: #777777;
}
text.caption{
font-weight: 400;
font-size: 30px;
fill: #777777;
font-family: "ITC Avant Garde Gothic Std Medium";
}
text.label{
font-size: 40px;
font-family: "ITC Avant Garde Gothic Std Medium";
}
text.z_label {
font-size: 45px;
font-family: "ITC Avant Garde Gothic Std Medium";
}
text.cc_label {
font-size: 20px;
font-family: "ITC Avant Garde Gothic Std Medium Condensed";
font-weight: 600;
}
text.c_label {
font-size: 40px;
font-family: 'ITC Avant Garde Gothic Std Medium';
opacity: 0.45;
}
text.p_label {
font-size: 28px;
font-family: "ITC Avant Garde Gothic Std Bold Condensed";
font-weight: 500;
}
text.valueLabel{
font-size: 40px;
font-weight: 600;
fill: #000000;
}
text.dateText{
font-size: 64px;
font-weight: 700;
fill: #777777;
font-family: 'ITC Avant Garde Gothic Std Medium';
}
text.rankText {
font-size: 150px;
font-weight: 700;
opacity: 0.50;
}
text.sub_caption {
font-size: 24px;
font-weight: 900;
}
.tick text {
fill: #ffffff;
font-size: 20px;
}
.xAxis .tick:nth-child(2) text {
text-anchor: start;
}
.tick line {
shape-rendering: CrispEdges;
stroke: #ffffff;
}
.tick line.origin{
stroke: #ffffff;
}
path.domain{
display: none;
}
</style>
</head>
<body>
<script>
function calculate_age(curr, dob) {
if (dob == "..") {
return "??";
}
else {
var diff_ms = new Date(curr).getTime() - new Date(dob).getTime();
var age_dt = new Date(diff_ms);
return Math.abs(age_dt.getUTCFullYear() - 1970);
}
}
function calculate_m(curr, yl) {
if (yl == null) {
return 0;
}
else {
return new moment(yl).diff(new moment(curr), 'months');
}
}
var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
// top_n
const top_n = 10;
const f_size = 86;
const p_zone = 360
let r_player = null;
let r_date = null;
// dimensions
const height = 1080;
const width = 1920;
const speed = 1; // playback speed
// animation delay (ms)
const tickDuration = 200 / speed;
const delayDuration = 100 / speed;
// date start/end
const dateStart = "1981-01-01";
const dateEnd = "2001-01-31";
// title/subtitle
const title = `ITTF Senior Men\'s Singles Top 10 Ranking ` +
`(1928 to 2020)`;
const subtitle = "(ranking points)";
// make an svg object
const svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
// margin and padding
const margin = {
top: 80,
right: 60,
bottom: 10,
left: 60
};
const barPadding = (height - (margin.bottom + margin.top)) / (top_n * 5);
// add title, subtitle, caption to svg
svg.append("text")
.attr('class', 'title')
.attr('x', margin.left)
.attr('y', margin.top - 25)
.html(title);
svg.append("text")
.attr('class', 'caption')
.attr('x', width - margin.right - 120)
.attr('y', margin.top - 20)
.style('text-anchor', 'end')
.html('Source: ITTF');
// initiate date
let date = dateStart;
// read CSV and perform
d3.csv(`csv_${"en"}/part_${3}_3.csv`)
.then(function(data) {
console.log(data); // output to console all data collected
data.forEach(d => {
d.colour = d3.hsl(d["Hue"], 0.75, 0.75);
}); // assign HSL colour attribute to each entry
let lastValues = {}; // last values
function _normalizeData() {
const values = {}; // values
const ret = []; // ret ??
data.forEach(d => {
const name = d["Name"]; // grab name
const country = d["Country"];
const flag = d["Country Flag"];
let pic = `player/${d["Picture"]}`;
if (d["Picture"] == 0) pic = "player/UNKNOWN.jpg";
const dob = d["DOB"];
const id = d["ID"];
const country_n = d["Country Name"];
let val = parseFloat(d[date]); // grab value by (current) date
let lastValue = lastValues[id];
if (lastValue == null) lastValue = 0;
ret.push({
id : id,
name : name, // name attribute
dob : dob,
pic : pic,
country : country,
country_n : country_n,
flag : flag,
colour : d.colour, // colour attribute
value : val, // value attribute
lastValue : lastValue // last value attribute
}); // push attributes to ret ??
values[id] = val; // remember current value of player
}); // prepares data into values
lastValues = values; // overwrite ??
return ret.sort((a, b) => b.value - a.value).slice(0, top_n); // sort values/slice at top_n position
}
let dateSlice = _normalizeData(); // perform the above function and get current top_n
dateSlice.forEach((d, i) => d.rank = i); // assign rank number i to each entry of dateSlice
console.log('dateSlice: ', dateSlice); // output to console current slice
let x = d3.scaleLinear() // define x units (points), DOMAIN and RANGE
.domain([0, d3.max(dateSlice, d => d.value)]) // x-axis DOMAIN (zero to max)
.range([margin.left + 60, width - margin.right - 465]); // x-axis RANGE (according to margin)
let y = d3.scaleLinear() // define y units (ranking), DOMAIN and RANGE
.domain([top_n, 0]) // y-axis DOMAIN (from the defined top_n to zero) ??
.range([height - p_zone - margin.bottom, margin.top]); // y-axis RANGE (according to margin)
let xAxis = d3.axisTop() // define time x-axis
.scale(x) // scale is by x units
.ticks(width > 500 ? 5 : 2) // x units (points) major gridlines
.tickSize(-(height - margin.top - p_zone - margin.bottom)) // gridline size
.tickFormat(d => d3.format(',')(d)); // ??
svg.append('g') // draw the grid
.attr('class', 'axis xAxis')
.attr('transform', `translate(0, ${margin.top + p_zone})`)
.call(xAxis)
.selectAll('.tick line')
.classed('origin', d => d == 0);
svg.selectAll('text.r_label')
.data(dateSlice, d => d.id)
.enter()
.append('text')
.attr('class', 'z_label')
.attr('x', 95)
.attr('y', d => margin.top + 410 + d.rank * 63)
.style('text-anchor', 'end')
.html(d => `${d.rank + 1}`);
svg.selectAll('rect.bar') // bar
.data(dateSlice, d => d.id)
.enter()
.append('rect')
.attr('class', 'bar')
.attr('x', x(0) + 1)
.attr('width', d => x(d.lastValue) - x(0))
.attr('y', d => y(d.rank) + 5 + p_zone)
.attr('height', y(1) - y(0) - barPadding + 11)
.style('fill', d => d.colour);
let dateText = svg.append('text') // current date text
.attr('class', 'dateText')
.attr('x', width - margin.right - 80)
.attr('y', height - 50)
.style('text-anchor', 'end')
.html(date);
let ticker = d3.interval(e => {
dateSlice = _normalizeData(); // prep data ??
dateSlice.forEach((d,i) => d.rank = i); // rank each top_n ??
x.domain([0, d3.max(dateSlice, d => d.value)]); // set domain of points
svg.select('.xAxis')
.transition()
.duration(tickDuration)
.ease(d3.easeLinear)
.call(xAxis);
const bars = svg.selectAll('.bar').data(dateSlice, d => d.id);
bars // bar enter transition ??
.enter()
.append('rect')
.attr('class', d => `bar ${d.id}`)
.attr('x', x(0) + 1)
.attr('width', d => x(d.value) - x(0))
.attr('y', d => y(top_n + 1) + 5 + p_zone)
.attr('height', y(1) - y(0) - barPadding + 11)
.style('fill', d => d.colour)
.transition()
.duration(tickDuration)
.ease(d3.easeLinear)
.attr('y', d => y(d.rank) + 5 + p_zone)
bars
.transition()
.duration(tickDuration)
.ease(d3.easeLinear)
.attr('width', d => Math.max(0, x(d.value) - x(0)))
.attr('y', d => y(d.rank) + 5 + p_zone);
bars // bar exit transition ??
.exit()
.transition()
.duration(tickDuration)
.ease(d3.easeLinear)
.attr('width', d => Math.max(0, x(d.value) - x(0)))
.attr('y', d => y(top_n + 1) + 5 + p_zone)
.remove();
const labels = svg.selectAll('.label')
.data(dateSlice, d => d.id);
labels // name label enter transition ??
.enter()
.append('text')
.attr('class', 'label')
.attr('x', d => x(d.value) - 70 - f_size - 40)
.attr('y', d => y(top_n + 1) + ((y(1) - y(0)) / 2) + p_zone + 15)
.style('text-anchor', 'end')
.html(d => `${d.name} (${calculate_age(date, d.dob)})`)
.transition()
.duration(tickDuration)
.ease(d3.easeLinear)
.attr('y', d => y(d.rank) + ((y(1) - y(0)) / 2) + p_zone + 15);
labels
.transition()
.duration(tickDuration)
.ease(d3.easeLinear)
.attr('x', d => x(d.value) - 70 - f_size - 40)
.attr('y', d => y(d.rank) + ((y(1) - y(0)) / 2) + p_zone + 15);
labels // name label exit transition ??
.exit()
.transition()
.duration(tickDuration)
.ease(d3.easeLinear)
.attr('x', d => x(d.value) - 70 - f_size - 40)
.attr('y', d => y(top_n + 1) + 5 + p_zone)
.remove();
const p_labels = svg.selectAll('.p_label')
.data(dateSlice.slice(0, 5), d => d.id)
p_labels
.enter()
.append('text')
.attr('class', 'p_label')
.attr('x', d => (5 + 3) * 280 + margin.left)
.attr('y', margin.top + 307)
.html(d => `${d.rank + 1}. ${d.name}`)
.style('text-anchor', 'middle')
.transition()
.duration(tickDuration)
.ease(d3.easeLinear)
.attr('x', d => (d.rank + (d.rank != 0 ? 1 : 0)) * 280 + margin.left + 140);
p_labels
.transition()
.duration(tickDuration)
.ease(d3.easeLinear)
.attr('x', d => (d.rank + (d.rank != 0 ? 1 : 0)) * 280 + margin.left + 140);
p_labels
.exit()
.transition()
.duration(tickDuration)
.ease(d3.easeLinear)
.attr('x', (5 + 3) * 280 + margin.left)
.remove();
const cc_labels = svg.selectAll('.cc_label')
.data(dateSlice.slice(0, 5), d => d.id)
cc_labels
.enter()
.append('text')
.attr('class', 'cc_label')
.attr('x', d => (5 + 3) * 280 + margin.left)
.attr('y', margin.top + 330)
.html(d => `${d.country_n}`)
.style('text-anchor', 'middle')
.transition()
.duration(tickDuration)
.ease(d3.easeLinear)
.attr('x', d => (d.rank + (d.rank != 0 ? 1 : 0)) * 280 + margin.left + 140);
cc_labels
.transition()
.duration(tickDuration)
.ease(d3.easeLinear)
.attr('x', d => (d.rank + (d.rank != 0 ? 1 : 0)) * 280 + margin.left + 140);
cc_labels
.exit()
.transition()
.duration(tickDuration)
.ease(d3.easeLinear)
.attr('x', (5 + 3) * 280 + margin.left)
.remove();
const c_labels = svg.selectAll('.c_label')
.data(dateSlice, d => d.id);
c_labels // country label enter transition ??
.enter()
.append('text')
.attr('class', 'c_label')
.attr('x', d => x(d.value) - 4)
.attr('y', d => y(top_n + 1) + 5 + ((y(1) - y(0)) / 2) + 10 + p_zone)
.style('text-anchor', 'end')
.html(d => d.country)
.transition()
.duration(tickDuration)
.ease(d3.easeLinear)
.attr('y', d => y(d.rank) + 5 + ((y(1) - y(0)) / 2) + 10 + p_zone);
c_labels
.transition()
.duration(tickDuration)
.ease(d3.easeLinear)
.attr('x', d => x(d.value) - f_size - 4)
.attr('y', d => y(d.rank) + 5 + ((y(1) - y(0)) / 2) + 10 + p_zone);
c_labels // country label exit transition ??
.exit()
.transition()
.duration(tickDuration)
.ease(d3.easeLinear)
.attr('x', d => x(d.value) - f_size - 4)
.attr('y', d => y(top_n + 1) + 5 + p_zone)
.remove();
const flagImages = svg.selectAll('.f_image').data(dateSlice, d => d.id);
flagImages // enter
.enter()
.append('image')
.attr('class', 'f_image')
.attr('x', d => x(d.value) - f_size + 4)
.attr('y', d => y(top_n + 1) + 0 + ((y(1) - y(0)) / 2) - 11 + p_zone)
.attr('xlink:href', d => d.flag)
.attr('width', f_size)
.attr('height', f_size)
.transition()
.duration(tickDuration)
.ease(d3.easeLinear)
.attr('y', d => y(d.rank) + 0 - 11+ p_zone)
flagImages
.transition()
.duration(tickDuration)
.ease(d3.easeLinear)
.attr('x', d => x(d.value) - f_size + 4)
.attr('y', d => y(d.rank) + 0 -11 + p_zone)
flagImages // exit
.exit()
.transition()
.duration(tickDuration)
.ease(d3.easeLinear)
.attr('x', d => x(d.value) - f_size + 4)
.attr('y', d => y(top_n + 1) + 0 -11+ p_zone)
.remove();
const playerImages = svg.selectAll('.p_image').data(dateSlice.slice(0, 5), d => d.id);
playerImages
.enter()
.append('image')
.attr('class', 'p_image')
.attr('x', d => (5 + 2) * 280 + margin.left)
.attr('y', margin.top)
.attr('xlink:href', d => d.pic)
.attr('width', 280)
.attr('height', 280)
.transition()
.duration(tickDuration)
.ease(d3.easeLinear)
.attr('x', d => (d.rank + (d.rank != 0 ? 1 : 0)) * 280 + margin.left);
playerImages
.transition()
.duration(tickDuration)
.ease(d3.easeLinear)
.attr('x', d => (d.rank + (d.rank != 0 ? 1 : 0)) * 280 + margin.left);
playerImages
.exit()
.transition()
.duration(tickDuration)
.ease(d3.easeLinear)
.attr('x', (5 + 2) * 280 + margin.left)
.remove();
if (new moment(date) == new moment(dateEnd)) {
ticker.stop();
}
let d_conv = moment(date);
s_date = date.split('-');
if (dateSlice[0].name != r_player) {
r_date = `${new moment(date).add(1, "months").format("YYYY-MM-")}01`;
r_player = dateSlice[0].name;
}
p_labels.html(d => `${d.rank + 1}. ${d.name}`)
dateText.html(`${months[d_conv.format('M') - 1]} ${d_conv.format('YYYY')}`);
labels.html(d => `${d.name} (${calculate_age(new Date(date), d.dob)})`) // age bracket
date = new moment(d_conv.add(2, 'days')).format()
.split("T")[0];
if (new moment(date) > new moment(dateEnd)) {
date = dateEnd;
}
}, delayDuration);
});
</script>
</body>
</html>