Chartjs-How to use ChartJS object in a click event handler?

0👍

First, make sure it’s really global:

console.log(window.myChart);

1: Try to pass it as a param somehow (if possible):

pie.on('click', function(params, myChart) {
    console.log(myChart);
});

2: Try ES6 arrow notation, to be able to access the scope outside of the function block:

pie.on('click', (params) => {
    console.log(window.myChart);
});

3: Map it to this, and access it in the function block:

this.myChart = myChart || window.myChart;
pie.on('click', (params) => {
    console.log(this.myChart);
});

Leave a comment