admin管理员组文章数量:1430346
I am using a modified version of this D3 tutorial bar chart.
What is important for me is that animations should not stack upon leaving the browser window then trigger all of them when the window is in focus again, causing the browser to hang, so as per this suggestion, I'm trying to use a setTimeout
instead of setInterval
that should be called when the animation has ended.
I'm having problems with the callbacks and I don't understand why the simple transition()
callback is working, but not the enter()
for example.
After I set up the chart & scale, here's how my initializing function looks like:
function redrawTimer() {
data.shift();
data.push(next());
redraw(function(){
console.log('callback');
setTimeout(redrawTimer, 1500);
});
}
setTimeout(redrawTimer, 1500);
function redraw(callback) {
var rect = chart.selectAll("rect")
.data(data, function(d) { return d.time; });
rect.enter().insert("rect")
.attr("x", function(d, i) { return x(i + 1) - .5; })
.attr("y", function(d) { return h - y(d.value) - .5; })
.attr("width", w)
.attr("height", function(d) { return y(d.value); })
.attr("fill", "white")
.attr("fill-opacity", 0.2)
.transition()
.duration(1000)
.attr("x", function(d, i) { return x(i) - .5; })
.each('end', callback); // Doesn't work at all
rect.transition()
.duration(1000)
.attr("x", function(d, i) { return x(i) - .5; });
//.each('end', callback); // Works but for each of the 50 elements
rect.exit().transition()
.duration(1000)
.attr("x", function(d, i) { return x(i - 1) - .5; })
//.each('end', callback) // This only works after the first transition so using it to trigger the next data point is useless
.remove();
}
See this jsFiddle for fiddling with the code :)
It may be that I do not perfectly understand how transition()
works differently on either enter()
or exit()
or just the selector.
Can you enlighten me?
If you can make the callback work, but doesn't solve my problem stated above (animations on browser leaving), please help me with that as well, as I'll upvote your ments/answer.
Edit:
I managed to get rid of the animation queue buildup by testing for which element the transition has ended, and only take the last one:
rect.transition()
.duration(1000)
.attr("x", function(d, i) { return x(i) - .5; })
.each('end', function(d, i){
if(i == 49)
callback();
});
I am using a modified version of this D3 tutorial bar chart.
What is important for me is that animations should not stack upon leaving the browser window then trigger all of them when the window is in focus again, causing the browser to hang, so as per this suggestion, I'm trying to use a setTimeout
instead of setInterval
that should be called when the animation has ended.
I'm having problems with the callbacks and I don't understand why the simple transition()
callback is working, but not the enter()
for example.
After I set up the chart & scale, here's how my initializing function looks like:
function redrawTimer() {
data.shift();
data.push(next());
redraw(function(){
console.log('callback');
setTimeout(redrawTimer, 1500);
});
}
setTimeout(redrawTimer, 1500);
function redraw(callback) {
var rect = chart.selectAll("rect")
.data(data, function(d) { return d.time; });
rect.enter().insert("rect")
.attr("x", function(d, i) { return x(i + 1) - .5; })
.attr("y", function(d) { return h - y(d.value) - .5; })
.attr("width", w)
.attr("height", function(d) { return y(d.value); })
.attr("fill", "white")
.attr("fill-opacity", 0.2)
.transition()
.duration(1000)
.attr("x", function(d, i) { return x(i) - .5; })
.each('end', callback); // Doesn't work at all
rect.transition()
.duration(1000)
.attr("x", function(d, i) { return x(i) - .5; });
//.each('end', callback); // Works but for each of the 50 elements
rect.exit().transition()
.duration(1000)
.attr("x", function(d, i) { return x(i - 1) - .5; })
//.each('end', callback) // This only works after the first transition so using it to trigger the next data point is useless
.remove();
}
See this jsFiddle for fiddling with the code :)
It may be that I do not perfectly understand how transition()
works differently on either enter()
or exit()
or just the selector.
Can you enlighten me?
If you can make the callback work, but doesn't solve my problem stated above (animations on browser leaving), please help me with that as well, as I'll upvote your ments/answer.
Edit:
I managed to get rid of the animation queue buildup by testing for which element the transition has ended, and only take the last one:
rect.transition()
.duration(1000)
.attr("x", function(d, i) { return x(i) - .5; })
.each('end', function(d, i){
if(i == 49)
callback();
});
Share
Improve this question
edited May 23, 2017 at 12:17
CommunityBot
11 silver badge
asked May 9, 2013 at 20:03
CristianCristian
6,0956 gold badges48 silver badges57 bronze badges
1 Answer
Reset to default 7Elements can only have one active transition at a time. The rect.enter().transtition()
immediately gets overwritten by the rect.transition()
(rect
is the update selection which also contains the entering elements!). Therefore rect.enter().transition().each('end', callback)
never gets called.
For more information see the API documentation on transitions or Mike's tutorial.
本文标签: javascriptd3js Transition callback is not called for enter()Stack Overflow
版权声明:本文标题:javascript - d3.js: Transition callback is not called for enter() - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745504518a2661183.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论