title: d3.js - group 2 data values in a stacked bar chart
body: <p>I have the following csv data,</p>
  
  <pre><code>date,scanned,unscanned,compid,sbu
  01/2014,10,90,101,f&amp;r
  02/2014,55,40,101,f&amp;r
  03/2014,45,23,101,f&amp;r
  04/2014,65,35,101,f&amp;r
  05/2014,100,20,101,f&amp;r
  06/2014,50,30,101,f&amp;r
  07/2014,10,90,101,f&amp;r
  08/2014,22,48,101,f&amp;r
  09/2014,0,100,101,f&amp;r
  10/2014,3,97,101,f&amp;r
  11/2014,22,60,101,f&amp;r
  12/2014,57,37,101,f&amp;r
  01/2014,30,100,101,ip
  02/2014,130,10,101,ip
  </code></pre>
  
  <p>Is there a way that we can combine the data for jan-2014 for both the f&amp;r and ip sbu values and show the values in stacked bar. for e.g if i check a checkbox to group, i need to show scanned as 30+10=40 and unscanned as 100+90= 190 in a stack for jan 2014 in x-axis.</p>
  
  <p>My code to build the stack bar is as follows:</p>
  
  <pre><code>var w = 960,
      h = 500,
      p = [20, 50, 30, 20],
  
      x = d3.time.scale().range([1, 80]);
      y = d3.scale.linear().range([0, h - p[0] - p[2]]),
      z = d3.scale.ordinal().range(["#819FF7", "#CB491A"]),
      parse = d3.time.format("%m/%Y").parse,
      format = d3.time.format("%b-%y");
  
      var xAxis=d3.svg.axis()
            .scale(x)
            .orient("bottom")
            .ticks(d3.time.month, 1)
                //.ticks(12)
  
  
  
     xAxis.tickFormat(d3.time.format("%b-%y"));
  
  
      /*var yAxis = d3.svg.axis()
      .scale(y)
      .ticks(12)
      .orient("left");*/
  
  var svg = d3.select("#container").append("svg:svg")
      .attr("width", w)
      .attr("height", h)
    .append("svg:g")
      .attr("transform", "translate(" + p[3] + "," + (h - p[2]) + ")");
  
  d3.csv("scandata.csv", function(scan) {
  
    // Transpose the data into layers by cause.
    var scantypes = d3.layout.stack()(["scanned", "unscanned"].map(function(scans) {
      return scan.map(function(d) {
        return {x: parse(d.date), y: +d[scans],z:d.compid,typescan:scans};
      });
    }));
  
  
  
  
    // Compute the x-domain (by date) and y-domain (by top).
    x.domain(scantypes [0].map(function(d) { return d.x; }));
    y.domain([0, d3.max(scantypes[scantypes .length - 1], function(d) { return d.y0 + d.y; })]);
  
    // Add a group for each scan.
    var cause = svg.selectAll("g.scan")
        .data(scantypes)
      .enter().append("svg:g")
        .attr("class", "scan")
        .style("fill", function(d, i) { return z(i); })
        .style("stroke", function(d, i) { return d3.rgb(z(i)).darker(); });
  
    // Add a rect for each date.
    var rect = cause.selectAll("rect")
        .data(Object)
      .enter().append("svg:rect")
        .attr("id", function(d,i) { return i + " comp " + d.z;  })
        .attr("x", function(d,i) { 
                          if (i ==0) 
                          { 
                              return x(d.x) ;
                          } 
                          else 
                          {
                              return x(d.x);
                          }} )
        .attr("y", function(d) { return -y(d.y0) - y(d.y); })
        .attr("height", function(d) { return y(d.y); })
        .attr("width", 30)//x.rangeBand()/2
      .on("mouseover", function(d){
  
                     return tooltip.style("visibility", "visible")
                                     .text((d.y))//d.typescan + " -  " + 
                                     .style("left", (d3.event.pageX) + "px") 
                                     .style("top", (d3.event.pageY - 20) + "px");      ;})
        .on("mousemove", function(d){
  
                        return tooltip.style("visibility", "visible")
                                     .text((d.y)) //d.typescan + " -  " + 
                                     .style("left", (d3.event.pageX) + "px") 
                                     .style("top", (d3.event.pageY - 20) + "px");      ;})
  
        .on("mouseout", function(d){return tooltip.style("visibility", "hidden");}) 
        .on("click", function(d){});
  
  
  
    var tooltip = d3.select("#container")
      .append("div")
      .style("position", "absolute")
      .style("z-index", "10")
      .style("visibility", "visible")
      .text("Scanned vs UnScanned")
      .style("font", "Arial")
        .style("color", "white")
      .style("font-size", "14px");
  
    //Add x-Axis
      svg.append("g")
      .attr("class", "x axis")
      //.attr("transform", function(d) { return "translate(0,80)"; })
      .call(xAxis)
  
  
  
  
    // Add a label per date.
    var label = svg.selectAll("text")
        .data(x.domain())
      .enter().append("svg:text")
        .attr("x", function(d) { return x(d.x); })//x.rangeBand() / 4
        .attr("y", 6)
        .attr("text-anchor", "middle")
        .attr("dy", ".71em")
        .text(format);
  
    // Add y-axis rules.
    var rule = svg.selectAll("g.rule")
        .data(y.ticks(5))
      .enter().append("svg:g")
        .attr("class", "rule")
        .attr("transform", function(d) { return "translate(0," + -y(d) + ")"; });
  
    rule.append("svg:line")
        .attr("x2", w - p[1] - p[3])
        .style("stroke", function(d) { return d ? "#fff" : "#000"; })
        .style("stroke-opacity", function(d) { return d ? .7 : null; });
  
    rule.append("svg:text")
        .attr("x", -15)
        .style("font-family","Arial 12px")
        .attr("dy", ".25em")
        .text(d3.format(",d"));
  </code></pre>