-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
210 lines (177 loc) · 6.46 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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Shot Chart</title>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script src="svgToPng.js" charset="utf-8"></script>
</head>
<body>
<div id="shot_chart"></div>
<div id="svg_container"></div>
<div id="png_container"></div>
<div>
<button id="btn_save">save</button>
</div>
</body>
<script type="text/javascript">
var fgm = [0, 2, 2, 1.6, 2, 2, 4, 8, 2, 2, 11, 3, 1, 7];
var fga = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14];
var width = 652,
height = 613;
var xScale = d3.scale.linear()
.domain([0, 652])
.range([0, width]);
var yScale = d3.scale.linear()
.domain([0, 613])
.range([0, height]);
var fontSize = Math.min(xScale(16), yScale(16));
function percRed(i) {
if(i >= 4 && i <= 8) // long two
return 0.4;
else if(i >= 1 && i <= 3) // mid two
return 0.5;
else if(i >= 9 && i <= 13) // three
return 0.35;
else // under the hoop
return 0.6;
}
function fillZoneColor(i) {
var perc = fgm[i]/fga[i];
var percR = percRed(i);
var percY = percR - 0.1;
if(perc < percY) // blue
return "#88b8f6";
else if (perc < percR) // yellow
return "#eadead";
else // red
return "#e5786d";
}
var svg = d3.select("#shot_chart").append("svg")
.attr("id", "shot_chart_svg")
.attr("width", width)
.attr("height", height);
var image = svg.append("svg:image")
.attr("xlink:href", "shotchart_background_zone_652.png")
.attr("width", width)
.attr("height", height);
var pavement = d3.svg.line()
.interpolate("linear")
.x(function(d) {
return xScale(d[0]);
})
.y(function(d) {
return yScale(d[1]);
})
d3.json("shot_chart_zone.json", function(error, jsonData) {
var color1 = d3.scale.category10();
svg.selectAll("g")
.data(jsonData.features)
.enter()
.append("g")
.attr("fill-opacity", 0.5)
.on("mouseover", function() {
console.log(this);
d3.select(this).attr("fill-opacity", 0.8);
})
.on("mouseout", function() {
console.log(this);
d3.select(this).attr("fill-opacity", 0.5);
});
svg.selectAll("g")
.append("path")
.attr("d", function(d, i) {
return pavement(d.geometry.coordinates);
})
.attr("fill", function(d, i) {
//return color1(i);
return fillZoneColor(i);
});
svg.selectAll("g")
.append("rect")
.attr("x", function(d) {
return (xScale(d.geometry.center[0]-40));
})
.attr("y", function(d) {
return (yScale(d.geometry.center[1]-25));
})
.attr("rx", function() {
return xScale(10);
})
.attr("ry", function() {
return yScale(50);
})
.attr("width", function() {
return xScale(80);
})
.attr("height", function() {
return yScale(50);
})
.attr("fill", "black")
.attr("style", "stroke: grey; stroke-width: 1px");
svg.selectAll("g")
.append("text")
.attr("id", function(d) {
return d.properties.zone_name;
})
.attr("class", "fg")
.attr("x", function(d) {
return xScale(d.geometry.center[0]);
})
.attr("y", function(d) {
return yScale(d.geometry.center[1]);
})
.attr("style", "font:" + fontSize + "px sans-serif;")
.attr("fill", "white")
.attr("fill-opacity", 1.0)
.attr("text-anchor", "middle");
svg.selectAll("text")
.append("tspan")
.attr("x", function(d) {
return xScale(d.geometry.center[0]);
})
.attr("dy", "-0.2em")
.text( function(d, i) {
return ( fgm[i] + ' / ' + fga[i] );
});
svg.selectAll("text")
.append("tspan")
.attr("x", function(d) {
return xScale(d.geometry.center[0]);
})
.attr("dy", "1em")
.text( function(d, i) {
return ( (100*fgm[i]/fga[i]).toFixed(1) + '%' );
});
});
d3.select("#btn_save").on("click", function() {
// inline <image> in <svg> first
inlineImages( function() {
var parentNode = document.createElement("div");
var cloneNode = document.getElementById("shot_chart_svg")
.cloneNode(true);
var xmlns = "http://www.w3.org/2000/xmlns/";
cloneNode.setAttribute("version", "1.1");
cloneNode.setAttributeNS(xmlns, "xmlns", "http://www.w3.org/2000/svg");
cloneNode.setAttributeNS(xmlns, "xmlns:xlink", "http://www.w3.org/1999/xlink");
var css = styles(cloneNode);
if(css != null)
cloneNode.insertBefore(css, cloneNode.firstChild);
parentNode.appendChild(cloneNode)
var html = parentNode.innerHTML;
var svgEncode = window.btoa(unescape(encodeURIComponent(html)));
var svgSrc = 'data:image/svg+xml;base64,'+ svgEncode;
var svgImg = '<img src="'+svgSrc+'">';
d3.select("#svg_container").html(svgImg);
svgSrcToPngSrc(svgSrc, function(pngSrc) {
var pngImg = '<img src="'+pngSrc+'">';
d3.select("#png_container").html(pngImg);
var a = document.createElement('a');
a.download = "shot_chart.png";
a.href = pngSrc;
a.click();
});
});
});
</script>
</html>