3

MyHighChartComponent.ts

export class MyHighChartComponent {
    highchartsConfiguration: any = {
        chart: {
            events: {
                click(e) {
                    if (!($(event.target)[0].textContent)) {
                        console.log('clicked'); //this is printing
                        this.drillDown(); // how to call typescript function here?
                    }
                },
            },
        }
    };  

    drillDown() {
      console.log('drill down method called');
    }
}

How to call a typescript function from inside high charts click event?

I'm getting below error

Error Stack : TypeError: this.drillDownis not a function

5
  • first make sure that the code is valid js/ts :) Commented Jun 25, 2017 at 11:43
  • @toskv it's valid, but I have not posted the entire code since it is not required. Just wanted to know how to call drillDown method Commented Jun 25, 2017 at 11:52
  • the code you posted is not valid javascript code.. the click(e) {} is not valid js.. all you're missing is the use of an arrow function for the click handler that's all. Commented Jun 25, 2017 at 12:37
  • @toskv This would solve the problem for him, but not in all cases. I, for example, have the case that I need access to the unterlying chart object of highcharts, which is the 'this' context in this function when used without lambda. If I would use lamda now, I would loose access to the chart object, or at least, would have to get a reference myself. But if I use a function, I can't call any methods on my ts class object, because 'this' points to the chart object of highcharts. Commented Jul 17, 2018 at 10:37
  • @BenjaminJesuiter I updated the answer to fit your need as well. it's not that pretty but it should work. Commented Jul 17, 2018 at 11:03

1 Answer 1

4

You must use an arrow function to preserve the same context (this) in the click handler.

It would look like this:

export class MyHighChartComponent {
    highchartsConfiguration: any = {
        chart: {
            events: {
                click : (e) => {
                    if (!($(event.target)[0].textContent)) {
                        console.log('clicked'); //this is printing
                        this.drillDown(); // how to call typescript function here?
                    }
                },
            },
        }
    };  

    drillDown() {
      console.log('drill down method called');
    }
}

If you need access to both the chart context and the class context you can manually save the class context in a variable (the way you'd do it before arrow functions were a thing).

class MyHighChartComponent {
  public highchartsConfig() {
    var that = this; // store this in a variable to use later
    return {
      chart: {
        events: {
          click: function(e) {
            if (!($(event.target)[0].textContent)) {
              console.log('clicked');
              // this variable now stores the chart
              // call methods on class using the that variable
              that.drillDown();
            }
          },
        },
      }
    };

  }
  public drillDown() {}
}
Sign up to request clarification or add additional context in comments.

1 Comment

Is this works same as for column point click???

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.