Built with blockbuilder.org
Last active
October 25, 2019 01:58
-
-
Save mforando/378cc161ff5857b08cc3c20b8b3b47a1 to your computer and use it in GitHub Desktop.
NFL Score Matrix
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
license: mit |
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
<!DOCTYPE html> | |
<head> | |
<meta charset="utf-8"> | |
<script src="https://d3js.org/d3.v4.min.js"></script> | |
<script src="https://d3js.org/d3-scale-chromatic.v0.3.min.js"></script> | |
<style> | |
body { | |
background:rgb(0,20,40); | |
margin:0;position:fixed;top:0;right:0;bottom:0;left:0; } | |
</style> | |
</head> | |
<body> | |
<script> | |
d3.csv("https://raw.githubusercontent.com/fivethirtyeight/nfl-elo-game/master/data/nfl_games.csv",function(d){ | |
console.log(d); | |
console.log(parseCSV(d)); | |
renderMatrixChart(parseCSV(d)) | |
}) | |
function parseCSV(data){ | |
data.forEach(function(d){ | |
d.score1 = +d.score1; | |
d.score2 = +d.score2; | |
d.season = +d.season; | |
d.playoff = +d.playoff; | |
}) | |
return data; | |
} | |
var width = 400; | |
var height = 400; | |
var svg = d3.select("body").append("svg") | |
.attr("width", width) | |
.attr("height", height) | |
.style("outline","1px solid white") | |
function renderMatrixChart(data){ | |
var nested = d3.nest().key(function(d){return d.score1 + "-" + d.score2;}) | |
.entries(data); | |
var scaleBandX = d3.scaleBand() | |
.range([0,width]) | |
.domain(d3.range(60)); | |
var scaleBandY = d3.scaleBand() | |
.range([0,height]) | |
.domain(d3.range(60)); | |
var scaleBandXLinear = d3.scaleLinear() | |
.range([0,width]) | |
.domain([0,60]); | |
var scaleBandYLinear = d3.scaleLinear() | |
.range([0,height]) | |
.domain([0,60]); | |
var colorScale = d3.scaleSequential(d3.interpolateYlOrRd) | |
.domain([1,60]) | |
.clamp(true); | |
var rects = svg.selectAll("rect").data(nested); | |
rects.enter() | |
.append("rect") | |
.attr("fill",function(d){ | |
return colorScale(d.values.length);}) | |
.style("stroke-width",.5) | |
.style("stroke","white") | |
.attr("width",scaleBandX.bandwidth()) | |
.attr("height",scaleBandY.bandwidth()) | |
.attr("x",function(d){return scaleBandX(d.values[0].score1);}) | |
.attr("y",function(d){return scaleBandY(d.values[0].score2);}) | |
.style("opacity",function(d){ | |
if(d.values.length <= 10){return .15} | |
else{return 1} | |
}) | |
var lines = svg.selectAll(".liney").data(d3.range(10).map(function(d){ | |
return (d) * 7 + 0 | |
})) | |
lines.enter() | |
.append("line") | |
.attr("class","liney") | |
.attr("x1",function(d){return scaleBandXLinear(d);}) | |
.attr("x2",function(d){return scaleBandXLinear(d);}) | |
.attr("y1",0) | |
.attr("y2",height) | |
.style("stroke","white") | |
var linesX = svg.selectAll(".linex").data(d3.range(10).map(function(d){ | |
return (d) * 7 + 0 | |
})) | |
linesX.enter() | |
.append("line") | |
.attr("class","linex") | |
.attr("y1",function(d){return scaleBandYLinear(d);}) | |
.attr("y2",function(d){return scaleBandYLinear(d);}) | |
.attr("x1",0) | |
.attr("x2",width) | |
.style("stroke","white") | |
} | |
</script> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment