2

I'm using a wrapper for Chart.js which allows for an animation callback to determine when the chart is done drawing.

So, my chart options look like this:

public chartOptions: any = {
    animation: {
        duration: 2000,
        onComplete: function () {
            //alert('anim complete');
            this.chartTestMethod();
        }
    },
    responsive: true
};

and my chartTestMethod() looks like this:

chartTestMethod() {
     console.log('chartTestMethod called.');
}

My hope is to have the method chartTestMethod() (which is in the same TypeScript file) called when the chart animation is complete. However, when the animation is complete and that method call line is executed, I get the error:

TypeError: this.chartTestMethod is not a function. 

Basically, how can I call that method properly?

3 Answers 3

7

I imply that your chartTestMethod is in the same class as chartOptions since you're using it on this. You should make sure you understand how this is handled in JavaScript (and TypeScript being a superset of JavaScript). There must be a million references out there.

Without knowing anything about Chart.js, I think it is safe to assume that in no way the this context fits your class instance when onComplete is invoked. So what you want is an arrow function, like this:

onComplete: () => { this.chartTestMethod(); }

Read about TypeScript arrow function to understand how to make sure this is actually pointing to your instance.

Sign up to request clarification or add additional context in comments.

4 Comments

Firstly, it doesn't solve OP's problem, because it is the same as defining function as an key of an object. Secondly, arrow function is not available in all ECMAScript versions.
The question is labelled "typescript" and unless the OP chooses a target supporting arrow functions it will be emulated using closure. In any case, using an arrow function "this" will point to the instance of whatever contains chartOptions.
Thanks for the input guys. sgrtho's answer worked perfectly for me.
@sgrtho Then how can we access the data.datasets in Chart.helpers.each(this.data.datasets.forEach((dataset, i) within oncomplete
1

You got an error because this if referring to the object where function is executing. In your case, this is referring to any.animation object which do not have chartTestMethod key. You can solve it depending on where chartTestMethod is defined. If it is defined in global object, you can just remove this keyword. You can rewrite your code like this

function chartTestMethod(){
    console.log('chartTestMethod called.');
}

any = {
    animation: {
        duration: 2000,
        onComplete: function (){
            chartTestMethod();
        }
    },
    responsive: true
};

Also, if you want this method to be in the same object, you can do this

any = {
    animation: {
        duration: 2000,
        onComplete: function (){
            this.chartTestMethod();
        },
        chartTestMethod: function(){
            console.log('chartTestMethod called.');
      }
    },
    responsive: true
};

Comments

0
you can use bind(this) 

public chartOptions: any = {
    animation: {
        duration: 2000,
        onComplete: function () {
            //alert('anim complete');
            this.chartTestMethod();
        }.bind(this)
    },
    responsive: true
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.