Example of a Waffle Chart (or square pie chart) made with d3.js. More info of waffle charts on this post of eagereyes blog, of Robert Kosara: http://eagereyes.org/blog/2008/engaging-readers-with-square-pie-waffle-charts
-
-
Save WillAbides/e30a0f192ac4608b1b886f1bcd7b3c53 to your computer and use it in GitHub Desktop.
Waffle Chart
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
age | population | |
---|---|---|
travis-ci | 30.80% | |
circleci | 15.96% | |
jenkins | 5.87% | |
appveyor | 3.16% | |
coveralls | 2.95% | |
codeship | 2.65% | |
codecov | 2.53% | |
codeclimate | 1.66% | |
greenkeeper | 0.93% | |
drone | 0.91% | |
semaphoreci | 0.89% | |
codacy | 0.74% | |
buildkite | 0.67% | |
wercker | 0.61% | |
teamcity | 0.53% | |
snyk | 0.50% | |
vsts | 0.47% | |
sonarqube | 0.47% | |
scrutinizer | 0.45% | |
code-review | 0.41% | |
Other | 26.82% |
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> | |
<html> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta charset="utf-8"> | |
<style> | |
body { | |
font: 18px Arial; | |
} | |
p { | |
margin: 0; | |
} | |
#waffle { | |
margin-bottom: 30px; | |
} | |
#legend { | |
font: 13px Arial; | |
} | |
</style> | |
</head> | |
<body> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
<div id="title"> | |
<p>A measure, by age population</p> | |
</div> | |
<div id="waffle"> | |
</div> | |
<div id="legend"> | |
</div> | |
</body> | |
<script> | |
var total = 0; | |
var width, | |
height, | |
widthSquares = 20, | |
heightSquares = 5, | |
squareSize = 25, | |
squareValue = 0, | |
gap = 1, | |
theData = []; | |
var color = d3.scale.category10(); | |
d3.csv("data.csv", function(error, data) | |
{ | |
//total | |
total = d3.sum(data, function(d) { return d.population; }); | |
//value of a square | |
squareValue = total / (widthSquares*heightSquares); | |
//remap data | |
data.forEach(function(d, i) | |
{ | |
d.population = +d.population; | |
d.units = Math.floor(d.population/squareValue); | |
theData = theData.concat( | |
Array(d.units+1).join(1).split('').map(function() | |
{ | |
return { squareValue:squareValue, | |
units: d.units, | |
population: d.population, | |
groupIndex: i}; | |
}) | |
); | |
}); | |
width = (squareSize*widthSquares) + widthSquares*gap + 25; | |
height = (squareSize*heightSquares) + heightSquares*gap + 25; | |
var waffle = d3.select("#waffle") | |
.append("svg") | |
.attr("width", width) | |
.attr("height", height) | |
.append("g") | |
.selectAll("div") | |
.data(theData) | |
.enter() | |
.append("rect") | |
.attr("width", squareSize) | |
.attr("height", squareSize) | |
.attr("fill", function(d) | |
{ | |
return color(d.groupIndex); | |
}) | |
.attr("x", function(d, i) | |
{ | |
//group n squares for column | |
col = Math.floor(i/heightSquares); | |
return (col*squareSize) + (col*gap); | |
}) | |
.attr("y", function(d, i) | |
{ | |
row = i%heightSquares; | |
return (heightSquares*squareSize) - ((row*squareSize) + (row*gap)) | |
}) | |
.append("title") | |
.text(function (d,i) | |
{ | |
return "Age range: " + data[d.groupIndex].age + " | " + d.population + " , " + d.units + "%" | |
}); | |
//add legend with categorical data | |
var legend = d3.select("#legend") | |
.append("svg") | |
.attr('width', 300) | |
.attr('height', 200) | |
.append('g') | |
.selectAll("div") | |
.data(data) | |
.enter() | |
.append("g") | |
.attr('transform', function(d,i){ return "translate(0," + i*20 + ")";}); | |
legend.append("rect") | |
.attr("width", 18) | |
.attr("height", 18) | |
.style("fill", function(d, i) { return color(i)}); | |
legend.append("text") | |
.attr("x", 25) | |
.attr("y", 13) | |
.text( function(d) { return d.age}); | |
//add value of a unit square | |
var legend2 = d3.select("#legend") | |
.select('svg') | |
.append('g') | |
.attr('transform', "translate(100,0)"); | |
legend2.append("text") | |
.attr('y', '14') | |
.attr('font-size', '18px') | |
.text("Total: " + total) | |
.attr("fill", "#444444"); | |
}); | |
</script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment