Skip to content

Instantly share code, notes, and snippets.

@n8agrin
Forked from mbostock/.block
Last active December 11, 2015 02:09
Show Gist options
  • Save n8agrin/4528442 to your computer and use it in GitHub Desktop.
Save n8agrin/4528442 to your computer and use it in GitHub Desktop.

This stacked bar chart is constructed from a CSV file storing the populations of different states by age group. The chart employs conventional margins and a number of D3 features:

This example doesn’t use d3.layout.stack because it’s easy to just stack each state independently via array.forEach. Here, it makes sense to group the rects by state (column), rather than grouping by age group (series), as you do with a stacked area chart.

State Under 5 Years 5 to 13 Years 14 to 17 Years 18 to 24 Years 25 to 44 Years 45 to 64 Years 65 Years and Over
AL 310504 552339 259034 450818 1231572 1215966 641667
AK 52083 85640 42153 74257 198724 183159 50277
AZ 515910 828669 362642 601943 1804762 1523681 862573
AR 202070 343207 157204 264160 754420 727124 407205
CA 2704659 4499890 2159981 3853788 10604510 8819342 4114496
CO 358280 587154 261701 466194 1464939 1290094 511094
CT 211637 403658 196918 325110 916955 968967 478007
DE 59319 99496 47414 84464 230183 230528 121688
DC 36352 50439 25225 75569 193557 140043 70648
FL 1140516 1938695 925060 1607297 4782119 4746856 3187797
GA 740521 1250460 557860 919876 2846985 2389018 981024
HI 87207 134025 64011 124834 356237 331817 190067
ID 121746 201192 89702 147606 406247 375173 182150
IL 894368 1558919 725973 1311479 3596343 3239173 1575308
IN 443089 780199 361393 605863 1724528 1647881 813839
IA 201321 345409 165883 306398 750505 788485 444554
KS 202529 342134 155822 293114 728166 713663 366706
KY 284601 493536 229927 381394 1179637 1134283 565867
LA 310716 542341 254916 471275 1162463 1128771 540314
ME 71459 133656 69752 112682 331809 397911 199187
MD 371787 651923 316873 543470 1556225 1513754 679565
MA 383568 701752 341713 665879 1782449 1751508 871098
MI 625526 1179503 585169 974480 2628322 2706100 1304322
MN 358471 606802 289371 507289 1416063 1391878 650519
State Under 5 Years 5 to 13 Years 14 to 17 Years 18 to 24 Years 25 to 44 Years 45 to 64 Years 65 Years and Over
Alabama 310504 552339 259034 450818 1231572 1215966 641667
Alaska 52083 85640 42153 74257 198724 183159 50277
Arizona 515910 828669 362642 601943 1804762 1523681 862573
Arkansas 202070 343207 157204 264160 754420 727124 407205
California 2704659 4499890 2159981 3853788 10604510 8819342 4114496
Colorado 358280 587154 261701 466194 1464939 1290094 511094
Connecticut 211637 403658 196918 325110 916955 968967 478007
Deleware 59319 99496 47414 84464 230183 230528 121688
District of Columbia 36352 50439 25225 75569 193557 140043 70648
Florida 1140516 1938695 925060 1607297 4782119 4746856 3187797
Georgia 740521 1250460 557860 919876 2846985 2389018 981024
Hawaii 87207 134025 64011 124834 356237 331817 190067
Idaho 121746 201192 89702 147606 406247 375173 182150
Illinois 894368 1558919 725973 1311479 3596343 3239173 1575308
Indiana 443089 780199 361393 605863 1724528 1647881 813839
Iowa 201321 345409 165883 306398 750505 788485 444554
Kansas 202529 342134 155822 293114 728166 713663 366706
Kentucky 284601 493536 229927 381394 1179637 1134283 565867
Louisiana 310716 542341 254916 471275 1162463 1128771 540314
Maine 71459 133656 69752 112682 331809 397911 199187
Maryland 371787 651923 316873 543470 1556225 1513754 679565
Massachussets 383568 701752 341713 665879 1782449 1751508 871098
Missouri 625526 1179503 585169 974480 2628322 2706100 1304322
Minnesota 358471 606802 289371 507289 1416063 1391878 650519
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.bar {
fill: steelblue;
}
.x.axis path {
display: none;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
function graph(data, margin) {
margin = margin || {top: 20, right: 20, bottom: 30, left: 40};
var width = 615 - margin.left - margin.right,
height = 379 - margin.top - margin.bottom;
var x = d3.scale.ordinal()
.rangeRoundBands([0, width], .1);
var y = d3.scale.linear()
.rangeRound([height, 0]);
var color = d3.scale.ordinal()
.range(["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00"]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.tickFormat(d3.format(".2s"));
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 + ")");
color.domain(d3.keys(data[0]).filter(function(key) { return key !== "State"; }));
data.forEach(function(d) {
var y0 = 0;
d.ages = color.domain().map(function(name) { return {name: name, y0: y0, y1: y0 += +d[name]}; });
d.total = d.ages[d.ages.length - 1].y1;
});
data.sort(function(a, b) { return b.total - a.total; });
x.domain(data.map(function(d) { return d.State; }));
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)
var state = svg.selectAll(".state")
.data(data)
.enter().append("g")
.attr("class", "g")
.attr("transform", function(d) { return "translate(" + x(d.State) + ",0)"; });
state.selectAll("rect")
.data(function(d) { return d.ages; })
.enter().append("rect")
.attr("width", x.rangeBand())
.attr("y", function(d) { return y(d.y1); })
.attr("height", function(d) { return y(d.y0) - y(d.y1); })
.style("fill", function(d) { return color(d.name); });
var legend = svg.selectAll(".legend")
.data(color.domain().slice().reverse())
.enter().append("g")
.attr("class", "legend")
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });
legend.append("rect")
.attr("x", width - 18)
.attr("width", 18)
.attr("height", 18)
.style("fill", color);
legend.append("text")
.attr("x", width - 24)
.attr("y", 9)
.attr("dy", ".35em")
.style("text-anchor", "end")
.text(function(d) { return d; });
return svg;
}
d3.csv("data.csv", function(error, data) {
graph(data);
d3.csv("data.csv", function(error, data) {
var svg = graph(data, {top: 20, right: 20, bottom: 100, left: 40});
svg.selectAll('.x.axis text')
.attr('transform', 'rotate(90) translate(10, -12)')
.style('text-anchor', 'start');
d3.csv("data-abrev.csv", function(error, data) {
graph(data);
});
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment