-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrpm-table.js
More file actions
196 lines (173 loc) · 8.39 KB
/
rpm-table.js
File metadata and controls
196 lines (173 loc) · 8.39 KB
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
// Global variable to store dataset (reccomended from Scott Murray's book)
var dataset;
// Converts the data from a .csv to an array object with the correc types
var row_converter = function(d) {
rounding_scale = 1000;
return {
Player: d.name,
Teams: d.teams,
Minutes: parseFloat(d.minutes),
FIFA: parseFloat(d.fifa),
APM: Math.round(parseFloat(d.APM) * rounding_scale) / rounding_scale,
Augmented_APM: Math.round(parseFloat(d.AugAPM) * rounding_scale) / rounding_scale,
Augmented_APM_SE: Math.round(parseFloat(d.AugAPMSE) * rounding_scale) / rounding_scale
};
}
// Primary funtion to convever .csv file into a sortable table
var create_rpm_table = function(filepath) {
d3.csv(filepath + "?q" + Math.random(), row_converter, function(error, data) {
dataset = data; // Store dataset in global variable
// Print out the data-set constructed by d3.csv()
if (error) {
console.log(error);
} else {
console.log(data);
}
// Adds and empty table to webpage
var table = d3.select('#page-wrap')
.append("table");
// Add table headers to the empty table (<thead>)
var column_titles = d3.keys(data[0]);
column_titles.pop() // Remove the Standard Error as a column title
var headers = table.append("thead")
.append("tr")
.selectAll("th")
.data(column_titles)
.enter()
.append("th")
.text(function (d) {
if (d == "Teams") { return "Team(s)" }
if (d == "Augmented_APM") { return "Augmented APM" }
return d;
})
// Sort the table column by click
sort_ascend = true;
headers.on('click', function(h) {
console.log("Sorting by: ", h);
if (sort_ascend) {
rows.sort( function(a, b) { return d3.ascending(a[h], b[h]); });
sort_ascend = false;
} else {
rows.sort( function(a, b) { return d3.descending(a[h], b[h]); });
sort_ascend = true;
}
})
// Add rows the the table (<tr> elements) to <tbody>
var rows = table.append('tbody')
.selectAll('tr')
.data(data)
.enter()
.append('tr')
// Add the cell values to each table
rows.attr("class", "data-tr")
.selectAll('td')
.data(function (d) {
return column_titles.map(function (k) { // extracts the name and value
return { 'value': d[k], 'name': k};
});
})
.enter()
.append('td') // Add the cells to each table row
.attr('data-th', function (d) { // Adds "data-th" attribute to each cell
return d.name;
})
.text(function (d) { // Adds value to each cell
return d.value;
});
// Add a chart variable to the table
var chartWidth = "100px",
percent = d3.format(".2%");
// Create the SVG element where we place our shapes
var svg = d3.select("body").append("svg")
// Add an SVG placeholder element for the location of the chart
var svg_chart_width = 100;
var svg_chart_height = 20;
var chart_location = d3.select("tbody")
.selectAll("tr")
.append("td")
.attr("class", "chart")
.append("svg")
.attr("width", svg_chart_width)
.attr("height", svg_chart_height)
.attr("class", "player-chart")
// Create a scale that maps between APM and the SVG range
var apm_max = d3.max(data, function(d) { return d.Augmented_APM; });
var apm_min = d3.min(data, function(d) { return d.Augmented_APM; });
var apm_max_se = d3.max(data, function(d) { return d.Augmented_APM_SE });
var chart_scale = d3.scaleLinear()
.domain([apm_min - (2 * apm_max_se), apm_max + (2 * apm_max_se)])
.range([0, svg_chart_width]);
var color_scale = d3.scaleLinear()
.domain([apm_min, apm_max])
.range(["blue", "red"])
// Add the circles to the chart location
var sd_line = chart_location.append("line")
.attr("x1", function(d) {
return chart_scale(d.Augmented_APM - (2 * d.Augmented_APM_SE))
})
.attr("x2", function(d) {
return chart_scale(d.Augmented_APM + (2 * d.Augmented_APM_SE))
})
.attr("y1", svg_chart_height / 2)
.attr("y2", svg_chart_height / 2)
.attr("stroke", function(d) {
return color_scale(d.APM);
})
// Add lines for the standard errors
var line_upper = chart_location.append("line")
.attr("x1", function(d) {
return chart_scale(d.Augmented_APM + (2 * d.Augmented_APM_SE))
})
.attr("x2", function(d) {
return chart_scale(d.Augmented_APM + (2 * d.Augmented_APM_SE))
})
.attr("y1", 1 * (svg_chart_height / 4))
.attr("y2", 3 * (svg_chart_height / 4))
.attr("stroke", function(d) {
return color_scale(d.Augmented_APM);
})
var line_lower = chart_location.append("line")
.attr("x1", function(d) {
return chart_scale(d.Augmented_APM - (2 * d.Augmented_APM_SE))
})
.attr("x2", function(d) {
return chart_scale(d.Augmented_APM - (2 * d.Augmented_APM_SE))
})
.attr("y1", 1 * (svg_chart_height / 4))
.attr("y2", 3 * (svg_chart_height / 4))
.attr("stroke", function(d) {
return color_scale(d.Augmented_APM);
})
var circles = chart_location.append("circle")
.attr("cx", function(d) {
return chart_scale(d.Augmented_APM)
})
.attr("cy", svg_chart_height / 2)
.attr("r", 5)
.attr("fill", function(d) {
return color_scale(d.Augmented_APM);
})
});
}
// Add a sleep function so that the new click works on a MAC
function sleep(seconds){
var waitUntil = new Date().getTime() + seconds * 1000;
while(new Date().getTime() < waitUntil) true;
}
// Updates the table with the new .csv
var update_csv = function() {
league_val = document.getElementById("league").value;
season_val = document.getElementById("season").value;
filepath = "data/" + league_val + "_" + season_val + ".csv"
// Remove the previous table ---
d3.selectAll("table").remove();
sleep(.01);
create_rpm_table(filepath);
}
// Initialize the table w/ the 2017 EPL results
create_rpm_table("data/epl_2017.csv?v=1.1")
// When selected changes, call the update .csv function
d3.selectAll('.selector')
.on('change', function() {
update_csv();
})