Skip to content

Instantly share code, notes, and snippets.

@mrphilroth
Last active December 21, 2015 02:49
Show Gist options
  • Save mrphilroth/6238016 to your computer and use it in GitHub Desktop.
Save mrphilroth/6238016 to your computer and use it in GitHub Desktop.
Baseball Payroll Prototype

Here I show the total payroll of each baseball team from 1999 to the present. Use the arrow keys to change the year. Future years show player contracts that the team is obligated to pay.

TODO: Color the teams bars with team colors Sort the total payroll from low to high. Then get the bars to move around when changing years. Ghost bars in future years equal to average recent payroll.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Baseball Team Payrolls</title>
<script src="http://d3js.org/d3.v3.js"></script>
<style>
svg {
font: 10px sans-serif;
}
.y.axis path {
display: none;
}
.y.axis line {
stroke: #fff;
stroke-opacity: .2;
shape-rendering: crispEdges;
}
.y.axis .zero line {
stroke: #000;
stroke-opacity: 1;
}
.title {
font: 300 46px Helvetica Neue;
fill: #666;
}
.team {
text-anchor: middle;
}
.team {
fill: #fff;
}
rect {
fill-opacity: 1;
fill: #e377c2;
}
rect:first-child {
fill: #1f77b4;
}
</style>
</head>
<body>
<script>
var margin = {top: 20, right: 40, bottom: 30, left: 20},
width = 960 - margin.left - margin.right,
height = 400 - margin.top - margin.bottom,
barWidth = Math.floor(width / 30) - 1;
var x = d3.scale.ordinal()
.rangeBands([barWidth / 2, width - barWidth / 2], 1, 0);
var y = d3.scale.linear()
.range([height, 0]);
var yAxis = d3.svg.axis()
.scale(y)
.orient("right")
.tickSize(-width)
.tickFormat(function(d) { return Math.round(d / 1e6) + "M"; });
// An SVG element with a bottom-right origin.
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// A sliding container to hold the bars by team
var teams = svg.append("g")
.attr("class", "teams");
// A label for the current year.
var title = svg.append("text")
.attr("class", "title")
.attr("dy", ".71em")
.text(2013);
d3.csv("salary_total.csv", type, function(error, data) {
// Compute the extent of the data set in years.
var year0 = d3.min(data, function(d) { return d.year; }),
year1 = d3.max(data, function(d) { return d.year; }),
year = 2013;
// A list of the team names
var allteams = d3.set(data.map(function(d) { return d.team; })).values();
// Update the scale domains.
x.domain(data.map(function(d) { return d.team; }));
y.domain([0, d3.max(data, function(d) { return d.payroll; })]);
// Add an axis to show the payroll values.
svg.append("g")
.attr("class", "y axis")
.attr("transform", "translate(" + width + ",0)")
.call(yAxis)
.selectAll("g")
.filter(function(value) { return !value; })
.classed("zero", true)
// Produce a map from year and team to payroll
data = d3.nest()
.key(function(d) { return d.year; })
.key(function(d) { return d.team; })
.rollup(function(v) { return v.map(function(d) { return d.payroll; }); })
.map(data);
// Add labeled rects for each team
var team = teams.selectAll(".team")
.data(allteams)
.enter().append("g")
.attr("class", "team")
.attr("transform", function(team) { return "translate(" + x(team) + ",0)"; });
team.selectAll("rect")
.data(function(team) { return data[year][team] || 0; })
.enter().append("rect")
.attr("x", -barWidth / 2)
.attr("width", barWidth)
.attr("y", y)
.attr("height", function(value) { return height - y(value); });
// Add labels to show the team
team.append("text")
.attr("y", height - 4)
.text(function(team) { return team; });
// Allow the arrow keys to change the displayed year.
window.focus();
d3.select(window).on("keydown", function() {
switch (d3.event.keyCode) {
case 37: year = Math.max(year0, year - 1); break;
case 39: year = Math.min(year1, year + 1); break;
}
update();
});
function update() {
if (!(year in data)) return;
title.text(year);
// teams.transition()
// .duration(750)
// .attr("transform", "translate(" + (x(year1) - x(year)) + ",0)");
team.selectAll("rect")
.data(function(team) { return data[year][team] || 0; })
.transition()
.duration(750)
.attr("y", y)
.attr("height", function(value) { return height - y(value); });
}
});
function type(d) {
d.year = +d.year;
d.payroll = +d.payroll;
return d;
}
</script>
</body>
</html>
team year payroll
ARI 1999 68303999
ARI 2000 80812833
ARI 2001 76855499
ARI 2002 102819999
ARI 2003 80657000
ARI 2004 69450750
ARI 2005 62011166
ARI 2006 59984226
ARI 2007 52067546
ARI 2008 66202712
ARI 2009 75920666
ARI 2010 52868166
ARI 2011 53639833
ARI 2012 67069833
ARI 2013 87475000
ARI 2014 89300000
ARI 2015 68600000
ARI 2016 65375000
ARI 2017 36375000
ARI 2018 11100000
ARI 2019 14500000
ARI 2020 0
ARI 2021 0
ARI 2022 0
ARI 2023 0
ARI 2024 0
ATL 1999 64836000
ATL 2000 75817836
ATL 2001 91291666
ATL 2002 90960367
ATL 2003 103243667
ATL 2004 90182500
ATL 2005 86457302
ATL 2006 74967333
ATL 2007 71655833
ATL 2008 101975683
ATL 2009 97788166
ATL 2010 84021166
ATL 2011 87188132
ATL 2012 85728000
ATL 2013 86013750
ATL 2014 43800000
ATL 2015 41950000
ATL 2016 15450000
ATL 2017 16450000
ATL 2018 0
ATL 2019 0
ATL 2020 0
ATL 2021 0
ATL 2022 0
ATL 2023 0
ATL 2024 0
BAL 1999 80805863
BAL 2000 82147435
BAL 2001 55345500
BAL 2002 47704697
BAL 2003 53547500
BAL 2004 43623333
BAL 2005 73275833
BAL 2006 71646082
BAL 2007 85650252
BAL 2008 59804580
BAL 2009 69504166
BAL 2010 81612500
BAL 2011 84604038
BAL 2012 75904000
BAL 2013 80600000
BAL 2014 49717000
BAL 2015 40745000
BAL 2016 16000000
BAL 2017 16000000
BAL 2018 17000000
BAL 2019 0
BAL 2020 0
BAL 2021 0
BAL 2022 0
BAL 2023 0
BAL 2024 0
BOS 1999 63897500
BOS 2000 71312833
BOS 2001 109833333
BOS 2002 107311060
BOS 2003 99646500
BOS 2004 126694000
BOS 2005 123505125
BOS 2006 119374824
BOS 2007 133526214
BOS 2008 125390035
BOS 2009 125009499
BOS 2010 161347333
BOS 2011 166662475
BOS 2012 89136000
BOS 2013 107381500
BOS 2014 118125000
BOS 2015 57000000
BOS 2016 26000000
BOS 2017 28500000
BOS 2018 16000000
BOS 2019 15000000
BOS 2020 13000000
BOS 2021 12000000
BOS 2022 0
BOS 2023 0
BOS 2024 0
CHC 1999 61388000
CHC 2000 60069333
CHC 2001 64715833
CHC 2002 75487333
CHC 2003 79258333
CHC 2004 90560000
CHC 2005 87032933
CHC 2006 90391166
CHC 2007 99287332
CHC 2008 120345833
CHC 2009 139652000
CHC 2010 145784002
CHC 2011 125047329
CHC 2012 79937700
CHC 2013 94103000
CHC 2014 35250000
CHC 2015 29500000
CHC 2016 25000000
CHC 2017 16000000
CHC 2018 17000000
CHC 2019 22000000
CHC 2020 30500000
CHC 2021 14500000
CHC 2022 0
CHC 2023 0
CHC 2024 0
CHW 1999 24970000
CHW 2000 31308500
CHW 2001 63703667
CHW 2002 57052833
CHW 2003 51010000
CHW 2004 65212500
CHW 2005 74860000
CHW 2006 102750667
CHW 2007 108671833
CHW 2008 120796832
CHW 2009 100081000
CHW 2010 107195000
CHW 2011 127789000
CHW 2012 118208000
CHW 2013 116063500
CHW 2014 50250000
CHW 2015 34750000
CHW 2016 33400000
CHW 2017 12000000
CHW 2018 12500000
CHW 2019 13500000
CHW 2020 0
CHW 2021 0
CHW 2022 0
CHW 2023 0
CHW 2024 0
CIN 1999 33550761
CIN 2000 46567200
CIN 2001 48776000
CIN 2002 44269890
CIN 2003 58965667
CIN 2004 46315250
CIN 2005 61552583
CIN 2006 56819519
CIN 2007 68142480
CIN 2008 69100195
CIN 2009 78159000
CIN 2010 71455542
CIN 2011 76547134
CIN 2012 70579500
CIN 2013 97010000
CIN 2014 76600000
CIN 2015 81500000
CIN 2016 54500000
CIN 2017 49000000
CIN 2018 25000000
CIN 2019 25000000
CIN 2020 25000000
CIN 2021 25000000
CIN 2022 25000000
CIN 2023 25000000
CIN 2024 20000000
CLE 1999 73479962
CLE 2000 76767271
CLE 2001 92527001
CLE 2002 76903949
CLE 2003 39984834
CLE 2004 33700800
CLE 2005 41186000
CLE 2006 56031500
CLE 2007 60902167
CLE 2008 78970066
CLE 2009 75224866
CLE 2010 60778966
CLE 2011 48776566
CLE 2012 70672800
CLE 2013 60770333
CLE 2014 64933333
CLE 2015 40500000
CLE 2016 40250000
CLE 2017 38000000
CLE 2018 0
CLE 2019 0
CLE 2020 0
CLE 2021 0
CLE 2022 0
CLE 2023 0
CLE 2024 0
COL 1999 58935837
COL 2000 60811190
COL 2001 70916334
COL 2002 55201043
COL 2003 67179667
COL 2004 56445167
COL 2005 47839000
COL 2006 40232000
COL 2007 53541000
COL 2008 68655500
COL 2009 72835200
COL 2010 84674000
COL 2011 88148071
COL 2012 75005000
COL 2013 72780500
COL 2014 68700000
COL 2015 36000000
COL 2016 37000000
COL 2017 40000000
COL 2018 20000000
COL 2019 20000000
COL 2020 14000000
COL 2021 15000000
COL 2022 0
COL 2023 0
COL 2024 0
DET 1999 36209666
DET 2000 59205167
DET 2001 52831167
DET 2002 54830500
DET 2003 49168000
DET 2004 46203000
DET 2005 65752500
DET 2006 76282866
DET 2007 93085369
DET 2008 136343696
DET 2009 119510145
DET 2010 119439928
DET 2011 105475231
DET 2012 118394000
DET 2013 110072000
DET 2014 111050000
DET 2015 90800000
DET 2016 68800000
DET 2017 68800000
DET 2018 68000000
DET 2019 52000000
DET 2020 46000000
DET 2021 0
DET 2022 0
DET 2023 0
DET 2024 0
HOU 1999 50114000
HOU 2000 51289111
HOU 2001 58312667
HOU 2002 62698417
HOU 2003 70730000
HOU 2004 75397000
HOU 2005 76779000
HOU 2006 81525416
HOU 2007 87379000
HOU 2008 87556807
HOU 2009 105804414
HOU 2010 92400500
HOU 2011 69524500
HOU 2012 37160500
HOU 2013 16848800
HOU 2014 3000000
HOU 2015 0
HOU 2016 0
HOU 2017 0
HOU 2018 0
HOU 2019 0
HOU 2020 0
HOU 2021 0
HOU 2022 0
HOU 2023 0
HOU 2024 0
KCR 1999 24035000
KCR 2000 24700500
KCR 2001 31970000
KCR 2002 47257000
KCR 2003 40518000
KCR 2004 46667500
KCR 2005 33921000
KCR 2006 46959000
KCR 2007 66917500
KCR 2008 58970500
KCR 2009 76006333
KCR 2010 71405210
KCR 2011 31962000
KCR 2012 55245075
KCR 2013 77836600
KCR 2014 52300000
KCR 2015 45750000
KCR 2016 27750000
KCR 2017 20250000
KCR 2018 5000000
KCR 2019 6000000
KCR 2020 0
KCR 2021 0
KCR 2022 0
KCR 2023 0
KCR 2024 0
LAA 1999 53933166
LAA 2000 52264167
LAA 2001 33778500
LAA 2002 61521667
LAA 2003 78729167
LAA 2004 100233167
LAA 2005 84400322
LAA 2006 103472000
LAA 2007 108868833
LAA 2008 109716333
LAA 2009 117769000
LAA 2010 104163866
LAA 2011 135154166
LAA 2012 139619500
LAA 2013 102104500
LAA 2014 106075000
LAA 2015 121025000
LAA 2016 105500000
LAA 2017 58000000
LAA 2018 27000000
LAA 2019 28000000
LAA 2020 29000000
LAA 2021 30000000
LAA 2022 0
LAA 2023 0
LAA 2024 0
LAD 1999 80662453
LAD 2000 87924286
LAD 2001 101272620
LAD 2002 85228453
LAD 2003 105572620
LAD 2004 81684334
LAD 2005 69639000
LAD 2006 98092187
LAD 2007 108059524
LAD 2008 102578135
LAD 2009 101711592
LAD 2010 94953516
LAD 2011 103785477
LAD 2012 153324600
LAD 2013 209817000
LAD 2014 174797000
LAD 2015 138047000
LAD 2016 129797000
LAD 2017 122547000
LAD 2018 103547000
LAD 2019 21500000
LAD 2020 0
LAD 2021 0
LAD 2022 0
LAD 2023 0
LAD 2024 0
MIA 1999 21085000
MIA 2000 19194000
MIA 2001 28360500
MIA 2002 41777917
MIA 2003 49150000
MIA 2004 41838042
MIA 2005 60058834
MIA 2006 14671500
MIA 2007 30127000
MIA 2008 19441500
MIA 2009 38879000
MIA 2010 57454719
MIA 2011 57694000
MIA 2012 89198000
MIA 2013 33290500
MIA 2014 2500000
MIA 2015 2500000
MIA 2016 0
MIA 2017 0
MIA 2018 0
MIA 2019 0
MIA 2020 0
MIA 2021 0
MIA 2022 0
MIA 2023 0
MIA 2024 0
MIL 1999 43377395
MIL 2000 35633333
MIL 2001 43670833
MIL 2002 46532833
MIL 2003 40322000
MIL 2004 27528500
MIL 2005 39934833
MIL 2006 57568333
MIL 2007 65236500
MIL 2008 76687499
MIL 2009 81384502
MIL 2010 80708278
MIL 2011 85022333
MIL 2012 95717000
MIL 2013 73742000
MIL 2014 72700000
MIL 2015 72500000
MIL 2016 32000000
MIL 2017 24250000
MIL 2018 19000000
MIL 2019 18000000
MIL 2020 16000000
MIL 2021 20000000
MIL 2022 0
MIL 2023 0
MIL 2024 0
MIN 1999 22107500
MIN 2000 17529500
MIN 2001 23895000
MIN 2002 40200000
MIN 2003 55505000
MIN 2004 48140000
MIN 2005 55540000
MIN 2006 63069006
MIN 2007 70649500
MIN 2008 56932766
MIN 2009 66914266
MIN 2010 85409166
MIN 2011 112737000
MIN 2012 92566000
MIN 2013 71340000
MIN 2014 46000000
MIN 2015 30350000
MIN 2016 27500000
MIN 2017 23000000
MIN 2018 23000000
MIN 2019 0
MIN 2020 0
MIN 2021 0
MIN 2022 0
MIN 2023 0
MIN 2024 0
NYM 1999 65727092
NYM 2000 79509776
NYM 2001 92444428
NYM 2002 91550260
NYM 2003 116876429
NYM 2004 78844303
NYM 2005 100669321
NYM 2006 101251963
NYM 2007 114947663
NYM 2008 129984376
NYM 2009 151192237
NYM 2010 132753942
NYM 2011 100252602
NYM 2012 91621424
NYM 2013 41979935
NYM 2014 50000000
NYM 2015 27000000
NYM 2016 29000000
NYM 2017 30000000
NYM 2018 31000000
NYM 2019 15000000
NYM 2020 12000000
NYM 2021 0
NYM 2022 0
NYM 2023 0
NYM 2024 0
NYY 1999 86334359
NYY 2000 92713260
NYY 2001 110181143
NYY 2002 125498583
NYY 2003 147199814
NYY 2004 184193950
NYY 2005 208306817
NYY 2006 186663079
NYY 2007 206659045
NYY 2008 211496041
NYY 2009 206330039
NYY 2010 206733389
NYY 2011 194104628
NYY 2012 192967425
NYY 2013 223111775
NYY 2014 124000000
NYY 2015 66500000
NYY 2016 67500000
NYY 2017 45000000
NYY 2018 0
NYY 2019 0
NYY 2020 0
NYY 2021 0
NYY 2022 0
NYY 2023 0
NYY 2024 0
OAK 1999 24421833
OAK 2000 33172333
OAK 2001 33810750
OAK 2002 39781167
OAK 2003 50260834
OAK 2004 59102167
OAK 2005 54025762
OAK 2006 62243079
OAK 2007 79366940
OAK 2008 47967126
OAK 2009 61635000
OAK 2010 52932400
OAK 2011 67094000
OAK 2012 56630000
OAK 2013 56917500
OAK 2014 41875000
OAK 2015 22500000
OAK 2016 0
OAK 2017 0
OAK 2018 0
OAK 2019 0
OAK 2020 0
OAK 2021 0
OAK 2022 0
OAK 2023 0
OAK 2024 0
PHI 1999 30957500
PHI 2000 44308000
PHI 2001 41663833
PHI 2002 55004999
PHI 2003 70460000
PHI 2004 92619167
PHI 2005 95522000
PHI 2006 87923333
PHI 2007 88668213
PHI 2008 94302380
PHI 2009 114679046
PHI 2010 141928379
PHI 2011 172148379
PHI 2012 171501558
PHI 2013 166860000
PHI 2014 123500000
PHI 2015 102500000
PHI 2016 88000000
PHI 2017 45500000
PHI 2018 22500000
PHI 2019 24000000
PHI 2020 0
PHI 2021 0
PHI 2022 0
PHI 2023 0
PHI 2024 0
PIT 1999 23997666
PIT 2000 29328334
PIT 2001 49535833
PIT 2002 38321099
PIT 2003 50709929
PIT 2004 32227929
PIT 2005 37496000
PIT 2006 46063750
PIT 2007 38537833
PIT 2008 48286283
PIT 2009 51112500
PIT 2010 35033000
PIT 2011 44180000
PIT 2012 70077000
PIT 2013 72687000
PIT 2014 44700000
PIT 2015 14000000
PIT 2016 17500000
PIT 2017 20500000
PIT 2018 22250000
PIT 2019 8500000
PIT 2020 0
PIT 2021 0
PIT 2022 0
PIT 2023 0
PIT 2024 0
SDP 1999 41851513
SDP 2000 47879333
SDP 2001 39182833
SDP 2002 41190000
SDP 2003 45210000
SDP 2004 55384833
SDP 2005 62623833
SDP 2006 69896141
SDP 2007 57726567
SDP 2008 72286416
SDP 2009 49722800
SDP 2010 37799300
SDP 2011 45040240
SDP 2012 54744700
SDP 2013 65794200
SDP 2014 27750000
SDP 2015 27000000
SDP 2016 18000000
SDP 2017 9000000
SDP 2018 0
SDP 2019 0
SDP 2020 0
SDP 2021 0
SDP 2022 0
SDP 2023 0
SDP 2024 0
SEA 1999 52325003
SEA 2000 60295000
SEA 2001 72745834
SEA 2002 80282668
SEA 2003 86659167
SEA 2004 81215834
SEA 2005 86213334
SEA 2006 87959833
SEA 2007 105080833
SEA 2008 117276482
SEA 2009 98919166
SEA 2010 84593500
SEA 2011 81196100
SEA 2012 77752000
SEA 2013 62002600
SEA 2014 43857000
SEA 2015 31857000
SEA 2016 25857000
SEA 2017 26857000
SEA 2018 26857000
SEA 2019 27858000
SEA 2020 1000000
SEA 2021 0
SEA 2022 0
SEA 2023 0
SEA 2024 0
SFG 1999 46798057
SFG 2000 53737826
SFG 2001 61105167
SFG 2002 78299835
SFG 2003 74102167
SFG 2004 72569166
SFG 2005 90199500
SFG 2006 90056419
SFG 2007 90219056
SFG 2008 74094500
SFG 2009 79483950
SFG 2010 98186333
SFG 2011 118198333
SFG 2012 111157350
SFG 2013 139845667
SFG 2014 101916667
SFG 2015 71166666
SFG 2016 61000000
SFG 2017 52900000
SFG 2018 54400000
SFG 2019 33400000
SFG 2020 21400000
SFG 2021 21400000
SFG 2022 0
SFG 2023 0
SFG 2024 0
STL 1999 49438195
STL 2000 61653863
STL 2001 74013333
STL 2002 74260875
STL 2003 82881666
STL 2004 83840333
STL 2005 91346833
STL 2006 87356371
STL 2007 89904823
STL 2008 91476449
STL 2009 90128409
STL 2010 93540751
STL 2011 98746072
STL 2012 111221000
STL 2013 91683000
STL 2014 82000000
STL 2015 69250000
STL 2016 71000000
STL 2017 73500000
STL 2018 47500000
STL 2019 0
STL 2020 0
STL 2021 0
STL 2022 0
STL 2023 0
STL 2024 0
TBR 1999 38660000
TBR 2000 53840129
TBR 2001 41980000
TBR 2002 33878000
TBR 2003 19630000
TBR 2004 29554167
TBR 2005 28666567
TBR 2006 34917967
TBR 2007 24123500
TBR 2008 44569897
TBR 2009 65663434
TBR 2010 70123471
TBR 2011 41053571
TBR 2012 62888000
TBR 2013 56448500
TBR 2014 23500000
TBR 2015 29000000
TBR 2016 19000000
TBR 2017 22500000
TBR 2018 22500000
TBR 2019 24500000
TBR 2020 15000000
TBR 2021 18500000
TBR 2022 19500000
TBR 2023 13000000
TBR 2024 0
TEX 1999 76709931
TEX 2000 67770921
TEX 2001 87213500
TEX 2002 103359455
TEX 2003 93125000
TEX 2004 43183750
TEX 2005 54877500
TEX 2006 67897662
TEX 2007 67937675
TEX 2008 67316996
TEX 2009 69170548
TEX 2010 55668644
TEX 2011 90384264
TEX 2012 123638900
TEX 2013 98760100
TEX 2014 105825000
TEX 2015 104850000
TEX 2016 78200000
TEX 2017 61200000
TEX 2018 51750000
TEX 2019 15000000
TEX 2020 15000000
TEX 2021 14000000
TEX 2022 14000000
TEX 2023 15000000
TEX 2024 0
TOR 1999 44869333
TOR 2000 46038332
TOR 2001 73695999
TOR 2002 73064333
TOR 2003 50959000
TOR 2004 50017000
TOR 2005 45719500
TOR 2006 71365000
TOR 2007 81061300
TOR 2008 97390000
TOR 2009 82733700
TOR 2010 60919100
TOR 2011 62117800
TOR 2012 74784200
TOR 2013 97682700
TOR 2014 111450000
TOR 2015 114200000
TOR 2016 77000000
TOR 2017 30750000
TOR 2018 22000000
TOR 2019 0
TOR 2020 0
TOR 2021 0
TOR 2022 0
TOR 2023 0
TOR 2024 0
WSN 1999 17480500
WSN 2000 29794333
WSN 2001 34934500
WSN 2002 38245500
WSN 2003 47848500
WSN 2004 40587500
WSN 2005 47937000
WSN 2006 54168000
WSN 2007 30537500
WSN 2008 54511000
WSN 2009 58553000
WSN 2010 63900000
WSN 2011 66192928
WSN 2012 88105000
WSN 2013 110152250
WSN 2014 88325000
WSN 2015 86250000
WSN 2016 47000000
WSN 2017 47000000
WSN 2018 26000000
WSN 2019 18000000
WSN 2020 18000000
WSN 2021 0
WSN 2022 0
WSN 2023 0
WSN 2024 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment