2014-03-05 66 views
0

我工作的这个柱状图中的应用。d3.js条形图动画

http://jsfiddle.net/NYEaX/166/

如何让他们从底部 b成长我

一)动画条)相应变形的轴旋转到新的数据集

animateBars: function(data){ 

         var svg = d3.select(methods.el["selector"] + " .barchart"); 
         var barrects = d3.select(methods.el["selector"] + " .barrects"); 


         var initialHeight = 0;  



         var bar = barrects.selectAll("rect") 
          .data(data); 

         // Enter 
         bar.enter() 
          .append("rect") 
          .attr("class", "bar") 
          .attr("y", initialHeight); 

         // Update 
         bar 
          .attr("height", initialHeight) 
          .transition() 
          .duration(500) 
          .attr("x", function(d) { return methods.x(d.letter); }) 
          .attr("width", methods.x.rangeBand()) 
          .attr("y", function(d) { return methods.y(d.frequency); }) 
          .attr("height", function(d) { return methods.height - methods.y(d.frequency); }) 

         // Exit 
         bar.exit() 
          .transition() 
          .duration(250) 
          .attr("y", initialHeight) 
          .attr("height", initialHeight) 
          .remove(); 



        }, 

回答

1

对于前者,设置y属性是最大高度,而不是0:

.attr("y", methods.height) 

对于后者,重新计算x域和再次调用轴分量:

methods.x.domain(data.map(function(d) { return d.letter; })); 
svg.select("g.x").call(methods.xAxis); 

完整示例here

+0

谢谢拉尔斯 - 我已经改进了小提琴也改变y轴 - 并就多个图表。 http://jsfiddle.net/NYEaX/171/ –

+0

拉尔斯 - 我有一个堆积图 - 也有类似的问题 - 我已经试过了适应条形图上的代码 - 但按计划它不是动画。 http://jsfiddle.net/NYEaX/172/ –

+0

一个不为我工作的话 - 可能会更好要了解有一个单独的问题。 –