简体   繁体   English

D3.js堆积条形图向负方向增长

[英]D3.js stacked bar chart growing to negative direction

I am new to D3 and javascripts. 我是D3和javascript的新手。 I am trying to build an animated vertical stacked bar chart. 我正在尝试构建一个动画的垂直堆积条形图。 I found an example here for a horizontal stacked bar chart. 我在这里找到了水平堆积条形图的示例。 After tweaking it, I got a vertical stacked bar chart, however, growing downwards. 进行调整之后,我得到了一个垂直堆积的条形图,但是该条形图向下增长。 Here is my edited fiddle . 这是我编辑过的小提琴

Update: after making several tweaks, including the ones suggested by below, this finally works: final working upward growing stacked bar chart I feel the place(s) that I need to change should be within this code chunk 更新:进行了一些调整(包括以下建议的调整)后,此方法终于成功了: 最终工作向上增长的堆叠条形图我觉得我需要更改的位置应该在此代码块内

rects = groups.selectAll('.stackedBar')
        .data(function(d,i) {console.log("data", d,i); return d; })
        .enter()
        .append('rect').attr('class','stackedBar')
        .attr('y', function(d) { return yScale(d.y0); })
        .attr('x', function(d, i) {return xScale(d.x); })
        .attr('height',0)
        .attr('width', xScale.rangeBand());

        console.log(document.querySelectorAll(".stacked"));
        var abc=document.querySelectorAll(".stackedBar");

     rects
        .transition()
        .delay(function(d,i,j){console.log("pre",j); return j*500;})
        .attr("y", function(d) { return yScale(d.y0); })
        .attr("height", function(d) { return yScale(d.y); } )
        .duration(1000);

But I searched around and tried to revert the yScale range, the initial position of y, and none of them worked. 但是我四处搜寻,并尝试还原yScale范围,y的初始位置,但没有一个起作用。 I also noticed that no matter what, my yAxis is always below my X axis going doward. 我还注意到,无论如何,我的yAxis始终位于X轴下方。

SVG's start from top left hand corner, so the 'height' specified means the height down the page, so some things work in reverse of what you expect. SVG从左上角开始,因此指定的'height'表示页面下方的高度,因此某些操作与您期望的相反。

Firstly start with your y-scale: the range needs to be mapped in the reverse order (height,0) rather than (0,height). 首先从y比例开始:范围需要以相反的顺序(height,0)而不是(0,height)映射。

yScale = d3.scale.linear().domain([0,yMax]).range([height,0]),1

Likewise, the x-axis needs to be transformed to start at the 'height' . 同样,x轴需要转换为从“ height”开始。

        svg.append('g')
        .attr('class', 'axis')
        .call(xAxis)
        .attr("transform", "translate(0," + height + ")");'

First you must change the orientation of your y-scale: 首先,您必须更改y比例尺的方向:

yScale = d3.scale.linear().domain([0,yMax]).range([height,0])

And the x-axis must be transformed as well so it start at "height". 而且,x轴也必须进行转换,以使其始于“高度”。 In svg the top has height=0 so the bottom of your svg equals "height": 在svg中,顶部的高度为0,因此svg的底部等于“高度”:

svg.append('g')
   .attr('class', 'axis')
   .call(xAxis)
   .attr("transform", "translate(0," + height + ")");

Last but not least, you have to change the building orientation of your stacked bar chart like: 最后但并非最不重要的一点是,您必须更改堆叠条形图的建筑物方向,例如:

rects
   .transition()
   .delay(function(d,i,j){console.log("pre",j); return j*500;})
   .attr("y", function(d) { return yScale(d.y + d.y0); })
   .attr("height", function (d) { return yScale(d.y0) - yScale(d.y + d.y0); } )
   .duration(1000);

Here is an updated fiddle . 这是更新的小提琴

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

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