Skip to content

Commit

Permalink
Fix hover interaction animations (#119)
Browse files Browse the repository at this point in the history
Flag the render request as "lazy" so the controller can cancel it in order to correctly animate hover interactions. The 1ms duration is a workaround to make sure an animation is created so the controller can stop it before any transition. Also, verify if there is an active animation, in which case we don't need to explicitly call render().
  • Loading branch information
simonbrunel authored Mar 14, 2019
1 parent c759a93 commit aab0a1f
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions src/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,27 @@ function handleClickEvents(chart, event) {
}
}

// https://github.com/chartjs/chartjs-plugin-datalabels/issues/108
function invalidate(chart) {
if (chart.animating) {
return;
}

// `chart.animating` can be `false` even if there is animation in progress,
// so let's iterate all animations to find if there is one for the `chart`.
var animations = Chart.animationService.animations;
for (var i = 0, ilen = animations.length; i < ilen; ++i) {
if (animations[i].chart === chart) {
return;
}
}

// No render scheduled: trigger a "lazy" render that can be canceled in case
// of hover interactions. The 1ms duration is a workaround to make sure an
// animation is created so the controller can stop it before any transition.
chart.render({duration: 1, lazy: true});
}

Chart.defaults.global.plugins.datalabels = defaults;

var plugin = {
Expand Down Expand Up @@ -214,9 +235,7 @@ var plugin = {

if (expando._dirty || updates.length) {
layout.update(expando._labels);
if (!chart.animating) {
chart.render();
}
invalidate(chart);
}

delete expando._dirty;
Expand Down

0 comments on commit aab0a1f

Please sign in to comment.