-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
496 lines (440 loc) · 20.1 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Score Progression</title>
<style>
:root {
--highlighted-color: #2661ff;
}
html, body {
margin: 20px;
padding: 0;
overflow: hidden;
font-family: sans-serif;
}
svg {
height: 100vh;
width: 100vw;
user-select: none;
}
h1 {
margin: 0;
}
table#teams {
background-color: #fff9c0;
padding: 4px;
}
table#teams td {
width: 48px;
height: 48px;
cursor: pointer;
}
table#teams td.special {
width: 46px;
height: 46px;
border: 1px solid #bbbbbb;
border-radius: 6px;
font-size: 12px;
text-align: center;
font-weight: bold;
background-color: lightgray;
}
.small {
font: 2px sans-serif;
}
.score-divisor {
fill: black;
font-size: 1.5pt;
}
rect.final-score {
stroke: var(--highlighted-color);
stroke-width: 0.2pt;
}
span.final-score {
color: var(--highlighted-color);
font-weight: bold;
}
#tooltip {
position: absolute;
background-color: white;
border: 1px solid gray;
border-radius: 4px;
padding: 10px;
}
.hidden {
display: none;
}
</style>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-101273659-1', 'auto');
ga('send', 'pageview');
</script>
</head>
<body>
<h1>World Cup 2018 Score Progression Analysis</h1>
<p>
An analysis of how matches progress, goal by goal.<br>
Scores go from red (more frequent) to yellow (less frequent).<br>
Final scores are highlighted with a <span class="final-score">border</span>.</p>
<!--<table id="teams" class="hidden">-->
<!--<tr>-->
<!--<td class="special">All</td>-->
<!--<td><img src="assets/argentina.png" alt="Argentina"></td>-->
<!--<td><img src="assets/australia.png" alt="Australia"></td>-->
<!--<td><img src="assets/belgium.png" alt="Belgium"></td>-->
<!--<td><img src="assets/brazil.png" alt="Brazil"></td>-->
<!--<td><img src="assets/colombia.png" alt="Colombia"></td>-->
<!--<td><img src="assets/costa-rica.png" alt="Costa Rica"></td>-->
<!--<td><img src="assets/croatia.png" alt="Croatia"></td>-->
<!--<td><img src="assets/denmark.png" alt="Denmark"></td>-->
<!--<td><img src="assets/egypt.png" alt="Egypt"></td>-->
<!--<td><img src="assets/england.png" alt="England"></td>-->
<!--<td><img src="assets/france.png" alt="France"></td>-->
<!--<td><img src="assets/germany.png" alt="Germany"></td>-->
<!--<td><img src="assets/iceland.png" alt="Iceland"></td>-->
<!--<td><img src="assets/iran.png" alt="Iran"></td>-->
<!--<td><img src="assets/japan.png" alt="Japan"></td>-->
<!--<td><img src="assets/mexico.png" alt="Mexico"></td>-->
<!--</tr><tr>-->
<!--<td class="special">None</td>-->
<!--<td><img src="assets/morocco.png" alt="Morocco"></td>-->
<!--<td><img src="assets/nigeria.png" alt="Nigeria"></td>-->
<!--<td><img src="assets/panama.png" alt="Panama"></td>-->
<!--<td><img src="assets/peru.png" alt="Peru"></td>-->
<!--<td><img src="assets/poland.png" alt="Poland"></td>-->
<!--<td><img src="assets/portugal.png" alt="Portugal"></td>-->
<!--<td><img src="assets/russia.png" alt="Russia"></td>-->
<!--<td><img src="assets/saudi-arabia.png" alt="Saudi Arabia"></td>-->
<!--<td><img src="assets/senegal.png" alt="Senegal"></td>-->
<!--<td><img src="assets/serbia.png" alt="Serbia"></td>-->
<!--<td><img src="assets/south-korea.png" alt="South Korea"></td>-->
<!--<td><img src="assets/spain.png" alt="Spain"></td>-->
<!--<td><img src="assets/sweden.png" alt="Sweden"></td>-->
<!--<td><img src="assets/switzerland.png" alt="Switzerland"></td>-->
<!--<td><img src="assets/tunisia.png" alt="Tunisia"></td>-->
<!--<td><img src="assets/uruguay.png" alt="Uruguay"></td>-->
<!--</tr>-->
<!--</table>-->
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
<defs>
<!-- arrowhead marker definition -->
<marker id="arrow" viewBox="0 0 10 10" refX="5" refY="5" markerWidth="4" markerHeight="4" orient="auto-start-reverse">
<path d="M 0 0 L 10 5 L 0 10 z" stroke="none" fill="#aaa"></path>
</marker>
<g id="score">
<g class="score">
<rect x="0" y="0" width="6" height="3" rx="0.8" ry="0.8" fill="#ddd" stroke="#aaa" stroke-width="0.1"></rect>
<text x="0" y="0" dx="1.5" dy="1.7" class="small score-left" alignment-baseline="middle" text-anchor="middle">0</text>
<text x="0" y="0" dx="3" dy="1.6" class="small score-divisor" alignment-baseline="middle" text-anchor="middle">-</text>
<text x="0" y="0" dx="4.5" dy="1.7" class="small score-right" alignment-baseline="middle" text-anchor="middle">0</text>
</g>
</g>
<line id="line-template" x1="0" y1="0" x2="0" y2="0" fill="none" stroke="#aaa" stroke-width="0.2" marker-end="url(#arrow)"></line>
</defs>
</svg>
<div id="tooltip" class="hidden">
<table>
<tr>
<td>Brazil</td>
<td>2</td>
<td>x</td>
<td>0</td>
<td>Belgium</td>
</tr>
<tr>
<td>France</td>
<td>1</td>
<td>x</td>
<td>2</td>
<td>Uruguay</td>
</tr>
</table>
</div>
<script>
class Match {
constructor (team1, team2, scoreProgression) {
this.team1 = team1;
this.team2 = team2;
this.scoreProgression = scoreProgression;
this.team1Score = -1;
this.team2Score = -1;
}
}
class App {
constructor () {
this.scoreTemplate = document.getElementById("score");
this.svg = document.querySelector("svg");
this.tooltip = document.getElementById("tooltip");
/** @type {Map<String, Element>} */
this.elementByScore = new Map();
/** @type {Map<String, Number>} */
this.countByScore = new Map();
/** @type {Set<String>} */
this.finalScores = new Set();
/** @type {Map<String, HTMLTableElement>} */
this.matchesTableByFinalScore = new Map();
/** @type {Map<String, Set<String>>} */
this.intermediateScoresByFinalScore = new Map();
this.maximumOccurrence = 0;
this.matches = [
// Group phase
new Match("Russia", "Saudi Arabia", "<<<<<"), // 5x0
new Match("Uruguay", "Egypt", "<"), // 1x0
new Match("Iran", "Morocco", "<"), // 1x0
new Match("Portugal", "Spain", "<><>><"), // 3x3
new Match("France", "Australia", "<><"), // 2x1
new Match("Argentina", "Iceland", "<>"), // 1x1
new Match("Denmark", "Peru", "<"), // 1x0
new Match("Croatia", "Nigeria", "<<"), // 2x0
new Match("Serbia", "Costa Rica", "<"), // 1x0
new Match("Mexico", "Germany", "<"), // 1x0
new Match("Brazil", "Switzerland", "<>"), // 1x1
new Match("Sweden", "Korea Republic", "<"), // 1x0
new Match("Belgium", "Panama", "<<<"), // 3x0
new Match("England", "Tunisia", "<><"), // 2x1
new Match("Japan", "Colombia", "<><"), // 2x1
new Match("Senegal", "Poland", "<<>"), // 2x1
new Match("Russia", "Egypt", "<<<>"), // 3x1
new Match("Portugal", "Morocco", "<"), // 1x0
new Match("Uruguay", "Saudi Arabia", "<"), // 1x0
new Match("Spain", "Iran", "<"), // 1x0
new Match("Denmark", "Australia", "<>"), // 1x1
new Match("France", "Peru", "<"), // 1x0
new Match("Croatia", "Argentina", "<<<"), // 3x0
new Match("Brazil", "Costa Rica", "<<"), // 2x0
new Match("Nigeria", "Iceland", "<<"), // 2x0
new Match("Serbia", "Switzerland", "<>>"), // 1x2
new Match("Belgium", "Tunisia", "<<><<<>"), // 5x2
new Match("Mexico", "Korea Republic", "<<>"), // 2x1
new Match("Sweden", "Germany", "<>>"), // 1x2
new Match("England", "Panama", "<<<<<<>"), // 6x1
new Match("Senegal", "Japan", "<><>"), // 2x2
new Match("Colombia", "Poland", "<<<"), // 3x0
new Match("Uruguay", "Russia", "<<<"), // 3x0
new Match("Egypt", "Saudi Arabia", "<>>"), // 1x2
new Match("Morocco", "Spain", "<><>"), // 2x2
new Match("Portugal", "Iran", "<>"), // 1x1
new Match("Peru", "Australia", "<<"), // 2x0
new Match("Denmark", "France", ""), // 0x0
new Match("Argentina", "Nigeria", "<><"), // 2x1
new Match("Korea Republic", "Germany", "<<"), // 2x0
new Match("Sweden", "Mexico", "<<<"), // 3x0
new Match("Brazil", "Serbia", "<<"), // 2x0
new Match("Switzerland", "Costa Rica", "<><>"), // 2x2
new Match("Poland", "Japan", "<"), // 1x0
new Match("Colombia", "Senegal", "<"), // 1x0
new Match("Panama", "Tunisia", "<>>"), // 1x2
new Match("Belgium", "England", "<"), // 1x0
// Round of 16
new Match("France", "Argentina", "<>><<<>"), // 4x3
new Match("Uruguay", "Portugal", "<><"), // 2x1
new Match("Spain", "Russia", "<>"), // 1x1
new Match("Denmark", "Croatia", "<>"), // 1x1
new Match("Brazil", "Mexico", "<<"), // 2x0
new Match("Japan", "Belgium", "<<>>>"), // 2x3
new Match("Sweden", "Switzerland", "<"), // 1x0
new Match("England", "Colombia", "<>"), // 1x1
// Quarter-finals
new Match("France", "Uruguay", "<<"), // 2x0
new Match("Belgium", "Brazil", "<<>"), // 2x1
new Match("England", "Sweden", "<<"), // 2x0
new Match("Russia", "Croatia", "<>><"), // 2x2
// Semi-finals
new Match("France", "Belgium", "<"), // 1x0
new Match("England", "Croatia", "<>>"), // 1x2
// Third place play-off
new Match("Belgium", "England", "<<"), // 2x0
// Final
new Match("France", "Croatia", "<><<<>"), // 4x2
];
this.generateScoreElements();
this.matches.forEach(match => this.processMatch(match));
this.updateOpacities();
this.highlightFinalScores();
}
generateScoreElements() {
this.makeScore(0, 0, App.calculateX(0, 1), App.calculateY(0));
for (let totalGoals = 1; totalGoals <= App.MAX_GOALS; totalGoals++) {
for (let team1Score = totalGoals; team1Score > 0; team1Score--) {
const team2Score = totalGoals - team1Score;
const x = App.calculateX(team2Score, totalGoals);
const y = App.calculateY(totalGoals);
this.makeScore(team1Score, team2Score, x, y);
}
}
}
updateOpacities() {
for (const [key, element] of this.elementByScore.entries()) {
const count = this.countByScore.get(key) || 0;
const opacityIndex = count / this.maximumOccurrence;
// const opacity = count ? 0.4 + 0.6 * opacityIndex : 0.05;
// element.setAttribute("opacity", opacity.toString());
if (count) {
const inverseIndex = 1 - opacityIndex;
const weightedInverseIndex = Math.pow(inverseIndex, 10);
const redComponent = 105 + Math.trunc(150 * weightedInverseIndex);
const redStr = redComponent.toString(16);
const color = `#FF${redStr.length === 1 ? "0" : ""}${redStr}00`;
element.querySelector("rect").setAttribute("fill", color);
}
}
}
highlightFinalScores() {
for (const key of this.finalScores) {
const element = this.elementByScore.get(key);
const rect = element.querySelector("rect");
rect.classList.add("final-score");
}
}
/**
* @param {Match} match
*/
processMatch(match) {
const scoreProgression = match.scoreProgression;
const team1 = scoreProgression[0];
const intermediateScores = new Set();
match.team1Score = 0;
match.team2Score = 0;
const nullScoreKey = App.getKeyFromScore(match.team1Score, match.team2Score);
this.incrementScore(nullScoreKey);
intermediateScores.add(nullScoreKey);
for (const team of scoreProgression) {
team === team1 ? match.team1Score++ : match.team2Score++;
const key = App.getKeyFromScore(match.team1Score, match.team2Score);
this.incrementScore(key);
intermediateScores.add(key);
}
const finalScoreKey = App.getKeyFromScore(match.team1Score, match.team2Score);
intermediateScores.add(finalScoreKey);
let accumulatedScores = this.intermediateScoresByFinalScore.get(finalScoreKey);
if (accumulatedScores) {
for (const score of accumulatedScores) {
intermediateScores.add(score);
}
}
this.intermediateScoresByFinalScore.set(finalScoreKey, intermediateScores);
this.processFinalScore(match, finalScoreKey);
}
/**
* @param {Match} match
* @param {String} finalScoreKey
*/
processFinalScore(match, finalScoreKey) {
this.finalScores.add(finalScoreKey);
let table = this.matchesTableByFinalScore.get(finalScoreKey);
if (!table) {
table = document.createElement("table");
this.matchesTableByFinalScore.set(finalScoreKey, table);
}
const row = table.insertRow();
row.insertCell().innerHTML = match.team1;
row.insertCell().innerHTML = match.team1Score;
row.insertCell().innerHTML = "-";
row.insertCell().innerHTML = match.team2Score;
row.insertCell().innerHTML = match.team2;
}
incrementScore(key) {
let count = this.countByScore.get(key);
count = !count ? 1 : count + 1;
this.countByScore.set(key, count);
if (count > this.maximumOccurrence) {
this.maximumOccurrence = count;
}
}
static getKeyFromScore(team1Score, team2Score) {
return `${team1Score}x${team2Score}`;
}
static calculateY(numScores) {
return App.DELTA_Y * (numScores + 1);
}
static calculateX(scoreIndex, numScores) {
const maxWidth = App.MAX_GOALS * App.SCORE_WIDTH + (App.MAX_GOALS - 1) * App.HORIZONTAL_SPACING;
const lineWidth = numScores * App.SCORE_WIDTH + (numScores - 1) * App.HORIZONTAL_SPACING;
return maxWidth / 2 - lineWidth / 2 + scoreIndex * (App.SCORE_WIDTH + App.HORIZONTAL_SPACING);
}
makeScore(team1Score, team2Score, x, y) {
const g = this.scoreTemplate.cloneNode(true);
const key = App.getKeyFromScore(team1Score, team2Score);
const coreGroup = g.querySelector(".score");
this.elementByScore.set(key, coreGroup);
coreGroup.setAttribute("data-score", key);
coreGroup.addEventListener("mousemove", this.onScoreMouseMove.bind(this, key));
coreGroup.addEventListener("mouseout", this.onScoreMouseOut.bind(this, key));
g.removeAttribute("id");
g.querySelector(".score-left").innerHTML = team1Score.toString();
g.querySelector(".score-right").innerHTML = team2Score.toString();
App.setGroupPosition(g, x, y);
if (team1Score + team2Score === 0) {
// 0x0 -> 1x0
const line = document.getElementById("line-template").cloneNode(true);
App.setLinePosition(line, App.SCORE_WIDTH / 2, App.SCORE_HEIGHT, App.SCORE_WIDTH / 2, App.DELTA_Y - 0.4);
g.appendChild(line);
} else if (team1Score + team2Score < App.MAX_GOALS) {
// left team scored
const lineLeft = document.getElementById("line-template").cloneNode(true);
App.setLinePosition(lineLeft, App.SCORE_WIDTH / 2 - 1.5, App.SCORE_HEIGHT, -0.4, App.DELTA_Y - 0.2);
g.appendChild(lineLeft);
// right team scored
const lineRight = document.getElementById("line-template").cloneNode(true);
App.setLinePosition(lineRight, App.SCORE_WIDTH / 2 + 1.5, App.SCORE_HEIGHT, App.SCORE_WIDTH + 0.4, App.DELTA_Y - 0.2);
g.appendChild(lineRight);
}
this.svg.appendChild(g);
}
onScoreMouseMove(key, event) {
const intermediateScores = this.intermediateScoresByFinalScore.get(key);
if (intermediateScores) {
this.dimAllScores();
for (const score of intermediateScores) {
const element = this.elementByScore.get(score);
element.setAttribute("opacity", "1");
}
}
const table = this.matchesTableByFinalScore.get(key);
if (table) {
this.tooltip.style.left = (event.clientX + 50) + "px";
this.tooltip.style.top = event.clientY + "px";
this.tooltip.innerHTML = "";
this.tooltip.appendChild(this.matchesTableByFinalScore.get(key));
this.tooltip.classList.remove("hidden");
}
}
onScoreMouseOut(key, event) {
this.tooltip.classList.add("hidden");
this.showAllScores();
}
dimAllScores() {
for (const element of this.elementByScore.values()) {
element.setAttribute("opacity", "0.1");
}
}
showAllScores() {
for (const element of this.elementByScore.values()) {
element.setAttribute("opacity", "1");
}
}
static setGroupPosition(element, x, y) {
element.setAttribute("transform", `translate(${x} ${y})`);
}
static setLinePosition(line, x1, y1, x2, y2) {
line.setAttribute("x1", x1);
line.setAttribute("y1", y1);
line.setAttribute("x2", x2);
line.setAttribute("y2", y2);
}
static get MAX_GOALS() { return 9; }
static get SCORE_WIDTH() { return 6; } // taken from the svg def
static get SCORE_HEIGHT() { return 3; } // taken from the svg def
static get HORIZONTAL_SPACING() { return 2; }
static get DELTA_Y() { return 5; }
static get DELTA_X() { return App.SCORE_WIDTH + App.HORIZONTAL_SPACING; }
}
new App();
</script>
</body>
</html>