JavaScript Chart.js animation: onComplete event fired on hover chart js animation oncomplete,animation oncomplete chartjs

I am loading an image export of my chart after the animation is complete, using the onComplete callback. My problem is that everytime you hover over the diagram, the onComplete callback is executed an this slows down the page immensely.

Here's my chart code:

this.chart = new Chart(this.$chart.getContext("2d"), {
        type: 'doughnut',
        data: data,
        options: {
            tooltips: {
                enabled: false
            },
            animation: {
                onComplete: (e) => {
                    console.log(e);
                    this.initExport();
                }
            }
        }
    });

I checked the Chart.js documentation but I did not find any other option that would only fire after the chart is built. So does anyone know how to distinguish between the hover-animation and the "build"-animation of the chart?

Answer:1

Replace your animation option/object with the following :

animation: {
   onComplete(e) => {
      this.options.animation.onComplete = null; //disable after first render
      console.log(e);
      this.initExport();
   }
}

Basically, you need to disable onComplete function by setting the chart.options.animation.onComplete property to null. This will make the onComplete function run only after the chart is rendered for the first time, and prevent it from firing when the chart gets hovered on.

TIP : do not use arrow function for object­'s method. (this will always refer to the window object)

Answer:2

Try this. seems you missed function keyword oncomplete:

this.chart = new Chart(this.$chart.getContext("2d"), {
        type: 'doughnut',
        data: data,
        options: {
            tooltips: {
                enabled: false
            },
            animation: {
                onComplete: function(e) {
                    console.log(e);
                    this.initExport();
                }
            }
        }
    });
Answer:3

I'm trying to display a list inside a list. This loop works in JavaScript. for ( i=0; self.InsuredCommoditiesList.length > i; i++){ self.CommoditiesCategories = self.InsuredCommoditiesList[...

I'm trying to display a list inside a list. This loop works in JavaScript. for ( i=0; self.InsuredCommoditiesList.length > i; i++){ self.CommoditiesCategories = self.InsuredCommoditiesList[...

I'm trying to understand the following pieces of code. If I understand correctly, below I have three resource objects that interact with REST server data sources (these objects are CategorySrv, ...

I'm trying to understand the following pieces of code. If I understand correctly, below I have three resource objects that interact with REST server data sources (these objects are CategorySrv, ...

Hi I am new to react native and I am facing strange issue with routing. I am doing something wrong but need someone to guide me. index.android.js import { LandingScreen } from './src/components/...

Hi I am new to react native and I am facing strange issue with routing. I am doing something wrong but need someone to guide me. index.android.js import { LandingScreen } from './src/components/...

  1. route should declare a screen react native

I am enabling and disabling a div by checking two radio button (yes and no) but I am facing an issue when I am putting both id and class in same div and when I am using two div (one for id as parent ...

I am enabling and disabling a div by checking two radio button (yes and no) but I am facing an issue when I am putting both id and class in same div and when I am using two div (one for id as parent ...

  1. html same class name
  2. html same class different id
  3. html same class
  4. html div class same row
  5. html class name same as id
  6. html repeating same class