-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
1385 lines (1071 loc) · 39.7 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
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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<head>
<!-- This visualisation mainly makes use of Adam Pearce's graph-scroll.js and a d3 block from https://bl.ocks.org/SpaceActuary/d6b5ca8e5fb17842d652d0de21e88a05 . Various other code snippets from stackoverflow have been used, have mentioned their urls . -->
<script type="text/javascript">
(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-78400808-1', 'auto');
ga('send', 'pageview');
</script>
<!-- for Facebook -->
<meta property="og:title" content="The history of India-Pakistan cricket matches...in dots!" />
<meta property="og:type" content="article" />
<meta property="og:image" content="http://i.imgur.com/8SrKyec.png" />
<meta property="og:description" content="With India facing Pakistan in the Champions Trophy final, here's an interactive to make sense of this fractious encounter." />
<!-- PUT IN AUTHOR NAME HERE SOMEWHERE -->
<!-- for Twitter -->
<meta name="twitter:card" content="summary" />
<meta name="twitter:title" content="The history of India-Pakistan cricket matches...in dots!" />
<meta name="twitter:description" content="With India facing Pakistan in the Champions Trophy final, here's an interactive to make sense of this fractious encounter." />
<meta name="twitter:image" content="http://i.imgur.com/8SrKyec.png" />
<style>
@import url(https://fonts.googleapis.com/css?family=Khula:400,700);
body{
background: #ffda29;
max-width: 900px;
margin: 0px auto;
font-family: 'Khula', sans-serif;
color:#4f4f4f;
}
#container{
position: relative;
}
#sections{
width: 340px;
}
#sections > div{
opacity: .2;
margin-bottom: 300px;
}
#sections > div:last-child{
margin-bottom: 50px;
}
#sections > div.graph-scroll-active{
opacity: 1;
}
#graph{
margin-left: 40px;
width: 500px;
position: absolute;
top: 0px;
margin-left: 380px;
}
.graph-scroll-fixed #graph{
position: fixed;
top: 0px;
}
.graph-scroll-below #graph{
bottom: 0px;
top: auto;
}
@media (max-width: 925px) {
#graph{
width: 100%;
transform: translate(-50%, 0);
margin-left: 50%;
}
#sections{
position: relative;
margin: 0px auto;
padding-top: 400px;
}
#sections div{
background: rgba(255,218,41,0.9); /* alpha parameter */
padding: 10px;
/* border: 1px solid; */
max-width: 400px;
margin-bottom: 500px;
}
}
h1{
margin: 50px;
}
h1, h3, h4{
text-align: center;
}
/*
svg{
border: 1px solid black;
}
*/
text {
font-family: 'Khula', sans-serif;
font-size: 15px;
fill: #4f4f4f;
font-weight: bold;
}
text.footnote{
font-size:12px;
font-weight: normal;
}
text.value {
fill: #ffda29;
opacity:1;
}
text.title {
font-size:16px;
font-weight: bold;
line-height:20px;
}
a {
color: #4f4f4f;
}
</style>
</head>
<body>
<h3>THE HISTORY OF INDIA-PAKISTAN CRICKET MATCHES...IN DOTS</h3>
<p style="margin-left:25px;margin-right:25px;">India will face Pakistan in the Champions Trophy final on Sunday, the 134th time they will have played each other. So what happened in the 133 other One-day matches they've played since 1978? This interactive helps you make sense of this fractious encounter.</p>
<h4>⇓ SCROLL DOWN TO BEGIN ⇓</h4>
<div id='container'>
<div id='graph'></div>
<div id='sections'>
<!-- SEC 1 -->
<div><p>Each dot in the chart represents a match played between the two countries. (If you're viewing this on the desktop, you can hover over a dot to get a summary of the match.) <span style="font-weight:bold;"><img src="graphics/pak_min.png" style="width:14px;height:14px;"> represents a match won by Pakistan, <img src="graphics/ind_min.png" style="width:14px;height:14px;"> a match won by India</span> and <img src="graphics/no_min.png" style="width:14px;height:14px;"> stands for a match that was abandoned or cancelled.</p></div>
<!-- SEC 2 -->
<div><p><p style="font-weight:bold;text-align:center;">Pakistan's won more</p>India first played Pakistan in a one-day international on October 1, 1978 in Quetta, Pakistan. India may have won that match by four runs, but since then <span style="font-weight:bold;">Pakistan has won the majority of matches, 72 out of 133 or 54%.</span></p></div>
<!-- SEC 3 -->
<div><p><p style="font-weight:bold;text-align:center;">Pakistan's dominance is waning though</p>Each of the 'blobs' in the chart represents matches played in a five-year period. We can see from the chart that Pakistan dominated <span style="font-weight:bold;">till the mid-2000s, after which the tide started to turn in India's favour.</span></p></div>
<!-- SEC 4 -->
<div><p style="font-weight:bold;text-align:center;">They love playing matches abroad</p><p>74 out of the 133 One-day matches between the two, or <span style="font-weight:bold;">55%, have been played outside India and Pakistan</span>. They've played each other 26 times in the UAE and also, interestingly, 16 times in Canada. This happened from 1996-98 when Toronto hosted an annual one-day series between the two countries.</p></div>
<!-- SEC 5 -->
<div><p style="font-weight:bold;text-align:center;">It pays to bat first</p><p>In the 133 matches played between India and Pakistan,<span style="font-weight:bold;"> the team batting first has won more matches -- 66 -- than the team bowling first -- 58</span>. There have been just two matches in England played between the two countries (including the group-stage encounter) and both were won by the team batting first.</p></div>
<!-- SEC 6 -->
<div><p style="font-weight:bold;text-align:center;">These matches are tight!</p><p>This chart looks at the margin of victory, ie. how many runs or wickets teams have won by. <span style="font-weight:bold;">If a country wins an India-Pakistan match batting first, it usually wins by less than 50 runs!</span></p></div>
</div>
</div>
<!-- PUT LINK TO GITHUB REPO BELOW -->
<h3 id='source'><a href='https://github.com/shijithpk/history_of_india_pakistan_cricket_matches'>CREDITS, CODE AND DATA</a></h3>
<center><a href="https://www.wionews.com"><img src="graphics/wion_horizontal.svg" width="200"></a></center>
<script src="scripts/d3v4+jetpack.js"></script>
<script src="scripts/graph-scroll.js"></script>
<script>
var oldWidth = 0
function render(){
if (oldWidth == innerWidth) return
oldWidth = innerWidth
var width = d3.select('#graph').node().offsetWidth,
height = innerWidth > 925 ? width : innerHeight * 0.9
;
//,
//this sets the graph on mobile at a 7th of the height, while on desktop it'll be a square
var svg = d3
.select('#graph')
.html('')
.append('svg')
.attrs({width: width, height: height})
;
function canvas_clear(){
svg
.selectAll("*")
.remove();
}
var radius = 5;
var iconsx = {
"India":"graphics/ind_min.svg",
"Pakistan":"graphics/pak_min.svg",
"No result":"graphics/no_min.svg"
};
//the code is a bit repetitious from this point on, sadly couldn't figure out how to do
//this viz without repeating the code :( .
//Man, I have to start getting better at javascript
//couldn't figure out a way to have the same circles moving around between
//charts, the graphic is generated afresh five times ! :(
//may have to look at using D3 with canvas elements for these kinds of graphics
function sec_1(){ //all the dots together
canvas_clear();
d3.csv("data/ind_pak_v2.csv", function(data){
data.forEach(function(d){
d.r = radius;
d.x = width/2;
d.y = height/2;
})
console.table(data);
var images = svg
.selectAll("image")
.data(data)
;
//idea for code below from http://bl.ocks.org/syntagmatic/4963194
var imagesEnter = images
.enter()
.append("image")
// .attr("r", function(d, i){ return d.r; })
.attr("x", function(d, i){ return 175 + 25 * i + 2 * i ** 2; })
.attr("y", function(d, i){ return 250; })
.attr("xlink:href", function(d) { return iconsx[d.winner];})
.attr("height", 20)
.attr("width", 20)
;
images = images
.merge(imagesEnter)
;
images
.append("title")
.text(function(d, i) {
if (d.winner != 'No result'){return "One-day match at " + d.ground + " (" + d.country + ") on " + d.date + ". " + d.winner + " won by " + d.margin + ".";}
else{return "One-day match at " + d.ground + " (" + d.country + ") on " + d.date + ". Match abandoned/cancelled.";}
});
function ticked() {
images
//.attr("x", function(d){ return d.x; })
// .attr("y", function(d){ return d.y; })
.attr("x", function(d){ return d.x = Math.max(d.r, Math.min(width - d.r, d.x)); })
.attr("y", function(d){ return d.y = Math.max(d.r, Math.min(height - d.r, d.y)); })
;
}
;
//from http://vallandingham.me/bubble_charts_with_d3v4.html
//a tick callback method that will draw the nodes after each iteration of the simulation.The ticked function for this is super simple, as it just moves the SVG circles used to make -the bubbles to their new positions
//we have our bubbles all coalescing together in a big bubble pile. Note that we aren’t using any transitions here. The “animation” is taken care of by the rapid repeated execution of the ticked function
//from http://d3indepth.com/force-layout/
//Each time the simulation iterates the function ticked will be called. This function joins the nodes array to circle elements and updates their positions
var simulation = d3
.forceSimulation()
//keeping the commented lines below in case I need them later
// .force("collide",d3.forceCollide( function(d){
// return d.r + 0 }).iterations(16))
//d.r is defined as radius above
.force("charge", d3.forceManyBody()
.strength(-10 )) //the default strength is -30
/*
.force("y", d3
.forceY()
//.strength(0.01)
.y(height / 2)
)
.force("x", d3
.forceX()
//.strength(0.01)
.x(width / 2)
)
*/
;
simulation
.nodes(data)
.on("tick", ticked)
;
var forceStrength = //1;
0.05;
function groupBubbles() {
simulation
.force('x', d3
.forceX()
.strength(forceStrength)
.x(width / 2))
.force('y', d3
.forceY()
.strength(forceStrength)
.y(width / 2))
;
simulation
.alpha(2)
// .alphaDecay(0.1)
//.alphaTarget(0.1)
.restart()
;
svg
.selectAll('text')
.data(data)
.enter()
.append('text')
.attr('x', function (d) { return bubble_position_x(d); })
.attr('y', function (d) { return bubble_position_y(d); })
.attr('text-anchor', 'middle')
//is there a way to vertically anchor the text?
.text(function (d) { return d[byVar];})
;
}
groupBubbles();
});
d3.xml("graphics/pg_1_min.svg", function(error, xml) {
if (error) {console.log(error); return;}
var svg_node = xml
.getElementsByTagName("svg")[0]
;
svg
.node()
.appendChild(svg_node)
;
d3
.select("svg#pg_number")
.attr("x", width - 60)
.attr('y', height - 15)
;
});
}
function sec_2(){ //who's won more
canvas_clear();
d3.csv("data/ind_pak_v2.csv", function(data){
svg
.append('text')
.attr('class','title')
.attr('y', 20)
.text("Who's won more matches?")
.attr('x',width/2)
.attr('text-anchor', 'middle')
;
data.forEach(function(d){
d.r = radius;
d.x = width / 2;
d.y = height / 2;
})
console.table(data);
var images = svg
.selectAll("image")
.data(data)
;
//idea for code below from http://bl.ocks.org/syntagmatic/4963194
var imagesEnter = images
.enter()
.append("image")
// .attr("r", function(d, i){ return d.r; })
.attr("x", function(d, i){ return 175 + 25 * i + 2 * i ** 2; })
.attr("y", function(d, i){ return 250; })
.attr("xlink:href", function(d) { return iconsx[d.winner];})
.attr("height", 20)
.attr("width", 20)
;
images = images
.merge(imagesEnter)
;
images
.append("title")
.text(function(d, i) {
if (d.winner != 'No result'){return "One-day match at " + d.ground + " (" + d.country + ") on " + d.date + ". " + d.winner + " won by " + d.margin + ".";}
else{return "One-day match at " + d.ground + " (" + d.country + ") on " + d.date + ". Match tied/cancelled." + ".";}
});
function ticked() {
images
//.attr("x", function(d){ return d.x; })
//.attr("y", function(d){ return d.y; })
.attr("x", function(d){ return d.x = Math.max(d.r, Math.min(width - d.r, d.x)); })
.attr("y", function(d){ return d.y = Math.max(d.r, Math.min(height - d.r, d.y)); })
;
}
;
var simulation = d3
.forceSimulation()
.force("charge", d3
.forceManyBody()
.strength(-6)
) //the default strength is -30
/*
//d.r is radius, defined on top as 5
.force("collide",d3
.forceCollide( function(d){
return d.r + 0.5; })
//.strength(0)
.iterations(16)
)
*/
;
simulation
.nodes(data)
.on("tick", ticked)
;
//the code below for setting the foci from https://github.com/vlandham/bubble_chart_v4/blob/master/src/bubble_chart.js
var bubble_foci = {
India: { x: width / 2, y: height / 3 },
Pakistan: { x: width / 3, y: 2*height / 3 },
'No result': { x: 2*width / 3, y: 2*height / 3 }
};
var forceStrength = //1;
0.05;
//byVar is just a variable
function splitBubbles(byVar) {
function bubble_position_x(d) {
return bubble_foci[d[byVar]].x;
}
function bubble_position_y(d) {
return bubble_foci[d[byVar]].y;
}
simulation
.force('x', d3
.forceX()
.strength(forceStrength)
.x(bubble_position_x)
)
.force('y', d3
.forceY()
.strength(forceStrength)
.y(bubble_position_y)
)
;
simulation.alpha(2).restart();
svg
.selectAll('rect')
.data(data)
.enter()
.append('rect')
.attr("fill", "#ffda29")
.style("fill-opacity", 0.07)
.attr("width", 65)
.attr("height", 16)
.attr("rx",'5')
.attr("ry",'5')
.attr('x', function (d) { return bubble_position_x(d); })
.attr('y', function (d) { return bubble_position_y(d); })
;
svg
.selectAll('text')
//.data(data, function(d){ return d[byVar];})
.data(data)
.enter()
.append('text')
.attr('x', function (d) { return bubble_position_x(d); })
.attr('y', function (d) { return bubble_position_y(d); })
.attr('text-anchor', 'start')
.attr('alignment-baseline',"hanging")
.text(function (d) { return d[byVar]; })
;
}
splitBubbles('winner');
});
d3.xml("graphics/pg_2_min.svg", function(error, xml) {
if (error) {console.log(error); return;}
var svg_node = xml
.getElementsByTagName("svg")[0]
;
svg
.node()
.appendChild(svg_node)
;
d3
.select("svg#pg_number")
.attr("x", width - 60)
.attr('y', height - 15)
;
});
}
function sec_3(){ //how has dominance changed over the decades
canvas_clear();
d3.csv("data/ind_pak_v2.csv", function(data){
svg
.append('text')
.attr('class','title')
.attr('y', 20)
.text("Who's winning more now?")
.attr('x',width/2)
.attr('text-anchor', 'middle')
;
data.forEach(function(d){
d.r = radius;
d.x = width / 2;
d.y = height / 2;
})
console.table(data);
var images = svg
.selectAll("image")
.data(data)
;
//idea for code below from http://bl.ocks.org/syntagmatic/4963194
var imagesEnter = images
.enter()
.append("image")
// .attr("r", function(d, i){ return d.r; })
.attr("x", function(d, i){ return 175 + 25 * i + 2 * i ** 2; })
.attr("y", function(d, i){ return 250; })
.attr("xlink:href", function(d) { return iconsx[d.winner];})
.attr("height", 20)
.attr("width", 20)
;
images = images
.merge(imagesEnter)
;
images
.append("title")
.text(function(d, i) {
if (d.winner != 'No result'){return "One-day match at " + d.ground + " (" + d.country + ") on " + d.date + ". " + d.winner + " won by " + d.margin + ".";}
else{return "One-day match at " + d.ground + " (" + d.country + ") on " + d.date + ". Match tied/cancelled." + ".";}
});
function ticked() {
images
// .attr("x", function(d){ return d.x; })
// .attr("y", function(d){ return d.y; })
.attr("x", function(d){ return d.x = Math.max(d.r, Math.min(width - d.r, d.x)); })
.attr("y", function(d){ return d.y = Math.max(d.r, Math.min(height - d.r, d.y)); })
;
}
;
var simulation = d3
.forceSimulation()
.force("charge", d3
.forceManyBody()
.strength(-5)
) //the default strength is -30
/*
//d.r is defined as radius above
.force("collide",d3
.forceCollide( function(d){
return d.r + 0.5; })
//.strength(0)
.iterations(16)
)
*/
;
simulation
.nodes(data)
.on("tick", ticked)
;
/*
var bubble_foci = {
'1970-1979': { x: width *3/8, y: height /4 },
'1980-1989': { x: width *5/ 8, y: height /4 },
'1990-1999': { x: width /2, y: height /2 },
'2000-2009': { x: width *3/8, y: height * 3/4 },
'2010-2017': { x: width * 5/8, y: height * 3/4 }
}
*/
var bubble_foci = {
'1975-1979': { x: width *2/8, y: height *1/4 },
'1980-1984': { x: width *2/4, y: height *1/4 },
'1985-1989': { x: width *6/8, y: height *1/4 },
'1990-1994': { x: width *2/8, y: height *2/4 },
'1995-1999': { x: width *2/4, y: height *2/4 },
'2000-2004': { x: width *6/8, y: height *2/4 },
'2005-2009': { x: width *2/8, y: height *3/4 },
'2010-2014': { x: width *2/4, y: height *3/4 },
'2015-2017': { x: width *6/8, y: height *3/4 }
}
var forceStrength = //1;
0.05;
function splitBubbles(byVar) {
function bubble_position_x(d) {
return bubble_foci[d[byVar]].x;
}
function bubble_position_y(d) {
return bubble_foci[d[byVar]].y;
}
simulation
.force('x', d3
.forceX()
.strength(forceStrength)
.x(bubble_position_x)
)
.force('y', d3
.forceY()
.strength(forceStrength)
.y(bubble_position_y)
)
;
simulation.alpha(2).restart();
svg
.selectAll('rect')
.data(data)
.enter()
.append('rect')
.attr("fill", "#ffda29")
.style("fill-opacity", 0.07)
.attr("width", 70)
.attr("height", 16)
.attr("rx",'5')
.attr("ry",'5')
.attr('x', function (d) { return bubble_position_x(d); })
.attr('y', function (d) { return bubble_position_y(d); })
;
svg
.selectAll('text')
//.data(data, function(d){ return d[byVar];})
.data(data)
.enter()
.append('text')
.attr('x', function (d) { return bubble_position_x(d); })
.attr('y', function (d) { return bubble_position_y(d); })
.attr('text-anchor', 'start')
.attr('alignment-baseline',"hanging")
.text(function (d) { return d[byVar]; })
;
}
splitBubbles('half_decade');
});
d3.xml("graphics/pg_3_min.svg", function(error, xml) {
if (error) {console.log(error); return;}
var svg_node = xml
.getElementsByTagName("svg")[0]
;
svg
.node()
.appendChild(svg_node)
;
d3
.select("svg#pg_number")
.attr("x", width - 60)
.attr('y', height - 15)
;
});
}
function sec_4(){ //where have they played each other?
canvas_clear();
d3.csv("data/ind_pak_v2.csv", function(data){
svg
.append('text')
.attr('class','title')
.attr('y', 20)
.text("Where have they played each other?")
.attr('x',width/2)
.attr('text-anchor', 'middle')
;
data.forEach(function(d){
d.r = radius;
d.x = width / 2;
d.y = height / 2;
})
console.table(data);
var images = svg
.selectAll("image")
.data(data)
;
//idea for code below from http://bl.ocks.org/syntagmatic/4963194
var imagesEnter = images
.enter()
.append("image")
// .attr("r", function(d, i){ return d.r; })
.attr("x", function(d, i){ return 175 + 25 * i + 2 * i ** 2; })
.attr("y", function(d, i){ return 250; })
.attr("xlink:href", function(d) { return iconsx[d.winner];})
.attr("height", 20)
.attr("width", 20)
;
images = images
.merge(imagesEnter)
;
images
.append("title")
.text(function(d, i) {
if (d.winner != 'No result'){return "One-day match at " + d.ground + " (" + d.country + ") on " + d.date + ". " + d.winner + " won by " + d.margin + ".";}
else{return "One-day match at " + d.ground + " (" + d.country + ") on " + d.date + ". Match tied/cancelled." + ".";}
});
function ticked() {
images
//.attr("x", function(d){ return d.x; })
//.attr("y", function(d){ return d.y; })
.attr("x", function(d){ return d.x = Math.max(d.r, Math.min(width - d.r, d.x)); })
.attr("y", function(d){ return d.y = Math.max(d.r, Math.min(height - d.r, d.y)); })
;
}
;
var simulation = d3
.forceSimulation()
.force("charge", d3
.forceManyBody()
.strength(-6)
) //the default strength is -30
/*
//d.r is defined as radius above
.force("collide",d3
.forceCollide( function(d){
return d.r + 0.5; })
//.strength(0)
.iterations(16)
)
*/
;
simulation
.nodes(data)
.on("tick", ticked)
;
//in these cases the clusters don't have to be in order
//it would be better to let the cluster foci be automatically generated
// and not be manually assigned like I did below
var bubble_foci = {
India: { x: width * 1/ 3, y: height * 3/12 },
Pakistan: { x: width * 2/3, y: height * 3/12 },
UAE: { x: width * 1/3, y: height * 5/12 },
Canada: { x: width * 2/3, y: height * 5/12 },
"B'desh": { x: width * 1/9, y: height * 8/12 },
Australia: { x: width * 3/9, y: height * 8/12 },
'S. Lanka': { x: width * 5/9, y: height * 8/12 },
USA: { x: width * 7/9, y: height * 8/12 },
UK: { x: width * 1/9, y: height * 10/12 },
'S. Africa': { x: width * 3/9, y: height * 10/12 },
"N'lands": { x: width * 5/9, y: height * 10/12 },
"S'pore": { x: width * 7/9, y: height * 10/12 }
}
var forceStrength = //1;
0.05;
function splitBubbles(byVar) {
function bubble_position_x(d) {
return bubble_foci[d[byVar]].x;
}
function bubble_position_y(d) {
return bubble_foci[d[byVar]].y;
}
simulation
.force('x', d3
.forceX()
.strength(forceStrength)
.x(bubble_position_x)
)
.force('y', d3
.forceY()
.strength(forceStrength)
.y(bubble_position_y)
)
;
simulation.alpha(2).restart();
svg
.selectAll('rect')
.data(data)
.enter()
.append('rect')
.attr("fill", "#ffda29")
.style("fill-opacity", 0.07)
.attr("width", 60)
.attr("height", 16)
.attr("rx",'5')
.attr("ry",'5')
.attr('x', function (d) { return bubble_position_x(d); })
.attr('y', function (d) { return bubble_position_y(d); })
;
svg
.selectAll('text')
//.data(data, function(d){ return d[byVar];})
.data(data)
.enter()
.append('text')
.attr('x', function (d) { return bubble_position_x(d); })
.attr('y', function (d) { return bubble_position_y(d); })
.attr('text-anchor', 'start')
.attr('alignment-baseline',"hanging")
.text(function (d) { return d[byVar]; })
;
}
splitBubbles('country');
});
d3.xml("graphics/pg_4_min.svg", function(error, xml) {
if (error) {console.log(error); return;}
var svg_node = xml
.getElementsByTagName("svg")[0]
;
svg
.node()
.appendChild(svg_node)
;
d3
.select("svg#pg_number")
.attr("x", width - 60)
.attr('y', height - 15)
;
});
}
function sec_5(){ //team first action, did team batting first win or lose?
canvas_clear();
//different csv here, removed matches where there was no
//result, was messing with the viz. Next time, figure out
//how to exclude rows you don't need through the code itself
d3.csv("data/ind_pak_v2_bat_first_winner.csv", function(data){
svg
.append('text')
.attr('class','title')
.attr('y', 20)
.text("Should a team bat first or bowl?")
.attr('x',width/2)
.attr('text-anchor', 'middle')
;
data.forEach(function(d){
d.r = radius;
d.x = width / 2;
d.y = height / 2;
})