简体   繁体   English

如何让第二个条形显示在分组条形图上?

[英]How to get second bars to show on grouped bar chart?

I'm trying to get a second bar into my graph.我正在尝试在我的图表中添加第二个条形。 The elements are correctly getting appended but not in the correct location and not the correct height.元素正确地被附加但不在正确的位置和不正确的高度。 What I want from the data to be at the 1 position in the x-axis to have 2 bars one with a height of 2 and the other height of 3 and so on.我希望数据在 x 轴的第 1 个位置有 2 个条,一个高度为 2,另一个高度为 3,依此类推。

http://jsfiddle.net/626uesbh/4/ http://jsfiddle.net/626uesbh/4/

var svg2 = d3.select("#histogram").append("svg2")
      .attr("width", width + margin.left + margin.right)
      .attr("height", height + margin.top + margin.bottom)
    .append("g")
      .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

svg2.selectAll(".rect")
      .data(data)
    .enter().append("g")
      .attr("transform", "translate(0, 100)")
    .append("rect")
      .attr("class", "rect")
      .attr("width", 10)
      .attr("height", function(d) { return height - y2Map(d); })
      .attr("x", xMap)
      .attr("y", yMap)
      .style("fill", "blue");

I suspect svg2 transform is the problem but after trying fiddling with it for an hour I can seem to get what I want.我怀疑 svg2 变换是问题所在,但在尝试摆弄一个小时后,我似乎得到了我想要的东西。 I looked at this question and tried to implement it into my problem.我看着这个问题,并试图将它实施到我的问题中。 D3.js grouped bar chart D3.js 分组条形图

Since each element in your data contains the values for both bars, you have to add them as a group.由于数据中的每个元素都包含两个条形的值,因此您必须将它们添加为一个组。 That is, add a 'g' element to the chart for each element in the array, then add a bar for inner_array[1] and inner_array[2].也就是说,为数组中的每个元素在图表中添加一个“g”元素,然后为inner_array[1] 和inner_array[2] 添加一个条形。

Hopefully this gets you on the right path, essentially all I changed was the stuff after your //bar comment.希望这能让你走上正确的道路,基本上我改变的只是你 //bar 评论之后的内容。

http://jsfiddle.net/626uesbh/6/ http://jsfiddle.net/626uesbh/6/

  // bar
  var bar_groups = svg.selectAll('.bar-group')
      .data(data)
      .enter().append('g')
      .attr('class', 'bar-group');

  bar_groups.append('rect')
      .attr("class", "rect")
      .attr("width", 10)
      .attr("height", function(d) { return height - yScale(d[1]); })
      .attr("x", function(d) {
          return xScale(d[0]) - 5;
      })
      .attr("y", function(d) {
          return yScale(d[1]);
      })
      .style("fill", "green");

  bar_groups.append('rect')
      .attr("class", "rect")
      .attr("width", 10)
      .attr("height", function(d) { return height - yScale(d[2]); })
      .attr("x", function(d) {
          return xScale(d[0]) + 5;
      })
      .attr("y", function(d) {
          return yScale(d[2]);
      })
      .style("fill", "blue");

Note: there are much more elegant ways to do this.注意:有很多更优雅的方法可以做到这一点。 I am only showing you how to add the bars to your existing code.我只是向您展示如何将条形添加到您现有的代码中。 Please take a look at http://bl.ocks.org/mbostock/3887051 for further guidance.请查看http://bl.ocks.org/mbostock/3887051以获得进一步指导。

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

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