简体   繁体   English

D3过渡高度

[英]D3 transition height

please tell me why transition() method in line 136 is only for attribute style and does not work for the attribute height. 请告诉我为什么第136行的transition()方法仅适用于属性样式,而不适用于属性高度。

state.selectAll("rect")
        .data(function (d) {
            return d.ages;
        })
        .enter().append("rect")
        .attr("width", x1.rangeBand())
        .attr("x", function (d) {
            return x1(d.name);
        })
        .attr("y", function (d) {
            return y(d.value);
        })
        .transition().delay(function (d, i){ return i * 300;}).duration(3300)
        .attr("height", function (d) {
            return height - y(d.value);
        })
        .style("fill", function (d) {
            return color(d.name);
        });

Example link https://jsfiddle.net/4u332kwp/ 示例链接https://jsfiddle.net/4u332kwp/

It is like this because if you don't provide a fill it defaults to black. 之所以这样,是因为如果您不提供填充,则默认为黑色。 So it then transitions from black to blue. 因此,它从黑色过渡到蓝色。 Whereas you don't give it an initial height so it doesn't transition. 而您没有给它一个初始高度,因此它不会过渡。

To solve this you would have to set height to 0 (or whatever you wish to start at) then transition. 为了解决这个问题,您必须将height设置为0(或您希望从此处开始的任何高度),然后过渡。 I have appended the function to a variable named rects so I can add the transition to that selection later : 我已经将该函数附加到了一个名为rects的变量上,以便稍后可以将过渡添加到该选择中:

var rects = state.selectAll("rect")
  .data(function(d) {
    return d.ages;
  })
  .enter().append("rect")
  .attr("width", x1.rangeBand())
  .attr("x", function(d) {
    return x1(d.name);
  })
  .attr("y", function(d) {
    return y(d.value);
  })
  .attr("height", 0);


rects.transition().delay(function(d, i) {
    return i * 300;
  }).duration(3300)
  .attr("height", function(d) {
    return height - y(d.value);
  })
  .style("fill", function(d) {
    return color(d.name);
  });

Updated fiddle : https://jsfiddle.net/thatOneGuy/4u332kwp/1/ 更新小提琴: https : //jsfiddle.net/thatOneGuy/4u332kwp/1/

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

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