Created
June 8, 2013 05:22
-
-
Save stimms/5734160 to your computer and use it in GitHub Desktop.
Visualization for showing a best of series popular in sporting events
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var BestOfLine = (function () { | |
function BestOfLine(totalGames, wonLeft, wonRight, options) { | |
this.totalGames = totalGames; | |
this.wonLeft = wonLeft; | |
this.wonRight = wonRight; | |
this.width = 500; | |
this.height = 25; | |
this.options = { | |
}; | |
this.options.leftTeamColour = "steelblue"; | |
this.options.rightTeamColour = "lightgreen"; | |
this.options.defaultColour = "white"; | |
$.extend(this.options, options); | |
} | |
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.getFillColour(i)); | |
} | |
}; | |
BestOfLine.prototype.getFillColour = function (boxIndex) { | |
if(boxIndex < (this.totalGames) / 2) { | |
if(boxIndex < this.wonLeft) { | |
return this.options.leftTeamColour; | |
} | |
return this.options.defaultColour; | |
} else { | |
if(boxIndex >= (this.totalGames) - this.wonRight) { | |
return this.options.rightTeamColour; | |
} | |
return this.options.defaultColour; | |
} | |
}; | |
return BestOfLine; | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
declare var d3: any; | |
declare var $: any; | |
class BestOfLine | |
{ | |
width: number; | |
height: number; | |
graph: any; | |
options: any; | |
//scales | |
groupScale: any; | |
heightScale: any; | |
constructor(public totalGames, public wonLeft, public wonRight, options) | |
{ | |
this.width = 500; | |
this.height = 25; | |
this.options = {}; | |
this.options.leftTeamColour = "steelblue"; | |
this.options.rightTeamColour = "lightgreen"; | |
this.options.defaultColour = "white"; | |
$.extend(this.options, options); | |
} | |
render(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++) | |
{ | |
var points = ""; | |
points += i * 5 " 0,"; | |
points += "0 0,"; | |
graph.append("polygon") | |
.attr("points", ) | |
.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.getFillColour(i)); | |
} | |
} | |
getFillColour(boxIndex):string | |
{ | |
if(boxIndex < (this.totalGames) /2) | |
{ | |
if(boxIndex < this.wonLeft) | |
return this.options.leftTeamColour; | |
return this.options.defaultColour; | |
} | |
else | |
{ | |
if(boxIndex >= (this.totalGames ) - this.wonRight) | |
return this.options.rightTeamColour; | |
return this.options.defaultColour; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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 - 3</h2> | |
<div id="graph3" style="padding: 10px"></div> | |
</body> | |
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.0.1.js" charset="utf-8"></script> | |
<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, {leftTeamColour: "pink"}); | |
graph2.render("graph2"); | |
var graph3 = new BestOfLine(7, 4, 3); | |
graph3.render("graph3"); | |
</script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment