How to Create a Group Chart in D3

A bar chart is a simple yet perceptually-accurate way to visualize such data. This introductory tutorial covers how to make a bar chart using the D3 JavaScript library. Now we’ll integrate everything we’ve learned so far to generate a simple bar chart with D3.

We’ll start by reviewing the bar chart we made earlier using div elements. Then we’ll adapt that code to draw the bars with SVG instead, giving us more flexibility over the visual presentation. Finally, we’ll add labels and more data sets, so we can see the data values clearly and are able to convert a bar chart to a grouped bar chart.

The old chart:


The new chart:

grouped chart

We can see the difference clearly. But to understand the process we have to analyze the code.

<!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 = 960 - margin.left - margin.right,
    height = 500 - - margin.bottom;

var x0 = d3.scale.ordinal()
    .rangeRoundBands([0, width], .1);

var x1 = d3.scale.ordinal();

var y = d3.scale.linear()
    .range([height, 0]);

var color = d3.scale.ordinal()
    .range(["#98abc5", "#8a89a6", "#d0743c"]);

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) {
  var ageNames = d3.keys(data[0]).filter(function(key) { return key !== "State"; });

  data.forEach(function(d) {
    d.ages = { return {name: name, value: +d[name]}; });

  x0.domain( { return d.State; }));
  x1.domain(ageNames).rangeRoundBands([0, x0.rangeBand()]);
  y.domain([0, d3.max(data, function(d) { return d3.max(d.ages, function(d) { return d.value; }); })]);

      .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(" + x0(d.State) + ",0)"; });

      .data(function(d) { return d.ages; })
      .attr("width", x1.rangeBand())
      .attr("x", function(d) { return x1(; })
      .attr("y", function(d) { return y(d.value); })
      .attr("height", function(d) { return height - y(d.value); })
      .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; });



In the above example we are using population of different states as data to view the sex ratio and total population at a glance. To achive this data retrival process should be the same as before. But in this chart there is a notification about the particular color against particular section. This notification is housed in upper right corner of the graph. This is called legend. Let us see how to create it?

   var legend = svg.selectAll(".legend")
      .attr("class", "legend")
      .attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });

As for the color we have to mention them manually. The code for to do

var color = d3.scale.ordinal()
    .range(["#98abc5", "#8a89a6", "#d0743c"]);

If you keep a closer eye then you will see that the color is asigining from left to right. It will amke you get through this chart very easily.

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

1 thought on “How to Create a Group Chart in D3

Leave a Reply

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