2
\$\begingroup\$

I've the following d3 chart which is both grouped and each grouped contains a stacked bar. But somehow, I feel this is not a proper way to implement and little complicated. If there was only stacked bar chart, I would have used d3.stack(). Is there anyway I can still use the d3.stack() with current setup. Can someone let me know is there any better way to do this?

Snippet as follows

var data = [
  {
    Category: "cat1",
    type1: 300,
    type2: 450,
    type3: 120
  },
  {
    Category: "cat2",
    type1: 400,
    type2: 100,
    type3: 200
  },
  {
    Category: "cat3",
    type1: 400,
    type2: 100,
    type3: 200
  },
  {
    Category: "cat4",
    type1: 400,
    type2: 100,
    type3: 200
  }
];

var margin = { top: 20, right: 20, bottom: 30, left: 40 },
  width = 500 - margin.left - margin.right,
  height = 500 - margin.top - margin.bottom;
var barWidth = 40;
var x0 = d3.scaleBand().range([0, width]);

var x1 = d3.scaleBand();

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

var xAxis = d3.axisBottom(x0);

var yAxis = d3.axisLeft(y).tickFormat(d3.format(".2s"));

var color = d3.scaleOrdinal().range(["#98abc5", "#8a89a6", "#7b6888"]);

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: ["type1", "type2"],
  column2: ["type3"]
};
var columnHeaders = d3.keys(data[0]).filter(function(key) {
  return key !== "Category";
});
color.domain(
  d3.keys(data[0]).filter(function(key) {
    return key !== "Category";
  })
);
var groupData = data.forEach(function(d) {
  var yColumn = new Array();
  d.columnDetails = columnHeaders.map(function(name) {
    for (ic in innerColumns) {
      if (innerColumns[ic].indexOf(name) >= 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;
  });
});

//console.log(data);
x0.domain(
  data.map(function(d) {
    return d.Category;
  })
);
x1.domain(d3.keys(innerColumns)).range([0, x0.bandwidth()]);

y.domain([
  0,
  1.15 *
    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 stackedbar = svg
  .selectAll(".stackedbar")
  .data(data)
  .enter()
  .append("g")
  .attr("class", "g")
  .attr("transform", function(d) {
    return "translate(" + x0(d.Category) + ",0)";
  });

stackedbar
  .selectAll("rect")
  .data(function(d) {
    return d.columnDetails;
  })
  .enter()
  .append("rect")
  .attr("width", barWidth)
  .attr("x", function(d) {
    return x1(d.column) + (x1.bandwidth() - barWidth) / 2;
  })
  .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);
  });
body {
  font: 10px sans-serif;
}
 
.axis path,
.axis line {
  fill: none;
  stroke: #000;
  shape-rendering: crispEdges;
}
 
.bar {
  fill: steelblue;
}

 
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.6.0/d3.min.js"></script>

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.