Set animation dynamically in Chart.JS
See original GitHub issueI 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
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:
- Created 3 years ago
- Comments:6 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Not sure what the issue is in v2.9.4. Works in v3: https://jsfiddle.net/4v8p2gyt/1/
Thanks @kurkle, will marked this as fixed in v3.