-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathtransforms.js
186 lines (170 loc) · 4.75 KB
/
transforms.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
const Transform = require("../../src/Transform").Transform;
const identityFunction = require("../../src/Transform").identityFunction;
var teamLogoTransform = new Transform(
"select * from teams;",
"nba",
function(row) {
var id = parseInt(row[0]);
var y = Math.floor(id / 6);
var x = id - y * 6;
var ret = [];
ret.push(row[0]);
ret.push((x * 2 + 1) * 80);
ret.push((y * 2 + 1) * 80 + 100);
ret.push(row[1]);
ret.push(row[2]);
ret.push(row[3]);
ret.push(row[4]);
return Java.to(ret, "java.lang.String[]");
},
["id", "x", "y", "team_id", "city", "name", "abbr"],
true
);
var teamTimelineTransform = new Transform(
"select game_id, year, month, day, home_team, away_team, home_score, away_score, 1 from games;",
"nba",
function(row, width, height, renderParams) {
var ret = [];
// id
ret.push(row[0]);
// x
var curDate = new Date(row[1], row[2] - 1, row[3]);
ret.push(
d3
.scaleTime()
.domain([new Date(2017, 9, 17), new Date(2018, 3, 11)])
.range([82, width - 82])(curDate)
);
// y
var beginDate = new Date(2000, 0, 1);
var oneDay = 24 * 60 * 60 * 1000;
var daysPassed = Math.round(
Math.abs((curDate.getTime() - beginDate.getTime()) / oneDay)
);
ret.push(
daysPassed % 2 == 0
? renderParams.timelineUpperY
: renderParams.timelineLowerY
);
// rest of the attributes
for (var i = 1; i <= 8; i++) ret.push(row[i]);
return Java.to(ret, "java.lang.String[]");
},
[
"game_id",
"x",
"y",
"year",
"month",
"day",
"home_team",
"away_team",
"home_score",
"away_score",
"timeline"
],
true,
// uncomment below to enable updates on the timeline canvas
{
cx: function(oldRow, width, height) {
let newRow = oldRow;
let x = newRow["cx"];
let reverseDate = d3
.scaleLinear()
.domain([82, width - 82])
.range([new Date(2017, 9, 17), new Date(2018, 3, 11)])(x);
let y = newRow["cy"];
if (Math.abs(y - 510) > Math.abs(y - 740)) y = 740;
else y = 510;
newRow["cy"] = y;
let month = reverseDate.getUTCMonth() + 1;
let day = reverseDate.getUTCDate();
let year = reverseDate.getUTCFullYear();
newRow["month"] = month;
newRow["day"] = day;
newRow["year"] = year;
return newRow;
},
cy: identityFunction,
year: identityFunction,
month: identityFunction,
day: identityFunction,
home_score: identityFunction,
away_score: identityFunction
}
);
var teamTimelineStaticTransform = new Transform(
"select city, name, abbr from teams;",
"nba",
"",
[],
true
);
var playByPlayTransform = new Transform(
"select games.game_id, period, qtr_time, score, margin, home_desc, away_desc, home_team, away_team, play_id, h_player_id, a_player_id" +
" from plays, games" +
" where plays.game_id = games.game_id;",
"nba",
function(row) {
var ret = [];
// game_id
ret.push(row[0]);
// y
ret.push((+row[9] + 1) * 160);
// period & qtr_time
ret.push(row[1]);
ret.push(row[2]);
// reverse score
if (row[3] == "None") ret.push("");
else {
var scores = row[3].split("-");
ret.push(
scores[1].replace(/\s+/, "") +
" - " +
scores[0].replace(/\s+/, "")
);
}
// rest of the attributes
for (var i = 4; i <= 8; i++) ret.push(row[i]);
ret.push(row[10]);
ret.push(row[11]);
return Java.to(ret, "java.lang.String[]");
},
[
"game_id",
"y",
"period",
"qtr_time",
"score",
"margin",
"home_desc",
"away_desc",
"home_team",
"away_team",
"h_player_id",
"a_player_id"
],
true
);
var playByPlayStaticTransform = new Transform(
"select team1.abbr as abbr1, team2.abbr as abbr2 from teams as team1, teams as team2;",
"nba",
"",
[],
true
);
var boxscoreTransform = new Transform(
"select * from player_boxscore;",
"nba",
"",
[],
true
);
module.exports = {
teamLogoTransform,
teamTimelineTransform,
teamTimelineStaticTransform,
playByPlayTransform,
playByPlayStaticTransform,
boxscoreTransform
};