Most basic stacked barplot in d3.js

This post describes how to build a very basic stacked barplot with d3.js. You can see many other examples in the barplot section of the gallery. Learn more about the theory of boxplots in

Barplot section


  • Start by understanding the basics of barplot in d3.js.

  • Data is available here. Have a look to it. Note the wide (untidy) format: each group is provided in a specific line, each subgroup in a specific column.

  • The d3.stack() function is used to stack the data: it computes the new position of each subgroup on the Y axis

  • The output of d3.stack() can be used to create a set of rect as for a normal barplot.
<!DOCTYPE html>
<meta charset="utf-8">

<!-- Load d3.js -->
<script src=""></script>

<!-- Create a div where the graph will take place -->
<div id="my_dataviz"></div>


// set the dimensions and margins of the graph
var margin = {top: 10, right: 30, bottom: 20, left: 50},
    width = 460 - margin.left - margin.right,
    height = 400 - - margin.bottom;

// append the svg object to the body of the page
var svg ="#my_dataviz")
    .attr("width", width + margin.left + margin.right)
    .attr("height", height + + margin.bottom)
          "translate(" + margin.left + "," + + ")");

// Parse the Data
d3.csv("", function(data) {

  // List of subgroups = header of the csv files = soil condition here
  var subgroups = data.columns.slice(1)

  // List of groups = species here = value of the first column called group -> I show them on the X axis
  var groups =, function(d){return(}).keys()

  // Add X axis
  var x = d3.scaleBand()
      .range([0, width])
    .attr("transform", "translate(0," + height + ")")

  // Add Y axis
  var y = d3.scaleLinear()
    .domain([0, 60])
    .range([ height, 0 ]);

  // color palette = one color per subgroup
  var color = d3.scaleOrdinal()

  //stack the data? --> stack per subgroup
  var stackedData = d3.stack()

  // Show the bars
    // Enter in the stack data = loop key per key = group per group
      .attr("fill", function(d) { return color(d.key); })
      // enter a second time = loop subgroup per subgroup to add all rectangles
      .data(function(d) { return d; })
        .attr("x", function(d) { return x(; })
        .attr("y", function(d) { return y(d[1]); })
        .attr("height", function(d) { return y(d[0]) - y(d[1]); })


Related blocks →

  • Simple Box Plot Example in d3.js v4.0 - link

  • D3.js Boxplot with Axes and Labels - link