简体   繁体   English

D3分组堆积的条形图列求和

[英]D3 grouped stacked bar chart column summation

I have a grouped stacked bar chart as looks in this image, http://pasteboard.co/1BKMicBq.jpg I want to add up the sum of the respective bars as in image. 我有一个分组堆积的条形图,如该图像所示: http://pasteboard.co/1BKMicBq.jpg我想将各个条形的总和加起来如图所示。 I have a dataset which has summed values ready for displaying, which looks like, 我有一个数据集,该数据集已准备好显示汇总值,看起来像这样,

var months= [
{  "key": "Jan",
  "values[0]": 25000,
  "values[1]": 25000,
  "values[3]": 25000
       },  {  "key": "Feb",
  "values[0]": 25000,
  "values[1]": 25000,
  "values[3]": 25000
       },
         {  "key": "March",
  "values[0]": 25000,
  "values[1]": 25000,
  "values[3]": 25000
       }
 ]

I have tried it with following code, but i'm not able to get the data over the every bar. 我已经用以下代码尝试过了,但是我无法在每个小节上获取数据。

var project_stackedbar = svg.selectAll(".project_stackedbar")
    .data(data)
  .enter().append("g")
    .attr("class", "g")
    .attr("transform", function (d) { return "translate(" + x0(d.Month) + ",0)"; });

project_stackedbar.selectAll("rect")
    .data(function (d) { return d.columnDetails; })
    .enter()
    .append("rect")
    .attr("width", x1.rangeBand())
    .attr("x", function (d) {
        return x1(d.column);
    })
    .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); });
var sum = 0;

project_stackedbar.selectAll("text")
    .data(function (d) { return d.columnDetails; })
    .enter()
    .append("text")
    .attr("x", function (d) { return x1(d.column) })
    .attr("y", function (d) {
    return y(d.yEnd) + 15;})
    .text(function (d) {if (d.yEnd - d.yBegin !== 0) return "$" + (d.yEnd - d.yBegin);});

columns = svg.append("g")
      .selectAll("text")
    .data(months)
      .enter().append("text")
      .attr("x", function (d) {
          return (x1(d.column));
      })
      .attr("y", function (d, i) {
          return y(d.values[i]) - 60;
      })
      .attr("dy", "1.35em")
      .attr('style', 'font-size:13px')
      .text(function (d, i) {
          if (d.values !== 0) return d3.format("$")(d.values[i]);
      })
      .style({ fill: 'black', "text-anchor": "middle" });

Here i'm only able to get 3 values which will be printed in first group of the bars. 在这里,我只能得到3个值,这些值将被打印在第一组栏中。 I'm not able to get right x axis positions in the graph. 我无法在图中获得正确的x轴位置。 can any one guide me here i'm going wrong? 谁能指导我在这里我错了?

I assume the part of the code you are refering to is this... 我假设您所指的代码部分就是这个...

  columns = svg.append("g")
    .selectAll("text")
    .data(months)
    .enter().append("text")
    .attr("x", function (d) {
      return (x1(d.column));
    })
    .attr("y", function (d, i) {
      return y(d.values[i]) - 60;
    })
    .attr("dy", "1.35em")
    .attr('style', 'font-size:13px')
    .text(function (d, i) {
      if (d.values !== 0) return d3.format("$")(d.values[i]);
    })
    .style({ fill: 'black', "text-anchor": "middle" });

This is the part that you tried to re-use from the example I linked and in that example it is doing the totals. 这是您尝试从我链接的示例中重用的部分,在该示例中,它是总计。 Hence my assumption... 因此,我的假设是...

  1. You are binding the months variable from your post to text elements that you are creating on a g on svg 你要绑定的months从您的帖子变量的文本正在创建一个元素gsvg
  2. You are referencing a member in that data called column which does not exist. 你引用的数据称为成员column不存在。 You haven't included the code where you define the scale x1 but, assuming it is accepting dates, you could try x1(d3.time.format("%b").parse(d.key)) instead of x1(d.column) . 您尚未在定义比例尺x1地方包含代码,但是假设它接受日期,则可以尝试使用x1(d3.time.format("%b").parse(d.key))代替x1(d.column)
  3. In the bound data you have stringified array references like "values[0]", but in your code you are referencing d.values[i] which does not exist in the data you provided. 在绑定的数据中,您有字符串数组引用,例如“ values [0]”,但是在您的代码中,您引用的是d.values[i] ,您提供的数据中不存在该值。 You need to sum the numeric values of the element like this: d3.sum(d3.values(d)) . 您需要像这样对元素的数值求和: d3.sum(d3.values(d))
  4. You are subtracting 60 from your y values which will probably push them outside the viewport, the totals are already being positioned with .attr("dy", "1.35em") , so if you need to adjust, do it there. 您要从y值中减去60,这可能会将它们推到视口之外,总数已经通过.attr("dy", "1.35em") ,因此,如果需要调整,请在此处进行调整。

Given all of this, I would guess that you need something like this: 考虑到所有这些,我想您需要这样的东西:

  var months= [ { "key": "Jan", "values[0]": 25000, "values[1]": 25000, "values[3]": 25000 }, { "key": "Feb", "values[0]": 15000, "values[1]": 25000, "values[3]": 25000 }, { "key": "March", "values[0]": 5000, "values[1]": 25000, "values[3]": 25000 } ]; var margin = {top: 20, right: 50, bottom: 35, left: 50}; var width = 760 - margin.left - margin.right, height = 400 - margin.top - margin.bottom; 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 x1 = d3.scale.ordinal() .domain(months.map(function(d) { return d.key; })) .rangeRoundBands([10, width-10], 0.35); var y = d3.scale.linear() .domain([0, d3.max(months, function(d) { return d3.sum(d3.values(d)); })]) .range([height, 0]); columns = svg.append("g") .selectAll("text") .data(months) .enter().append("text") .attr("x", function (d) { return (x1(d.key)); }) .attr("y", function (d, i) { return y(d3.sum(d3.values(d))); }) .attr("dy", "1.35em") .attr('style', 'font-size:13px') .text(function (d, i) { return d3.format("$")(d3.sum(d3.values(d))); }) .style({ fill: 'black', "text-anchor": "middle" }); 
 svg { outline: 1px solid black;} 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM