-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
306 lines (250 loc) · 9.09 KB
/
index.js
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
(function main() {
// Types of statistics that are suitable for graphing
var STAT_TYPES = [
'mp', 'fg', 'fga', 'fg%', '3p', '3pa', '3p%', 'ft', 'fta', 'ft%','orb',
'drb', 'trb', 'ast', 'stl', 'blk', 'tov', 'pf', 'pts', 'gmsc', 'ts%',
'efg%', 'orb%', 'drb%', 'trb%', 'ast%', 'stl%', 'blk%', 'tov%','usg%',
'ortg', 'drtg'
]
var INFO_TYPES = ['opp', 'date']
// Helper for use in event bindings
var bind = function(func, context) {
return Function.prototype.bind.apply(func, [].slice.call(arguments, 1))
}
var format = d3.time.format('%Y-%m-%d')
// Build or show the chart
//
// container - The container holding the table
// table - The statistics table
function showChart(container, table) {
var width = table.node().offsetWidth,
height = 400
if(table.node().querySelectorAll('tbody tr').length > 60)
width = 1200
table.style('display', 'none')
if(container.select('div.graph').node() == null) {
var data = toData(table),
player = d3.select('#info_box p.margin_top span.bold_text').text().split(' '),
name = player[0] + " " + player[player.length - 1],
stats = d3.keys(data[0]).filter(function(d) { return d != 'info' }),
padt = 30, padr = 10, padb = 70, padl = 20,
stat = stats.indexOf('pts') != -1 ? 'pts' : 'ortg',
curData = filterStat(stat, data),
x = d3.scale.ordinal().rangeRoundBands([0, width], 0.2),
y = d3.scale.linear().range([height, 0]),
xAxis = d3.svg.axis().scale(x).tickSize(8).tickFormat(function(i) {
return d3.time.format('%m/%d')(curData[i][1].date) + ' ' + curData[i][1].opp
}),
yAxis = d3.svg.axis().scale(y).orient("left").tickSize(-width + padl + padr)
var path = d3.svg.line()
.x(function(d, i) { return x(i) + x.rangeBand() / 2 })
.y(function(d) { return y(d) })
var div = container.append('div')
.attr('class', 'graph')
.style('width', width + padl + padr + 'px')
.style('border', '1px solid #ccc')
.style('padding', '0 0 0 10px')
var h3 = div.append('h3')
.style('padding', '10px')
.style('border-bottom', '1px solid #ccc')
.style('margin', 0)
h3.append('span')
.attr('class', 'player')
.text(name + ": ")
var subject = h3.append('span')
.attr('class', 'subject')
var select = h3.append('select')
.style('float', 'right')
select.selectAll('option')
.data(stats)
.enter().append('option')
.text(function(d) { return d })
.attr('selected', function(d) { return d == stat ? 'selected' : null })
var vis = div.append('svg')
.attr('class', 'bbref-chart')
.attr('width', width + padl + padr)
.attr('height', height + padt + padb)
.append('g')
.attr('transform', 'translate(' + padl + ',' + padt + ')')
vis.append("g")
.attr("class", "y axis")
vis.append('g')
.attr('class', 'x axis')
.attr('transform', 'translate(0,' + height + ')')
vis.append('path')
.attr('class', 'average')
function render(entries, curstat) {
var max = d3.max(entries, function(d) { return d[0] }),
averages = rollingAverageForStat(entries)
x.domain(d3.range(entries.length))
y.domain([0, max * 1.1])
vis.select('.y.axis').call(yAxis)
vis.select('.x.axis').call(xAxis)
vis.selectAll('.x.axis text')
.attr('transform', 'translate(' + -((x.rangeBand() / 2) + 10) + ',30), rotate(-65)')
.attr('text-anchor', 'end')
subject.text(stat.toUpperCase())
var bargroups = vis.selectAll('g.bar')
.data(entries)
var g = bargroups.enter().append('g')
.attr('class', 'bar')
.attr('transform', function(d, i) { return 'translate(' + x(i) + ',0)'})
rect = g.append('rect')
.attr('width', x.rangeBand())
g.append('text')
.attr('class', 'barlabel')
.attr('text-anchor', 'middle')
.attr('x', x.rangeBand() / 2)
bargroups.select('rect')
.attr('height', function(d) { return height - y(d[0])})
.attr('y', function(d) { return y(d[0]) })
bargroups.select('text')
.text(function(d) { return d[0] })
.style('display', function(d) {
if(isNaN(d[0])) return 'none'
})
if(entries.length > 40) {
bargroups.select('text')
.attr('transform', 'rotate(-90)')
.attr('text-anchor', 'start')
.attr('x', function(d) { return -y(d[0]) + 5 })
.attr('y', (x.rangeBand() + 2) / 1.5 )
//.attr('x', function(d) { return y(d[0]) - 5 })
// g.select('text.barlabel')
// .attr('transform', 'translate(')
} else {
bargroups.select('text')
.attr('y', function(d) { return y(d[0]) - 5 })
}
vis.select('path.average')
.attr('d', path(averages))
bargroups.exit().remove()
}
render(curData, stat)
select.on('change', function(event) {
stat = this.options[this.selectedIndex].text
curData = filterStat(stat, data)
render(curData, stat)
})
} else {
container.select('div.graph')
.style('display', 'block')
}
}
function filterStat(stat, data) {
return data.map(function(d) {
return [d[stat], d.info]
}).filter(function(d) { return d[0] != undefined && !isNaN(d[0]) })
}
// Hide the chart
//
// container - The container for the table
// table - The table holding the statistics
function hideChart(container, table) {
table.style('display', 'table')
container.select('div.graph').style('display', 'none')
}
// Chart link was clicked
//
// link - the anchor element that triggered the event
// event - the event object
function onChartLinkClick(link, event) {
event.preventDefault()
var container = d3.select(this.parentNode),
table = container.select('.stats_table')
if(table.style('display') == 'none') {
hideChart.apply(this, [container, table, event])
link.innerText = 'Chart'
} else {
showChart.apply(this, [container, table, event])
link.innerText = 'Table'
}
}
function minutesToDecimal(val) {
var mp = val.split(':').map(Number)
return parseFloat(d3.format('.2f')(((mp[0] * 60) + mp[1]) / 60))
}
function percentageToNumber(val) {
if(val == '') {
val = NaN
} else {
if((/^\./).test(val) || parseFloat(val) == 1) {
val = (parseFloat(val) * 100).toFixed(1)
} else {
val = parseFloat(val)
}
}
return val
}
function rollingAverageForStat(entries) {
var values = entries.map(function(d) { return d[0] }),
averages = [],
total = 0.0
values.forEach(function(val, idx) {
total += parseFloat(val)
averages.push(parseFloat(d3.format('.1f')(total / (idx + 1))))
})
return averages
}
// Build an array of objects from an HTML table
//
// table - d3 selection
//
// Returns an array or games [{game}, {game}, ...]
function toData(table) {
var dateidx = null,
headers = table.selectAll('thead th'),
rows = table.selectAll('tbody tr'),
data = []
headers.each(function(el, idx) {
if(this.innerText.toLowerCase() == 'date')
dateidx = idx + 1
})
// We want to have dates before we decide to continue
if(!dateidx) {
return console.log('No Date found in table')
}
// Get the stat labels
var labels = headers.map(function(hdrs) {
return hdrs.map(function(hdr) { return hdr.innerText.toLowerCase() })
})[0]
rows.each(function() {
var obj = {}
d3.select(this).selectAll('td').each(function(cell, idx) {
var label = labels[idx],
val = this.innerText
if(STAT_TYPES.indexOf(label) != -1) {
// convert minutes played to decimal
if(label == 'mp') {
val = minutesToDecimal(val)
// Convert percentage decimals to integers
} else if(label.indexOf('%') != -1) {
val = percentageToNumber(val)
// Floats strings to floats and number strings to numbers
} else {
val = val.indexOf('.') == -1 ? ~~val : parseFloat(val)
}
obj[label] = val
} else if(INFO_TYPES.indexOf(label) != -1) {
if(typeof obj.info == "undefined") obj.info = {}
if(label == 'date') val = format.parse(val)
obj.info[label] = val
}
})
data.push(obj)
})
return data
}
var headings = document.querySelectorAll('#basic_div .table_heading, #advanced_div .table_heading'),
i = 0,
len = headings.length
for(i; i < len; i++) {
var heading = headings[i]
a = document.createElement('a')
a.innerText = 'Chart'
a.href = '#chart'
a.className = 'bbref-chart-link'
a.addEventListener('click', bind(onChartLinkClick, heading, a))
heading.appendChild(a)
}
})()