-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.html
1025 lines (634 loc) · 26.5 KB
/
App.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>
<html>
<head>
<meta charset="UTF-8">
<title>NBA Shot Chart Creator</title>
<meta name="author" content="Nathan Harris">
<meta name="description" content="Shot Chart">
<meta name="keywords" content="NBA Basketball Steph Curry Lebron James Basketball">
<!-- twitter meta tags -->
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:site" content="@nathaniharris">
<meta name="twitter:creator" content="@nathaniharris">
<meta name="twitter:title" content="NBA Shot Chart Creator">
<meta name="twitter:description" content="Generate Interactive nba Shot Charts which you can Download">
<meta name="twitter:image" content="https://c1.staticflickr.com/4/3789/32829723900_32767569ab.jpg">
<!-- facebook meta tags -->
<meta property="og:url" content="http://www.nbashotchart.com" />
<meta property="og:type" content="website" />
<meta property="og:title" content="NBA Shot Chart Creator" />
<meta property="og:description" content="Generate Interactive nba Shot Charts which you can Download" />
<meta property="og:image" content="https://c1.staticflickr.com/4/3789/32829723900_32767569ab.jpg" />
<style>
h1, #wrapperAuthor, body{
}
#title{
font-size: 200%;
}
#legendTextText{
margin-top: 120px;
}
#form{
border-top:1px solid black;
border-bottom:1px solid black;
padding-bottom: 10px;
margin-left: 10px;
}
#root{
width:580;
height:550;
float:left;
}
.download {
background: #333;
color: #FFF;
font-weight: 900;
border: 2px solid #B10000;
padding: 4px;
margin:4px;
}
.hexagon {
stroke: #fff;
stroke-width: .0px;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
#pic{
float:left;
border-bottom:1px solid black;
}
#donate{
margin-top:10px;
float:right;
}
#legendSVG{
border-top:1px solid black;
margin-top:0px;
}
#corp{
font-size:12px;
margin-left: 10px;
margin-right:0;
}
#author{
font-size:12px;
float: right;
padding-right: 5px;
}
#corp, #author
{
display:inline;
}
#flex{
width:1000px;
border-right:1px solid black;
margin-top: 10px;
}
#mainTitle{
text-align:center;
font-size: 250%;
}
#explainer{
text-align:center;
width:250px;
margin-top:10px;
margin-left:5px;
color:#333333;
}
#flex, #explainer {
float: left;
}
#container{
overflow: hidden;
}
.fb-share-button{
margin-top 10px;
}
#tweetContainer{
margin-top:5px;
}
</style>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
</head>
<body>
<div id="fb-root"></div>
<script>(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/sdk.js#xfbml=1&version=v2.8";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://d3js.org/d3-hexbin.v0.2.min.js"></script>
<script src="https://d3js.org/d3-selection-multi.v0.4.min.js"></script>
<script src="https://platform.twitter.com/widgets.js"></script>
<script>
var hexbin = d3.hexbin();
</script>
<div id ="wrapperAuthor">
<div id="corp"> an <b>Independentviz.com</b> creation</div> <div id="author"> by <a href="https://www.twitter.com/nathaniharris"><u> Nathan Harris</u></a> </div>
</div>
<h1 id='mainTitle'>Shot Chart Creator</h1>
<div id='form'></div>
<br>
<a
class="download"
href="javascript:(function () { var e = document.createElement('script'); if (window.location.protocol === 'https:') { e.setAttribute('src', 'https://rawgit.com/NYTimes/svg-crowbar/gh-pages/svg-crowbar.js'); } else { e.setAttribute('src', 'http://nytimes.github.com/svg-crowbar/svg-crowbar.js'); } e.setAttribute('class', 'svg-crowbar'); document.body.appendChild(e); })();">
<!--⤋--><big>⇩</big> Download Chart
</a>
<br>
<div id='loading'></div>
<div id='container'><div id='flex'> <div id='root'></div><div id='pic'></div><div id='legendText'></div><div id='legend'></div></div>
<div id='explainer'></div></div>
<div id='svgdataurl'></div>
<div id='pngdataurl'></div>
<div class="fb-share-button" data-href="http://www.nbashotchart.com" data-layout="button_count" data-size="small" data-mobile-iframe="true"><a class="fb-xfbml-parse-ignore" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fwww.nbashotchart.com%2F&src=sdkpreparse">Share</a></div>
<div id="tweetContainer"></div>
<!-- Donate Button -->
<form id="donate" action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="encrypted" value="-----BEGIN PKCS7-----MIIHRwYJKoZIhvcNAQcEoIIHODCCBzQCAQExggEwMIIBLAIBADCBlDCBjjELMAkGA1UEBhMCVVMxCzAJBgNVBAgTAkNBMRYwFAYDVQQHEw1Nb3VudGFpbiBWaWV3MRQwEgYDVQQKEwtQYXlQYWwgSW5jLjETMBEGA1UECxQKbGl2ZV9jZXJ0czERMA8GA1UEAxQIbGl2ZV9hcGkxHDAaBgkqhkiG9w0BCQEWDXJlQHBheXBhbC5jb20CAQAwDQYJKoZIhvcNAQEBBQAEgYAMbDfPwdVC4oNOstjLnN0WEWIk1QU5JMkZo199nVcKuUTVa6fYclv/ZAMepF9UR/5RYR9rLjgu/yWRpcqbwAbDKVzbLVzd5OJlR5uI2zERhQc48kIm8H7rJx8WqcTuJdyACshL/sTga0rRrcGOcgSKrVnUGSHq80xv76+G5AhMvzELMAkGBSsOAwIaBQAwgcQGCSqGSIb3DQEHATAUBggqhkiG9w0DBwQIBYDlLM6qkIiAgaDI8H1tKlka2WHmiCLklqKzagVG0YSohrYwIoQMDamK8+melT4EJuSrkqSAWcOrz+5y5rU7KV7RAmLQQfnrLg0fZ4mQgPg+1Dc7f+9MMFCTCW5se+EpvW0vql3yZ5RB9tatIAI3zM+mwtr3ylABsaPJkELB65ruHRWdIGKkwZV3iIsXXleLWk52eIzavVL2Vnfr3QAfzvQxwdW9uBzzJ6FwoIIDhzCCA4MwggLsoAMCAQICAQAwDQYJKoZIhvcNAQEFBQAwgY4xCzAJBgNVBAYTAlVTMQswCQYDVQQIEwJDQTEWMBQGA1UEBxMNTW91bnRhaW4gVmlldzEUMBIGA1UEChMLUGF5UGFsIEluYy4xEzARBgNVBAsUCmxpdmVfY2VydHMxETAPBgNVBAMUCGxpdmVfYXBpMRwwGgYJKoZIhvcNAQkBFg1yZUBwYXlwYWwuY29tMB4XDTA0MDIxMzEwMTMxNVoXDTM1MDIxMzEwMTMxNVowgY4xCzAJBgNVBAYTAlVTMQswCQYDVQQIEwJDQTEWMBQGA1UEBxMNTW91bnRhaW4gVmlldzEUMBIGA1UEChMLUGF5UGFsIEluYy4xEzARBgNVBAsUCmxpdmVfY2VydHMxETAPBgNVBAMUCGxpdmVfYXBpMRwwGgYJKoZIhvcNAQkBFg1yZUBwYXlwYWwuY29tMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDBR07d/ETMS1ycjtkpkvjXZe9k+6CieLuLsPumsJ7QC1odNz3sJiCbs2wC0nLE0uLGaEtXynIgRqIddYCHx88pb5HTXv4SZeuv0Rqq4+axW9PLAAATU8w04qqjaSXgbGLP3NmohqM6bV9kZZwZLR/klDaQGo1u9uDb9lr4Yn+rBQIDAQABo4HuMIHrMB0GA1UdDgQWBBSWn3y7xm8XvVk/UtcKG+wQ1mSUazCBuwYDVR0jBIGzMIGwgBSWn3y7xm8XvVk/UtcKG+wQ1mSUa6GBlKSBkTCBjjELMAkGA1UEBhMCVVMxCzAJBgNVBAgTAkNBMRYwFAYDVQQHEw1Nb3VudGFpbiBWaWV3MRQwEgYDVQQKEwtQYXlQYWwgSW5jLjETMBEGA1UECxQKbGl2ZV9jZXJ0czERMA8GA1UEAxQIbGl2ZV9hcGkxHDAaBgkqhkiG9w0BCQEWDXJlQHBheXBhbC5jb22CAQAwDAYDVR0TBAUwAwEB/zANBgkqhkiG9w0BAQUFAAOBgQCBXzpWmoBa5e9fo6ujionW1hUhPkOBakTr3YCDjbYfvJEiv/2P+IobhOGJr85+XHhN0v4gUkEDI8r2/rNk1m0GA8HKddvTjyGw/XqXa+LSTlDYkqI8OwR8GEYj4efEtcRpRYBxV8KxAW93YDWzFGvruKnnLbDAF6VR5w/cCMn5hzGCAZowggGWAgEBMIGUMIGOMQswCQYDVQQGEwJVUzELMAkGA1UECBMCQ0ExFjAUBgNVBAcTDU1vdW50YWluIFZpZXcxFDASBgNVBAoTC1BheVBhbCBJbmMuMRMwEQYDVQQLFApsaXZlX2NlcnRzMREwDwYDVQQDFAhsaXZlX2FwaTEcMBoGCSqGSIb3DQEJARYNcmVAcGF5cGFsLmNvbQIBADAJBgUrDgMCGgUAoF0wGAYJKoZIhvcNAQkDMQsGCSqGSIb3DQEHATAcBgkqhkiG9w0BCQUxDxcNMTcwMjIyMDUxNTQ3WjAjBgkqhkiG9w0BCQQxFgQURyWtfT/FXCSVYkxLMiNtD7MfJVYwDQYJKoZIhvcNAQEBBQAEgYCNWXJB/q7/UQK7wAVp7bpZpwIu8w120gQa9zDfyuY+mjf+ufyHLJgHeDWelIHGxmFH9hr7lhilMhWMSP3BiQfWegFt4PVInm4nDGADtkt713oezk2QWxitDD6ZDUT4ZOyiRt3tdGDtddICXP7/VLh7D/ArrOrgW5D4eKxKRVJSUQ==-----END PKCS7-----
">
<input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_donate_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
<img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1">
</form>
<script>
twttr.widgets.createShareButton(
'/',
document.getElementById('tweetContainer'),
{
text: 'http://www.nbashotchart.com'
}
);
//colorSCale
var colorScale = d3.scaleLinear()
.domain([-.055, 0, .055])
.range(["blue", "yellow", "red"]);
var radius = d3.scaleLinear()
.domain([0, 1000])
.range([0, 500]);
//formatting hexagonal data function
var hexDataFormat=function(data){
var hexData=[];
var xData;
var yData;
var leagueAv;
var playerAv;
var dataAverage=[];
data.resultSets[1].rowSet.forEach((aver)=>{
dataAverage.push([aver[1],aver[2],aver[3],aver[6],0,0,0,0])
})
data.resultSets[0].rowSet.forEach((shot)=>{
if(shot[20]){
dataAverage.forEach((av,i)=>{
if(shot[13]==av[0] && shot[14]==av[1] && shot[15]==av[2]){
dataAverage[i][4]+=1;
}
})
}else{
dataAverage.forEach((av,i)=>{
if(shot[13]==av[0] && shot[14]==av[1] && shot[15]==av[2]){
dataAverage[i][5]+=1;
}
})
}
});
dataAverage.forEach((loc,i)=>{
var totalShots =(loc[4]+loc[5])
dataAverage[i][6]=totalShots
dataAverage[i][7]=(dataAverage[i][4]/dataAverage[i][6])
})
console.log('data average')
console.log(dataAverage)
data.resultSets[0].rowSet.forEach((d)=>{
xData=xScale(d[17])
yData=yScale(d[18])
dataAverage.forEach((av)=>{
if(d[13]==av[0] && d[14]==av[1] && d[15]==av[2]){
leagueAv=av[3];
playerAv=av[7];
hexData.push({0:xData,1:yData,league:leagueAv,player:playerAv})
}
})
})
return hexData;
}
//Loading
//loading
var createLoading= function(){
d3.select('#loading').append('div').attr('id','loadingDiv').text('Loading...');
}
var removeLoading=function(){
d3.select('#loadingDiv').remove()
}
//hexagonal ordinal scale
var colors=['#0000ff','#ffff00','#ff0000']
//Tooltip
var showToolTip=function(d){
$(this).popover({
placement:'auto top',
container:'body',
trigger:'manual',
html:true,
content: function(){ return "Shot Distance: "+d[16]+"<br>Action Type: " + d[11]+"</br> Game Date: "
+d[21].slice(4,6)+"-"+d[21].slice(6,8)+"-"+d[21].slice(0,4)+"<br>"+d[14]+"</br>"
}
});
$(this).popover('show');
}
var removeToolTip=function(d){
$('.popover').each(function(){
$(this).remove();
})
}
//chart state
var chartState={chart:'hex'};
//boilerplate D3
var margin={top:40, r:40, bottom: 40, l:40};
var width= 580-margin.r-margin.l;
var height= 550-margin.top-margin.bottom;
var dead;
//scales
var xScale=d3.scaleLinear().domain([250,-250]).range([0,width])
var yScale= d3.scaleLinear().domain([-50,420]).range([height,0])
var svg;
createLoading()
d3.json('/curry',function(err,data){
if(err){
console.log('there was an error')
}else{
console.log(data);
svg=d3.select('#root').append('svg').attr('id','svgChart')
.attr('width',width+margin.r+margin.l)
.attr('height',height+margin.top+margin.bottom)
.append('g')
.attr('width',width)
.attr('height',height)
.attr('transform','translate('+margin.l+','+margin.top+')')
.append("image").attr('xlink:href',"https://thedatagame.files.wordpress.com/2016/03/nba_court.jpg")
.attr('width',width)
.attr('height',height);
//Hex Data Format 17:locX 18:locY 20:made
console.log(data);
hexData=hexDataFormat(data);
console.log('worked until here')
var hexbin = d3.hexbin()
.extent([[-margin.left, -margin.top], [width + margin.right, height + margin.bottom]])
.radius(10);
console.log(hexbin(hexData));
console.log('It Worked')
//['parameters']['PlayerId']
d3.select('g').append("g").attr('id','hexG')
.attr("clip-path", "url(#clip)")
.selectAll(".hexagon")
.data(hexbin(hexData).sort(function(a, b) { return b.length - a.length; }))
.enter().append("path")
.attr("class", "hexagon")
.attr("d", function(d){if(d.length>100){
return hexbin.hexagon(radius(d.length))}else{return hexbin.hexagon(d.length)}})
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; })
.style('fill',function(d){return colorScale((d3.mean(d,function(d){return +d.player;})
-d3.mean(d, function(d){return +d.league;})))})
;
//player name
//player id
//pic link
// (d3.mean(d,function(d){return +d.player;})-d3.mean(d, function(d){return +d.league;}))
//Player Image
d3.select('#pic').append('img').attr('src','http://stats.nba.com/media/players/230x185/'+data.parameters.PlayerID+'.png');
createTitle(data);
legendHex();
d3.select('#explainer').text('The NBA Shot Chart Creator allows you to dynamically create NBA shot charts in both a XY scatterplot style and a Hexagonal Binnig style. The data is provided from stats.nba.com. This site is not affiliated in anyway with the NBA. This site was created by me, @nathanIharris. I created the front end and the back end. Feel free to download charts and use them in any publication. (to download multiple charts refresh the site). I hope to add more features in the near future as time allows. I can be contacted at nharrisanalyst@gmail.com or at my twitter handle.')
removeLoading();
}
})
</script>
<script>
var x;
var playerData;
var nameMapId={};
var nameMapTeamId={};
var change=function(t){
console.log($('#check').is(':checked'))
};
var year=["2016-17","2015-16","2014-15","2013-14"];
///Original Chart
d3.json('/nba/data/players.json',function(err,data){
//maps for players to team id and to player id
data.forEach((player)=>{
nameMapId[player.lastName+", "+player.firstName]=player.playerId
})
data.forEach((player)=>{
nameMapTeamId[player.lastName+", "+player.firstName]=player.teamId
})
console.log(nameMapId);
console.log(nameMapTeamId);
playerData=data
//The Input Form for generating a new shot chart
d3.select('#form').append('form').attr('id','theForm')
var select1=d3.select('#form').append('select').attr('id','select1').attr('form','theForm');
select1.selectAll('option').data(playerData).enter()
.append('option').attr('value',function(d){
return d.lastName+", "+d.firstName;
})
.text(function(d){ return d.lastName+", "+d.firstName;})
var select2=d3.select('#form').append('select').attr('id','select2').attr('form','theForm');
select2.selectAll('option').data(year).enter()
.append('option').attr('value',function(d){
return d;
})
.text(function(d){ return d;});
var chartOption=d3.select('form').append('label').text('scatterplot').append('input').attr('type','checkbox').attr('value','XY').attr('id','check').attr("onClick", "change(this)");
d3.select('form').append('br')
var button = d3.select('form').append('button').text('Create Shot Chart').attr('onclick','shotChart(event)');
;});
//new Shot Chart onClick function
var shotChart=function(e){
console.log(e)
e.preventDefault()
createLoading()
var player = $("#select1").val();
var season=$('#select2').val();
var data={playerId:nameMapId[player],seasonId:season,pic:'http://stats.nba.com/media/players/230x185/'+nameMapId[player]+'.png'}
var url='/shotChartData';
//the XMLHTTPREQUEST listener function (this creates for different charts depending on the chartState and scatterplot state
function reqListener(){
var data=this.response;
data=JSON.parse(data);
//d3chart hexgonal update only updates hex chart no state change
if($('#check').is(':checked')==0&& chartState.chart=='hex'){
var hexData=hexDataFormat(data);
console.log('worked until here')
var hexbin = d3.hexbin()
.extent([[-margin.left, -margin.top], [width + margin.right, height + margin.bottom]])
.radius(10);
var hexUp=d3.select('#hexG').selectAll(".hexagon")
.data(hexbin(hexData).sort(function(a, b) { return b.length - a.length; }));
hexUp.attr("class", "hexagon")
hexUp.enter().append('path').merge(hexUp).transition().duration(5000)
.attr("class", "hexagon")
.attr("d", function(d){if(d.length>100){
return hexbin.hexagon(radius(d.length))}else{return hexbin.hexagon(d.length)}})
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; })
.style('fill',function(d){return colorScale((d3.mean(d,function(d){return +d.player;})
-d3.mean(d, function(d){return +d.league;})))});
// .merger(hexUp)
// .attr("d", function(d){ if(d.length>100){
// return hexbin.hexagon(radius(d.length))}else{return hexbin.hexagon(d.length)}})
// .attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; })
// .style('fill',function(d){return colorScale((d3.mean(d,function(d){return +d.player;})
// -d3.mean(d, function(d){return +d.league;})))});
hexUp.exit().remove();
d3.selectAll('img').remove();
d3.select('#pic').append('img').attr('src','http://stats.nba.com/media/players/230x185/'+data.parameters.PlayerID+'.png')
createTitle(data);
removeLoading();
return
}
//Updates XY Chart does not change Chart state
else if($('#check').is(':checked')==1&& chartState.chart=='XY'){
//d3 xy plot Shot Chart
var circle=d3.select('#svgChart').select('g').selectAll('circle').data(data.resultSets[0].rowSet)
circle.attr('class','update')
circle.enter().append('circle').merge(circle)
.transition().duration(5000)
.attr('cx',function(d){return xScale(d[17])})
.attr('cy',function(d){return yScale(d[18])})
.attr('r',3)
.attr('stroke','black')
.attr('fill',function(d){if(d[20]==1){ return 'black'}else{return 'none'}})
circle.on("mouseover",showToolTip)
.on('mouseout',removeToolTip)
circle.exit().transition().duration(5000)
.attr('cx',function(d){if(xScale(d[17])>width/2){return width}else{return 0}})
.remove();
d3.selectAll('img').remove();
d3.select('#pic').append('img').attr('src','http://stats.nba.com/media/players/230x185/'+data.parameters.PlayerID+'.png');
createTitle(data);
removeLoading();
return
}
//changes chart to an XY plot and changes chart state to 'XY'
else if($('#check').is(':checked')==1&& chartState.chart=='hex'){
chartState.chart='XY'
removeLegend();
console.log(data);
console.log('this');
d3.select('#svgChart').remove();
svg=d3.select('#root').append('svg').attr('id','svgChart')
.attr('width',width+margin.r+margin.l)
.attr('height',height+margin.top+margin.bottom)
.append('g')
.attr('width',width)
.attr('height',height)
.attr('transform','translate('+margin.l+','+margin.top+')')
.append("image").attr('xlink:href',"https://thedatagame.files.wordpress.com/2016/03/nba_court.jpg")
.attr('width',width).attr('height',height)
var circle=d3.select('g').selectAll("circle").data(data.resultSets[0].rowSet).enter()
.append('circle')
.attr('cx',function(d){return xScale(d[17])})
.attr('cy',function(d){return yScale(d[18])})
.attr('r',3)
.attr('stroke','black')
.attr('fill',function(d){if(d[20]==1){ return 'black'}else{return 'none'}})
.on("mouseover",showToolTip)
.on('mouseout',removeToolTip)
d3.selectAll('img').remove();
d3.select('#pic').append('img').attr('src','http://stats.nba.com/media/players/230x185/'+data.parameters.PlayerID+'.png');
createTitle(data);
legendXY();
removeLoading();
return
}
/// Chart is change to hex and chart state is updated
else if($('#check').is(':checked')==0&& chartState.chart=='XY'){
chartState.chart='hex'
removeLegend();
d3.select('#svgChart').remove();
svg=d3.select('#root').append('svg').attr('id','svgChart')
.attr('width',width+margin.r+margin.l)
.attr('height',height+margin.top+margin.bottom)
.append('g')
.attr('width',width)
.attr('height',height)
.attr('transform','translate('+margin.l+','+margin.top+')')
.append("image").attr('xlink:href',"https://thedatagame.files.wordpress.com/2016/03/nba_court.jpg")
.attr('width',width)
.attr('height',height);
//17:locX 18:locY 20:made
var hexData=hexDataFormat(data);
console.log('worked until here')
var hexbin = d3.hexbin()
.extent([[-margin.left, -margin.top], [width + margin.right, height + margin.bottom]])
.radius(10);
console.log(hexbin(hexData));
console.log('It Worked')
//['parameters']['PlayerId']
d3.select('g').append("g").attr('id','hexG')
.attr("clip-path", "url(#clip)")
.selectAll(".hexagon")
.data(hexbin(hexData).sort(function(a, b) { return b.length - a.length; }))
.enter().append("path")
.attr("class", "hexagon")
.attr("d", function(d){if(d.length>100){
return hexbin.hexagon(radius(d.length))}else{return hexbin.hexagon(d.length)}})
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; })
.style('fill',function(d){return colorScale((d3.mean(d,function(d){return +d.player;})
-d3.mean(d, function(d){return +d.league;})))})
d3.selectAll('img').remove();
d3.select('#pic').append('img').attr('src','http://stats.nba.com/media/players/230x185/'+data.parameters.PlayerID+'.png');
createTitle(data);
legendHex();
removeLoading();
return }
}
//XMLHttpRequest
var shotData = new XMLHttpRequest();
shotData.addEventListener("load", reqListener);
shotData.open('POST','/shotChartData');
shotData.send(JSON.stringify(data));
}
//tooltip for XY plots
//"SHOT_DISTANCE" d[16] "ACTION_TYPE" d[11] "GAME_DATE" d[21].slice(4,6)-d[21].slice(6,8)-d[21].slice(0,4)
</script>
<script >
var download= function(){
(function (){
var e = document.createElement('script');
if (window.location.protocol === 'https:') {
e.setAttribute('src', 'https://rawgit.com/NYTimes/svg-crowbar/gh-pages/svg-crowbar.js');
} else {
e.setAttribute('src', 'http://nytimes.github.com/svg-crowbar/svg-crowbar.js');
}
e.setAttribute('class', 'svg-crowbar');
document.body.appendChild(e);
})();
}
d3.select('.download').on('click',download);
var createTitle=function(data){
if(!($('#title').val()==undefined)) {d3.select('#title').remove();}
d3.select('svg').append('text').attr('id','title').attr('transform','translate(197,35)').attr('fontsize',20).text(data.resultSets[0].rowSet[0][4].toString())
}
var legendXY = function(){
var legendData=['Made Shot','Missed Shot']
var legend= d3.select('#legend').append('svg').attrs({
'id':'legendSVG',
'width':350,
'height':95})
legend.append('g').selectAll('circle').data(legendData).enter()
.append('circle').attrs({
cx:5,
cy:function(d,i){return 10+(i*15);},
r:3,
stroke:'black',
fill:function(d){if(d=='Made Shot'){return "black"}else{ return "none"}}
})
legend.selectAll('text').data(legendData).enter().append('text')
.attrs({
x:15,
y:function(d,i){return 15+(i*15);},
}).text(function(d){return d});
var legText=d3.select('#legendText').append('svg').attr('id','legendTextText')
.attr('width',350).attr('height',80)
legText.append('text').attrs({
x:10,
y:50
}).text('Move the mouse over individual shot attempts')
legText.append('text').attrs({
x:10,
y:65
}).text('to learn more about each shot.')
}
var removeLegend=function(){
d3.select('#legendSVG').remove();
d3.select('#legendTextText').remove();
}
//-.055, 0 , .055
var legendHex =function(){
var colorLegScale = d3.scaleLinear()
.domain([3, 10, 17])
.range(["blue", "yellow", "red"]);
var legendData=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]
var legend = d3.select('#legend').append('svg').attrs({
'id': 'legendSVG',
'width':350,
'height':95})
legend.append('g').selectAll('rect').data(legendData).enter()
.append('rect')
.attrs({
x:function(d,i){return (i*10)+20},
y:55,
'width':10,
'height':10,
'fill':function(d){return colorLegScale(d)}
})
d3.select('#legendSVG').append('line')
.attrs({
'x1':115,
'x2':115,
'y1':65,
'y2':75,
'stroke-width':1,
'stroke':'black'
})
d3.select('#legendSVG').append('line')
.attrs({
'x1':50,
'x2':50,
'y1':55,
'y2':45,
'stroke-width':1,
'stroke':'black'
})
//
d3.select('#legendSVG').append('line')
.attrs({
'x1':180,
'x2':180,
'y1':55,
'y2':45,
'stroke-width':1,
'stroke':'black'
})
d3.select('#legendSVG').append('text')
.attrs({
'x':150,
'y':40,
}).text('5.5% Above NBA Avg.')
d3.select('#legendSVG').append('text')
.attrs({
'x':85,
'y':84,
}).text('NBA Avg.')
d3.select('#legendSVG').append('text')
.attrs({
'x':0,
'y':40,
}).text('5.5% Below NBA Avg.')
d3.select('#legendSVG').append('text')
.attrs({
'x':90,
'y':20,
}).text('Efficiency')
var legText=d3.select('#legendText').append('svg').attr('id','legendTextText')
.attr('width',350).attr('height',80)
legText.append('text').attrs({
x:10,
y:50