D3.js - 堆积条形图

时间:2014-02-04 08:05:45

标签: d3.js

这是类似d3.js - group 2 data values in a stacked bar chart的查询。

我正在努力研究原型。

我有以下csv

date,pass,fail,compid
01/2014,1,2,001
01/2014,11,5,002
02/2014,7,3,001
02/2014,8,1,002
03/2014,2,3,001
03/2014,6,1,002

我想显示日期的总通过和失败(包括两个comp id)。因此对于01/2014,总传递为12,失败为7.因此,对于jan-2014的x轴,我需要一个堆叠条,显示传递为12并且失败为7。

我的工作代码如下:

<!DOCTYPE html>
<html>
  <head>
    <title>Pass vs Fail</title>

    <script src="http://d3js.org/d3.v3.min.js"></script>


    <style type="text/css">

svg {
  width: 980px;
  height: 500px;
  border: solid 1px #ccc;
  font: 10px sans-serif;
  shape-rendering: crispEdges;
}

label {  
    display: inline-block;  
    cursor: pointer;  
    position: relative;
    padding-left:1px;  
    margin-right: 5px;  
    font-size: 13px;  
}  


g.axis path, g.axis line {fill:none; stroke:black; shape-rendering:crispEdges;}


    </style>
  </head>
  <body>

<div id="container">
<br>
</div>

    <script type="text/javascript">


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 tempdata;

    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("scanpassfail.csv", function(scan) {

  // Transpose the data into layers by cause.
  var scantypes = d3.layout.stack()(["pass", "fail"].map(function(scans) {
    return scan.map(function(d) {
      return {x: parse(d.date), y: +d[scans],z:d.compid,typescan:scans};
    });
  }));


  scan.forEach(function(d,i) {
    var y0 = 0;
    tempdata=d;
    d.date = d.date;
    d.pass = +d.pass;
    d.fail =+d.fail;
  }); 



  // 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)
    .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"));


var Rectangles = [
   { "x_axis": 870, "y_axis": -400,"color" : "#819FF7"},
   { "x_axis": 870, "y_axis": -440,"color" : "#CB491A"}];


var rectangle= svg.selectAll("#container").data(Rectangles).enter().append("rect");

var RectangleAttrb = rectangle.attr("x", function (d) { return d.x_axis; })
                       .attr("y", function (d) { return d.y_axis; })
                       .attr("width",30 )
               .attr("height",20)
                       .style("fill", function(d) { return d.color; });


var text = svg.append('text').text('Pass')
           .attr('x', 904)
           .attr('y', -390)
       .attr("style", "font-size: 18; font-family: Helvetica,sans-serif")
           .attr('fill', 'black')

var text = svg.append('text').text('Fail')
              .attr('x',904)
              .attr('y', -430)
          .attr("style", "font-size: 18; font-family: Helvetica,sans-serif")
              .attr('fill', 'black')



});

    </script>
  </body>
</html>    </script>
  </body>
</html>

虽然我试图根据日期对值进行分组,但它没有成功。

scan.forEach(function(d,i) {
        var y0 = 0;
        tempdata=d;
        d.date = d.date;
        d.pass = +d.pass;
        d.fail =+d.fail;
      }); 

我被困在可以根据列值的相似性添加列值的地方。

0 个答案:

没有答案