Skip to content

Instantly share code, notes, and snippets.

@tylermachado
Last active May 19, 2016 21:02
Show Gist options
  • Save tylermachado/e24468e796146142d0d50c2136b184f3 to your computer and use it in GitHub Desktop.
Save tylermachado/e24468e796146142d0d50c2136b184f3 to your computer and use it in GitHub Desktop.
Major League Soccer attendance history
license: gpl-3.0
height: 600

The complete history of regular season Major League Soccer attendance. Toronto FC kinda saved the league's bacon huh?

Clubs that no longer exist are colored in light gray. I am counting both instances of the San Jose Earthquakes as a single club, with a gap in the data in 2006 and 2007 to account for the years in between the original Earthquakes (who moved to become Houston Dynamo in 2006) and the new expansion Earthquakes who began in 2008.

Current up to the 2015 season, updated on 18 May 2016.

Credits: this code was adapted from Mike Bostock's stacked bar chart. The data is from Wikipedia. The font is Roboto Mono.

<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font: 10px 'Roboto Mono';
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.bar {
fill: steelblue;
}
.x.axis path {
display: none;
}
</style>
<link href='https://fonts.googleapis.com/css?family=Roboto+Mono:400,700' rel='stylesheet' type='text/css'>
<body>
<h1>MLS attendance history</h1>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
var margin = {top: 20, right: 70, bottom: 30, left: 40},
width = window.innerWidth - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var x = d3.scale.ordinal()
.rangeRoundBands([0, width], .1);
var y = d3.scale.linear()
.rangeRound([height, 0]);
var color = d3.scale.ordinal()
.domain(['CHI','CHV','COL','CLB','DAL','DCU','HOU','LA','MIA','MON','NE','NYC','NYRB','OC','PHI','POR','RSL','SJ','SEA','KC','TB','TOR','VAN'])
.range(['red','Gainsboro','DarkRed','yellow','red','black','orange','LightSeaGreen','Gainsboro','blue','navy','skyblue','red','purple','DarkGoldenRod','darkgreen','DarkRed','blue','LimeGreen','skyblue','Gainsboro','red','navy']);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.tickFormat(d3.format(".2s"));
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 + ")");
d3.csv("mls.csv", function(error, data) {
if (error) throw error;
color.domain(d3.keys(data[0]).filter(function(key) { return key !== "Season"; }));
data.forEach(function(d) {
var y0 = 0;
d.club = color.domain().map(function(name) { return {name: name, y0: y0, y1: y0 += +d[name]}; });
d.total = d.club[d.club.length - 1].y1;
});
console.log(data);
// data.sort(function(a, b) { return b.total - a.total; });
x.domain(data.map(function(d) { return d.Season; }));
y.domain([0, d3.max(data, function(d) { return d.total; })]);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
var Season = svg.selectAll(".Season")
.data(data)
.enter().append("g")
.attr("class", "g")
.attr("transform", function(d) { return "translate(" + x(d.Season) + ",0)"; });
Season.selectAll("rect")
.data(function(d) { return d.club; })
.enter().append("rect")
.attr("width", x.rangeBand())
.attr("y", function(d) { return y(d.y1); })
.attr("height", function(d) { return y(d.y0) - y(d.y1); })
.attr("data-club", function(d) {return d.name;})
.style("fill", function(d) { return color(d.name); })
.on("mouseover", function(d, i) {console.log(d);
svg.selectAll("rect").style("opacity", "0.2");
svg.selectAll("[data-club=" + d.name + "]").style("opacity", "1");
})
.on("mouseout", function(d, i) {
svg.selectAll("rect").style("opacity", "1");
});;
var legend = svg.selectAll(".legend")
.data(color.domain())
.enter().append("g")
.attr("class", "legend")
.attr("transform", function(d, i) { return "translate(50," + i * 20 + ")"; });
legend.append("rect")
.attr("x", width - 18)
.attr("width", 18)
.attr("height", 18)
.attr("data-club", function(d) {return d;})
.style("fill", color)
.on("mouseover", function(d, i) {console.log(d);
svg.selectAll("rect").style("opacity", "0.2");
svg.selectAll("[data-club=" + d + "]").style("opacity", "1");
})
.on("mouseout", function(d, i) {
svg.selectAll("rect").style("opacity", "1");
});
legend.append("text")
.attr("x", width - 24)
.attr("y", 9)
.attr("dy", ".35em")
.style("text-anchor", "end")
.text(function(d) { return d; });
});
</script>
Season CHI CHV COL CLB DAL DCU HOU LA MIA MON NE NYC NYRB OC PHI POR RSL SJ SEA KC TB TOR VAN
1996 0 0 10213 18950 16011 15262 0 28916 0 0 19205 0 23898 0 0 0 0 17232 0 12878 11679 0 0
1997 0 0 11835 15043 9678 16698 0 20626 0 0 21423 0 16899 0 0 0 0 13597 0 9058 11333 0 0
1998 17886 0 14812 12274 10947 16007 0 21784 10284 0 19187 0 16519 0 0 0 0 13653 0 8072 10312 0 0
1999 16016 0 14029 17696 12211 17419 0 17632 8689 0 16795 0 14706 0 0 0 0 14959 0 8183 13106 0 0
2000 13387 0 12580 15451 13102 18580 0 20400 7460 0 15463 0 17621 0 0 0 0 12460 0 9112 9452 0 0
2001 16388 0 16481 17511 12574 21518 0 17387 11177 0 15654 0 20806 0 0 0 0 9635 0 9112 10479 0 0
2002 12922 0 20690 17429 13122 16519 0 19047 0 0 16927 0 18155 0 0 0 0 11150 0 12255 0 0 0
2003 14005 0 16772 16250 7906 15565 0 21983 0 0 14641 0 15822 0 0 0 0 10466 0 15573 0 0 0
2004 17153 0 14195 16872 9008 17232 0 23809 0 0 12226 0 17195 0 0 0 0 13001 0 14816 0 0 0
2005 17238 17080 16638 12916 11189 16664 0 24204 0 0 12525 0 15077 0 0 0 18037 13037 0 9691 0 0 0
2006 14111 19840 12056 13294 14982 18251 18935 20814 0 0 11786 0 14570 0 0 0 16366 0 0 11083 0 0 0
2007 16490 14305 14749 15230 15154 20967 15883 24252 0 0 16787 0 16530 0 0 0 15960 0 0 11586 0 20130 0
2008 17034 15114 13659 14662 13024 19835 16939 26009 0 0 17580 0 15928 0 0 0 16179 13856 0 10686 0 20120 0
2009 15487 15725 13018 14175 12440 15585 17624 20827 0 0 12427 0 12744 0 0 0 17831 10329 31203 10053 0 20344 0
2010 15814 14576 14329 14642 10815 14532 17310 21437 0 0 12987 0 18441 0 19254 0 17095 9659 36173 10287 0 20453 0
2011 14274 14830 14838 12185 12861 15181 17694 23335 0 0 13222 0 19691 0 18259 18827 17594 11858 38496 17810 0 20267 20412
2012 16409 13056 15175 14397 14199 13846 21015 23136 0 22772 14001 0 18281 0 18053 20438 19087 13293 43144 19404 0 18155 19475
2013 15228 8366 15440 16080 15373 13646 19923 22152 0 20602 14844 0 19461 0 17867 20674 19218 12765 44038 19708 0 18131 20038
2014 16076 7063 15082 16881 16816 17030 20117 21258 0 17421 16681 0 19421 0 17631 20806 20351 14947 43734 20003 0 22086 20408
2015 16003 0 15657 16985 16015 16244 20658 23392 0 17750 19627 29016 19657 32847 17451 21144 20160 20979 44247 19687 0 23451 20507
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment