|
<!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> |