Chartjs-'this' in chartJS in Angular

1๐Ÿ‘

โœ…

Use arrow function to define toggleLegendClickHandler function It will point to component object

export class ChartComponent{
    
    legend: {onClick: this.toggleLegendClickHandler}
    
    toggleLegendClickHandler = ()=>{
    ...
    }
}

0๐Ÿ‘

You can use this shadow,
suppose this is your code,

chartFunction() {
     const _that = this;  // creating shadow of this

    createChart({
    data: {
            legend: {
              onClick: _that.toggleLegendClickHandler
            }
    }
    });

}

now it will refer the component class reference.

Leave a comment