question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Set animation dynamically in Chart.JS

See original GitHub issue

I try to set animation dynamically in Chart.js. It should be either enabled or disabled based on some condition.

But it is always either enabled or disabled for some reason.

I’ve made a JSFiddle to better describe my issue

Screen Shot 2021-01-26 at 19 35 08

Check out the code below:

<div class="container">

   <button onclick="chart(true)">ANIMATION</button>

   <button onclick="chart(false)">NO ANIMATION</button>
    
   <div id="animation-info"></div>
 
   <canvas id="chart-container" width="300" height="200"></canvas>

</div>
let animation = true
let CHART

chart(animation)


function chart(animation) {

  const anim_duration = animation == false 
  ? { duration : 0 }
  : { duration : 1000 }
  
  document.getElementById('animation-info').innerHTML =  `<br>Animation: ${animation} <br> Animation duration: ${anim_duration.duration}`

  
  // generate dataset with random values
  // random values are actual chart values here
  var dataset = Array.from( { length: 6 }, () => Math.floor(Math.random() * 6 + 1 ))
  
  var options = {
    type: 'doughnut',
   
    data: {
      labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
      datasets: [{
          label: '# of Votes',
          data: dataset,
          backgroundColor: [ "Red", "Blue", "Yellow", "Green", "Purple", "Orange" ],
          borderWidth: 1,
        }
      ]
    },
    options: {
       animation: anim_duration,
       cutoutPercentage : 60,
       responsive: true,
    }
  }
  
  var ctx = document.getElementById('chart-container').getContext('2d')


  if (typeof CHART == "undefined")
        CHART = new Chart(ctx, options)

  else {
        CHART.config = options  // updating with new chart data
        CHART.update()               // redraw the chart
  }
}
.container {
  text-align: center;
  padding: 20px;
}

#animation-info {
  padding: 5px;
  font-size: 16px;
  font-family: Arial;
}

canvas { 
  opacity : 0.7; 
  margin-top: 20px;
}

button {
  padding: 10px 20px;
  margin: 10px;
}

I have also tried to set the option directly like

Chart.defaults.global.animation.duration = duration

Seems to be the same issue.


I believe the issue is because I don’t invoke new Chart(ctx, options) every time, but update chart configuration data only.

I do this to save resources because I rebuild the chart lots of times and invoking new Chart(ctx, options) every time seems to be a pretty heavy operation.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
kurklecommented, Jan 27, 2021

Not sure what the issue is in v2.9.4. Works in v3: https://jsfiddle.net/4v8p2gyt/1/

0reactions
etimbergcommented, Jan 27, 2021

Thanks @kurkle, will marked this as fixed in v3.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Set animation dynamically in Chart.JS - Stack Overflow
I try to set animation dynamically in Chart.js. It should be either enabled or disabled based on some condition.
Read more >
Animations | Chart.js
The animation configuration provides callbacks which are useful for synchronizing an external draw to the chart animation. The callbacks can be ...
Read more >
Update dynamic chart js | Chart.js Course - YouTube
In this video you will learn how to use the Chart JS dynamically by using the updating function in Chart JS. Using Chart...
Read more >
Chart js Dynamically Add Datasets - YouTube
Dynamically map multiple datasets from a JSON file.Grab code from here: https://github.com/PKazanjian/ chartjs /blob/main/app.
Read more >
Chart Animation Speed Chart JS 3 | ChartJS 3 - YouTube
Chart Animation Speed Chart JS 3 | ChartJS 3. In this video we will explore how to use the newly chart JS version...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found