Making a Stacked Bar Chart in D3

Wherever we see data visualization we can see a bar chart. Weather it is a management issue or a project planning, even normal data visualization are also done by bar charts. For this reason bar charts have been modified in various type along the way.
Previously we talked about basic and grouped chart. In this article we are going to talk about a different approach stacked bar chart. In basic chart we have worked with a single set of data. Whereas we have worked with multiple sets of data in grouped chart . in grouped chart every single data created a single bar. In this feature stacked bar is unlike grouped bar.
<h2>What is stacked bar chart?</h2>
As you can assume from the name stacked bar chart is a bar chart where data are stacked one over another. In this way we can show a all the data of a single entry in a single bar. This stores up space and make the chart look compact.
Let us look to the below example to understand it thoroughly. In the below example we have kept the data in a tsv file.

<!DOCTYPE html>
<meta charset="utf-8">
body {
font: 10px sans-serif;
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
.bar {
fill: steelblue;
.x.axis path {
display: none;
<script src=""></script>
var margin = {top: 20, right: 20, bottom: 30, left: 40},
width = 500 - margin.left - margin.right,
height = 400 - - 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(["#edc31e", "#bc5532", "#66b7f4"]);
var xAxis = d3.svg.axis()
var yAxis = d3.svg.axis()
var svg ="body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + + margin.bottom)
.attr("transform", "translate(" + margin.left + "," + + ")");
d3.csv("data.csv", function(error, data) {
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.ages[d.ages.length - 1].y1;
data.sort(function(a, b) { return -; });
x.domain( { return d.State; }));
y.domain([0, d3.max(data, function(d) { return; })]);
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.attr("class", "y axis")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
var state = svg.selectAll(".state")
.attr("class", "g")
.attr("transform", function(d) { return "translate(" + x(d.State) + ",0)"; });
.data(function(d) { return d.ages; })
.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(; });
var legend = svg.selectAll(".legend")
.attr("class", "legend")
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });
.attr("x", width - 18)
.attr("width", 18)
.attr("height", 18)
.style("fill", color);
.attr("x", width - 24)
.attr("y", 9)
.attr("dy", ".35em")
.style("text-anchor", "end")
.text(function(d) { return d; });

If you find this article helpful, you can connect us in Google+ and Twitter.

1 thought on “Making a Stacked Bar Chart in D3

Leave a Reply

Your email address will not be published. Required fields are marked *