简体   繁体   English

D3过渡无法正常工作

[英]D3 transition not working as intended

I want bars on D3 charts to grow from the bottom. 我希望D3图表上的条形从底部开始增长。 I know I need to set the height to zero and the y to height before the transition to get that effect. 我知道我需要在过渡之前将height设置为零,将y设置为高度才能获得该效果。

However, when the chart is drawn after a click on a map, it first loads with the bars growing upwards but any other charts are then drawn from the top to bottom. 但是,单击地图后绘制图表时,首先加载的是向上的条形图,然后再从上到下绘制其他图表。 I can reproduce this in the example below: the chart is drawn correctly on the first click, on any other click after, the chart is drawn from the top to bottom. 我可以在下面的示例中重现该图表:第一次单击时正确绘制了图表,之后单击任何其他单击,则从上到下绘制了图表。

To my knowledge, I have coded it right because on first click, the bars are drawn from the bottom. 就我所知,我已经正确编码了,因为在第一次单击时,条是从底部绘制的。 I cant explain why on all following clicks, the chart is drawn from the top. 我无法解释为什么在随后的所有单击中,图表都是从顶部绘制的。

JSfiddle: http://jsfiddle.net/Monduiz/44javxww/ JSfiddle: http : //jsfiddle.net/Monduiz/44javxww/

Transition on click: 点击转换:

map.on('popupopen', function(e) {
    rects.transition()
    .delay(function (d,i){ return i * 80;})
    .duration(350)
    .attr("height", function(d) {return height - y(d.value);})
    .attr("y", function(d) {return y(d.value); });
});

Before transition: 过渡前:

var rects = bar.append("rect")
    .attr("width", x.rangeBand())
    .attr("y", function(d) {return height; })
    .attr("height", 0)
    .attr("fill", "#1f78b4");

Well, I just had a chat with Mike Bostock and he pointed out what the issue is. 好吧,我刚刚与Mike Bostock聊天,他指出了问题所在。 In the full code, there is also a popupclose event: 在完整的代码中,还有一个popupclose事件:

map.on('popupclose', function(e) {
    rects
    .attr("width", 0);
});

This code is only resetting width to 0 but Y needs to be reset to height as well. 该代码仅将宽度重置为0,但也需要将Y重置为高度。 So, this code is fixing the issue: 因此,此代码解决了这个问题:

map.on('popupclose', function(e) {
    rects
    .attr("width", 0)
    .attr("y", function(d) {return height; });
});

As an added bonus, Mike gave me very insightful feedback. 另外,麦克给了我非常深刻的反馈。 He doesn't recommend using transition the way I have here. 他不建议像我在这里那样使用过渡。 he says it introduces distraction and delay and its not informative. 他说,这会引起分心和延误,而且信息不足。 It would be best to keep transitions for when switching between datasets ( Object constancy ). 在数据集之间切换时最好保持过渡( 对象常量 )。 After review, I have to agree so I will not pursue this animation as an aesthetic property of my visualizations. 复习后,我必须同意,因此我将不会将此动画视为可视化效果的美学特性。

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

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