forked from gencay's block: Grouped Stacked Chart
Last active
December 14, 2017 16:33
-
-
Save tvachon/b3baf752777c8d3f9194db95f72e0889 to your computer and use it in GitHub Desktop.
D3 v3 Grouped Stacked 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
license: mit |
We can make this file beautiful and searchable if this error is corrected: It looks like row 8 should actually have 11 columns, instead of 12. in line 7.
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
PerformanceBand,Well Below Basic,Below Basic,Basic,Proficient,Advanced,Well Below Basic2,Below Basic2,Basic2,Proficient2,Advanced2 | |
Male,31,55,25,45,12,12,64,12,12,64 | |
Female,52,85,42,74,19,18,50,12,12,64 | |
Special Ed,51,82,36,60,18,15,86,12,12,64 | |
Non-Special Ed,20,34,15,26,75,72,40,12,12,64 | |
Econ Disadvantaged,27,44,21,38,10,88,41,12,12,64 | |
African American,35,58,26,46,19,12,51,12,12,64 | |
Asian,21,40,19,32,91,47,96,12,12,64, | |
Hispanic,21,40,19,32,91,21,40,19,32,91, | |
Native American,21,40,19,32,91,21,40,19,32,91 |
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> | |
<h1> Example of a grouped chart using D3 v3 </h1> | |
<meta charset="utf-8"> | |
<style> | |
body { | |
font: 10px sans-serif; | |
} | |
rect { | |
stroke: #fff; | |
} | |
.axis path, | |
.axis line { | |
fill: none; | |
stroke: #000; | |
shape-rendering: crispEdges; | |
} | |
</style> | |
<body> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script> | |
<script> | |
var margin = {top: 20, right: 20, bottom: 30, left: 40}, | |
width = 1000 - margin.left - margin.right, | |
height = 400 - margin.top - margin.bottom; | |
var x0 = d3.scale.ordinal() | |
.rangeRoundBands([0, width], 0.4); | |
var x1 = d3.scale.ordinal(); | |
var y = d3.scale.linear() | |
.range([height, 0]); | |
var xAxis = d3.svg.axis() | |
.scale(x0) | |
.orient("bottom"); | |
var yAxis = d3.svg.axis() | |
.scale(y) | |
.orient("left") | |
.tickFormat(d3.format(".2s")); | |
var color = d3.scale.ordinal() | |
.range(["#D8666A","#F4865F","#FFD774","#C9E068","#5DB575"]); | |
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 + ")"); | |
var yBegin; | |
var innerColumns = { | |
"column1" : ["Well Below Basic","Below Basic","Basic","Proficient", "Advanced"], | |
"column2" : ["Well Below Basic2","Below Basic2", "Basic2", "Proficient2", "Advanced2"] | |
} | |
d3.csv("data.csv", function(error, data) { | |
var columnHeaders = d3.keys(data[0]).filter(function(key) { return key !== "PerformanceBand"; }); | |
color.domain(d3.keys(data[0]).filter(function(key) { return key !== "PerformanceBand"; })); | |
data.forEach(function(d) { | |
var yColumn = new Array(); | |
d.columnDetails = columnHeaders.map(function(name) { | |
for (ic in innerColumns) { | |
if($.inArray(name, innerColumns[ic]) >= 0){ | |
if (!yColumn[ic]){ | |
yColumn[ic] = 0; | |
} | |
yBegin = yColumn[ic]; | |
yColumn[ic] += +d[name]; | |
return {name: name, column: ic, yBegin: yBegin, yEnd: +d[name] + yBegin,}; | |
} | |
} | |
}); | |
d.total = d3.max(d.columnDetails, function(d) { | |
return d.yEnd; | |
}); | |
}); | |
x0.domain(data.map(function(d) { return d.PerformanceBand; })); | |
x1.domain(d3.keys(innerColumns)).rangeRoundBands([0, x0.rangeBand()]); | |
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) | |
.append("text") | |
.attr("transform", "rotate(-90)") | |
.attr("y", 6) | |
.attr("dy", ".7em") | |
.style("text-anchor", "end") | |
.text(""); | |
var project_stackedbar = svg.selectAll(".project_stackedbar") | |
.data(data) | |
.enter().append("g") | |
.attr("class", "g") | |
.attr("transform", function(d) { return "translate(" + x0(d.PerformanceBand) + ",0)"; }); | |
project_stackedbar.selectAll("rect") | |
.data(function(d) { return d.columnDetails; }) | |
.enter().append("rect") | |
.attr("width", x1.rangeBand()) | |
.attr("x", function(d) { | |
return x1(d.column); | |
}) | |
.attr("y", function(d) { | |
return y(d.yEnd); | |
}) | |
.attr("height", function(d) { | |
return y(d.yBegin) - y(d.yEnd); | |
}) | |
.style("fill", function(d) { return color(d.name); }); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment