-
Notifications
You must be signed in to change notification settings - Fork 29
/
mlb.html
361 lines (323 loc) · 11.1 KB
/
mlb.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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=7,IE=9" />
<meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no"/>
<title>Esri <3 d3js</title>
<link rel="stylesheet" href="http://js.arcgis.com/3.13/esri/css/esri.css" />
<link rel="stylesheet" href="css/bootstrap.css"></link>
<link rel="stylesheet" href="css/mlb.css"></link>
<script>
var dojoConfig = {
parseOnLoad: true,
packages: [{
"name": "modules",
"location": location.origin + location.pathname.replace(/\/[^/]+$/, '')
}]
};
</script>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://js.arcgis.com/3.13/"></script>
<script>
var ranks = {
"Astros": 30,
"Marlins": 29,
"Pirates": 28,
"Athletics": 27,
"Rays": 26,
"Padres": 25,
"Indians": 24,
"Mariners": 23,
"Brewers": 22,
"Rockies": 21,
"City Royals": 20,
"Twins": 19,
"Mets": 18,
"Braves": 17,
"Diamondbacks": 16,
"Orioles": 15,
"Cubs": 14,
"Reds": 13,
"Nationals": 12,
"Blue Jays": 11,
"Cardinals": 10,
"White Sox": 9,
"Rangers": 8,
"Giants": 7,
"Red Sox": 6,
"Tigers": 5,
"Angels": 4,
"Phillies": 3,
"Yankees": 2,
"Dodgers": 1
}
var payroll = d3.scale.quantile()
.domain([50000000, 200000000])
.range([5, 10, 15, 20, 25]);
var salary = d3.scale.quantile()
.domain([200000, 20000000])
.range([5, 10, 15, 20]);
var forces = {};
var map;
var layer;
require([
"esri/map",
"modules/d3Layer",
"dojo/parser",
"dojo/domReady!"
], function(
Map,
d3Layer,
parser) {
parser.parse();
map = new Map("map", {
center: [-98, 39],
zoom: 4,
basemap: "gray"
});
layer = new d3Layer('data/mlb_salaries.json', {
attrs: [{
key: 'r',
value: function(d) {
return payroll(d.properties.team.payroll);
}
}, {
key: 'id',
value: function(d) {
return d.properties.team.name.replace(/\s/g, '');
}
}, {
key: 'class',
value: 'park'
}],
type: 'circle',
map: map
});
map.addLayer(layer);
map.on("zoom-end", function() {
setTimeout(function() {
d3.selectAll('.park')[0].forEach(function(park) {
var root = d3.select('#' + park.id + '_root');
if (root[0][0]) {
root.data()[0].x = park.cx.baseVal.value;
root.data()[0].px = park.cx.baseVal.value;
root.data()[0].y = park.cy.baseVal.value;
root.data()[0].py = park.cy.baseVal.value;
forces[park.id].start();
}
});
}, 10);
});
layer.on("load", function(evt) {
d3.selectAll('.park')
.on('mouseover', function(dot) {
var img = '<img src="img/' + dot.properties.team.name.replace(/\s/g, '') + '.gif"></img>';
d3.select('#payroll').style('display', 'block');
d3.select('#salary').style('display', 'none');
d3.select('#intro').remove();
var num = dot.properties.team.payroll;
var str = getAmount(num.toString().length);
d3.select('#team-logo')
.html(img)
d3.select('#team-name')
.html('<h1>' + dot.properties.team.name + '</h1>');
d3.select('#team-payroll')
.html('$' + addCommas(dot.properties.team.payroll).split(',')[0] + ' ' + str);
d3.select('#team-payroll-rank')
.html(ranks[dot.properties.team.name])
})
.on('mouseout', function(dot) {
setTimeout(function() {
//d3.select('#team').style('display', 'none');
//d3.select('#payroll')
// .html('');
}, 3000);
});
//add commas
function addCommas(nStr) {
nStr += '';
x = nStr.split('.');
x1 = x[0];
x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
return x1 + x2;
}
//get dollar amount text
function getAmount(dollar) {
var amount;
switch (true) {
case (dollar <= 6):
amount = "thousand";
break;
case (dollar <= 9):
amount = "million"
break;
case (dollar > 9):
amount = "billion"
break;
}
return amount;
}
d3.selectAll('.park')
.on('click', function(dot) {
if (d3.select('#' + dot.properties.team.city.replace(' ', ''))[0][0]) {
d3.select('#' + dot.properties.team.city.replace(' ', '')).style('display', 'block');
} else {
var selected = d3.select(this)[0][0];
var force = d3.layout.force()
.on("tick", tick)
var vis = evt.layer._element().append("g")
.attr('class', 'hover-force')
.attr('id', dot.properties.team.city.replace(' ', ''));
var root = dot.properties;
root.fixed = true;
root.x = selected.cx.baseVal.value;
root.y = selected.cy.baseVal.value;
var nodes = flatten(root),
links = d3.layout.tree().links(nodes);
forces[dot.properties.team.name.replace(/\s/g, '')] = force
.gravity(.01)
.charge(-75)
.linkStrength(5)
.linkDistance(50)
.size([700, 700])
.nodes(nodes)
.links(links)
.start();
vis.selectAll("line.link")
.data(links, function(d) {
return d.target.id;
})
.enter().insert("svg:line", ".node")
.attr("class", "link")
.attr("x1", function(d) {
return d.source.x;
})
.attr("y1", function(d) {
return d.source.y;
})
.attr("x2", function(d) {
return d.target.x;
})
.attr("y2", function(d) {
return d.target.y;
});
vis.selectAll("circle.node")
.data(nodes, function(d) {
return d.id;
})
.enter().append("svg:circle")
.attr("class", function(d) {
return (d.children) ? "root node" : "node"
})
.attr("id", function(d) {
return (d.children) ? d.team.name.replace(/\s/g, '') + '_root' : null
})
.attr("cx", function(d) {
return d.x;
})
.attr("cy", function(d) {
return d.y;
})
.attr("r", function(d) {
return d.children ? payroll(d.team.payroll) : salary(d.salary)
})
.style("fill", color)
.call(force.drag)
.on('mousedown', function(d) {
if (d.team && d.team.city) d3.select('#' + d.team.city.replace(' ', '')).style('display', 'none');
})
.on('mouseover', function(dot) {
if (!dot.team) {
var num = dot.salary;
var str = getAmount(num.toString().length);
d3.select('#salary').style('display', 'block');
d3.select('#player-name')
.html('<h1>' + dot.name + '</h1>');
d3.select('#player-salary')
.html('$' + addCommas(dot.salary).split(',')[0] + ' ' + str);
}
});
}
function tick() {
vis.selectAll("line.link")
.attr("x1", function(d) {
return d.source.x;
})
.attr("y1", function(d) {
return d.source.y;
})
.attr("x2", function(d) {
return d.target.x;
})
.attr("y2", function(d) {
return d.target.y;
});
vis.selectAll("circle.node")
.attr("cx", function(d) {
return d.x;
})
.attr("cy", function(d) {
return d.y;
});
}
// MLB colors
function color(d) {
return d._children ? "#d50032" : d.children ? "#FFF" : "#d50032";
}
// Returns a list of all nodes under the root.
function flatten(root) {
var nodes = [],
i = 0;
function recurse(node) {
if (node.children) node.salary = node.children.reduce(function(p, v) {
return p + recurse(v);
}, 0);
if (!node.id) node.id = ++i;
nodes.push(node);
return node.salary;
}
root.salary = recurse(root);
return nodes;
}
});
});
});
</script>
</head>
<body>
<div id="map"></div>
<div id="team">
<div id="team-inner">
<div id="intro">
<span>
<span style="color: #d50032">MLB</span> payrolls exceeded <br /><span class="number">3 billion</span> dollars in 2013.
</span>
</div>
<div id="payroll" class="info">
<div id="team-logo"></div>
<div id="team-name"></div>
<div id="payroll-container">
<div id="payroll-titles">
<span id="payroll-title">Payroll</span>
<span id="rank-title">Rank</span>
</div>
<div id="team-payroll"></div>
<div id="team-payroll-rank"></div>
</div>
</div>
<div id="salary" class="info">
<div id="player-name"></div>
<div id="salary-titles">
<span id="salary-title">Salary</span>
</div>
<div id="player-salary"></div>
</div>
</div>
</div>
</body>
</html>