Skip to content

Instantly share code, notes, and snippets.

@stimms
Created June 7, 2013 05:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stimms/5727304 to your computer and use it in GitHub Desktop.
Save stimms/5727304 to your computer and use it in GitHub Desktop.
var BestOfLine = (function () {
function BestOfLine(totalGames, wonLeft, wonRight) {
this.totalGames = totalGames;
this.wonLeft = wonLeft;
this.wonRight = wonRight;
this.width = 500;
this.height = 25;
}
BestOfLine.prototype.render = function (selector) {
var graph = d3.select("#" + selector).append("svg").attr("width", this.width).attr("height", this.height);
graph.append("rect").attr("width", this.width).attr("height", this.height).style("fill", "white").style("stroke", "black").style("stroke-width", "1");
var boxDomain = new Array();
for(var i = 0; i <= this.totalGames; i++) {
boxDomain.push(i);
}
var boxScale = d3.scale.ordinal().domain(boxDomain).rangeBands([
0,
this.width
], 0);
for(var i = 0; i <= this.totalGames; i++) {
graph.append("rect").attr("width", boxScale.rangeBand()).attr("height", this.height).attr("x", boxScale(i)).style("fill", "white").style("stroke", "black").style("stroke-width", "1").transition().delay(i * 100).duration(800).style("fill", this.shouldBeFilled(i));
}
};
BestOfLine.prototype.shouldBeFilled = function (boxIndex) {
if(boxIndex < (this.totalGames + 1) / 2) {
if(boxIndex < this.wonLeft) {
return "steelblue";
}
return "white";
} else {
if(boxIndex >= (this.totalGames + 1) - this.wonRight) {
return "steelblue";
}
return "white";
}
};
return BestOfLine;
})();
<html>
<body>
<h2>Best of 7 - Score 1 - 2</h2>
<div id="graph1" style="padding: 10px"></div>
<h2>Best of 5 - Score 1 - 2</h2>
<div id="graph2" style="padding: 10px"></div>
<h2>Best of 7 - Score 4 - 2</h2>
<div id="graph3" style="padding: 10px"></div>
</body>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script src="BestOfLine.js"></script>
<script>
var graph1 = new BestOfLine(7, 1, 2);
graph1.render("graph1");
var graph2 = new BestOfLine(5, 1, 2);
graph2.render("graph2");
var graph3 = new BestOfLine(7, 4, 2);
graph3.render("graph3");
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment