Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
60
Bringing "this" into scope for the formatLabel function
posted

I'm trying to create a formatLabel function that can reach "this" in typescript.

This populates the labels with the correct date, but i need to reach a variable in the class to check the resolution so i can format the date accordingly.

formatLabel: function (value) {
   var d = new Date(value.Time);
   return d.getDate() + "/" + (d.getMonth()+1);
}

When i do this i successfully trigger the desired method and generate the correct string based on the resolution, but it fails to populate the labels in the chart.

formatLabel: ((value) => { this.formatSeriesLabel(value) })

formatSeriesLabel(value) {    

   switch (this.dataSource.Resolution) {
      case 7:
         var d = new Date(value.Time);
         return d.getHours().toString();
      case 20:
         var d = new Date(value.Time);
         return d.getDate() + "/" + (d.getMonth() + 1);
   }
}

Changing the method to just return a test string does not return properly either.

formatSeriesLabel(value){

   return "test";

}

Why isn't this populating the labels in the chart?

Parents
  • 845
    Offline posted

    Hello Kristoffer ,

    Thank you for contacting Infragistics Developer Support.

    The `this` in your formatSeriesLabel function does not point to the chart object itself.

    You should get a reference to the chart object/or the property of the chart inside the formatLabel function.

    For example your function may look like:

    formatLabel: function(value) {

      var resolution = $('.chart_selector').igDataChart('option', 'dataSource').Resolution;

      switch (resolution) {

        // code to follow

      }

    }

    Let me know if I may be of further assistance

Reply Children