skone
Posts: 18
Joined: Mon Apr 19, 2021 2:41 pm

Highchart using with Angular and `this`

Hi,

I am using highcharts with Angular. The functions that are provided by highcharts use "this" allowing us to access the context of the charts. Since Angular uses Typescript, you can imagine that this conflicts quite a bit because classes are used all over Angular and the "this" keyword is heavily used to access members of a given class. If the class contains a chart then it becomes difficult to use the class properties and members within the highchart function. For example consider this simple example. I am not able to use the "this.getText()" function from the class itself as "this" points to the context of the highchart function. How do I get around this?

Code: Select all

import { Component, Input } from '@angular/core';
import * as Highcharts from 'highcharts';
import { SeriesPieOptions } from 'highcharts';

@Component({
  selector: 'pie-chart',
  templateUrl: './pie-chart.component.html'
})
export class PieChartComponent {
  Highcharts: typeof Highcharts = Highcharts; // required
  chartOptions: Highcharts.Options = {
    title: {
      text: null,
    },
    chart: {
      type: 'pie',
      events: {
        render: function () {
          // eslint-disable-next-line @typescript-eslint/no-this-alias
          const chart = this as any;
          const textX = chart.plotLeft + chart.series[0].center[0];
          const textY = chart.plotTop + chart.series[0].center[1];

          if (chart.centerText) chart.centerText.destroy();
          
          // I can not use this here, as it points to the highchart context and not the class object. :( 
          const text = this.getText();  // <---------------------------------------------- Can not used this --------------------------------------

          chart.centerText = chart.renderer
            .text('21', textX, textY)
            .css({
              color: 'teal',
              fontSize: 28,
              fontWeight: 'bold',
            })
            .add();

          chart.centerText.attr({
            x: textX - chart.centerText.getBBox().width / 2,
          });
        },
      },
    },
  };

  private getCount () {
    return '100'
  };
}
supremeauron
Posts: 9
Joined: Mon Oct 25, 2021 2:19 pm

Re: Highchart using with Angular and `this`

I'm having the same problem
michal.f
Posts: 1114
Joined: Thu Aug 12, 2021 12:04 pm

Re: Highchart using with Angular and `this`

Hello,

Thanks for contacting us with your question!

To around this you can change context using arrow function instead of regular function.
Demo: https://stackblitz.com/edit/highcharts- ... ine-qvmedf

Let me know if that was what you were looking for!
Best regards!
Michał Filipiec
Highcharts Developer
https://blacklabel.pl/highcharts/
supremeauron
Posts: 9
Joined: Mon Oct 25, 2021 2:19 pm

Re: Highchart using with Angular and `this`

Hello Michał,
thanks for your reply.

Unfortunately, you example doesn't fit my case.

I'm trying to set the "formatter" function to format the numbers over a Column chart.

By investigating the declarations, I saw that it expects to get a "DataLabelsFormatterCallbackFunction" but if I create it, the first mandatory param must be called "this".
The problem is that by using "this" in this way, I loose the possibility to point at any other component/service of my solution. As the chart is not the only place in which I have to format a number, I have to point to the proper service but I cannot do that since the "this" pointer of the component is no more usable (or I don't know how to reach it).

Example: https://stackblitz.com/edit/highcharts- ... ine-knxk1g

To emulate the service calling, I created another function named "formatNumber": to fit a normal usage, we have to find a way to get access to the Y value (so this.y) and to be able to call the "formatNumber" function (ideally: this.formatNumber(this.y) but that it's not possible of course).

Look forward for you replay and thanks for your time
michal.f
Posts: 1114
Joined: Thu Aug 12, 2021 12:04 pm

Re: Highchart using with Angular and `this`

Hi Marco,

I'm not sure if I understand you well. Try to do it like in this demo: https://stackblitz.com/edit/highcharts- ... ine-uko5ud

And let me know if that was what you were looking for!
Best regards!
Michał Filipiec
Highcharts Developer
https://blacklabel.pl/highcharts/
supremeauron
Posts: 9
Joined: Mon Oct 25, 2021 2:19 pm

Re: Highchart using with Angular and `this`

Hello Michał,
into your demo you're not solving the problem.

You just put under comment the component function in charge of formatting the numbers over the chart columns by replacing it with a function that sets a fixed string over all the bars.. this is not a real case of usage.

Problem:
it's not possible to refer to any component function from within the formatter function because the context of the "this" pointer is overwritten by the "formatter" function.

Problem Example:
Use this https://stackblitz.com/edit/highcharts- ... ine-h4bz11 and put within the function "formatNumberFromServiceEmulator" the logic to format the "Y values" on the top of the bars: ex: if(value > 1000000) return (value/1000000) + 'M'; if(value > 1000) return (value/1000) + 'K' .

Into any standard solution, you have a formatter function for the numbers placed somewhere and you'll use it for any number that you have to format (so it could be for a simple label or for the Highcharts Series data).

Since I'm using Angular and Typescript, I have a service in charge of formatting any values and I want to use it for formatting the values on the top of the Bars but I cannot do that because Highcharts overwrites the "this" pointer with its own context and then you cannot refer to any other component/service/etc of your solution. Into the example that I provided you, I cannot refers to the "formatNumberFromServiceEmulator" which is a component function .

Can you please tell me how to resolve this problem?

Best regards,
Marco
michal.f
Posts: 1114
Joined: Thu Aug 12, 2021 12:04 pm

Re: Highchart using with Angular and `this`

Hi Marco,

It's best if you somehow assign the context of the App Component class to a variable that you can refer to.

Demo: https://stackblitz.com/edit/highcharts- ... ine-hffsdd

Best regards!
Michał Filipiec
Highcharts Developer
https://blacklabel.pl/highcharts/
skone
Posts: 18
Joined: Mon Apr 19, 2021 2:41 pm

Re: Highchart using with Angular and `this`

While all of this is fine, are there plans to rename `this` to something like `context`? That would solve all issues.
michal.f
Posts: 1114
Joined: Thu Aug 12, 2021 12:04 pm

Re: Highchart using with Angular and `this`

Hello Marco,

We are not planning it. This is more JavaScript issues related to 'this'.

Feel free to ask any further questions!
Best regards!
Michał Filipiec
Highcharts Developer
https://blacklabel.pl/highcharts/
skone
Posts: 18
Joined: Mon Apr 19, 2021 2:41 pm

Re: Highchart using with Angular and `this`

IMO, using `this` on its own can sometimes be very confusing. When working with Typescript and angular, `this` refers to the class instance. Would it be possible to provide a different name for the Angular library? We are talking about renaming the variable from `this` to something that relates better to the incoming argument. Not only will this be much more clear, it also gives a much better developer experience when using Highcharts. Please reconsider changing the variable name from `this` to something else like `context`. Thanks
michal.f
Posts: 1114
Joined: Thu Aug 12, 2021 12:04 pm

Re: Highchart using with Angular and `this`

Hi Marco,

You can create a ticket on Highcharts wrapper Angular repo (https://github.com/highcharts/highcharts-angular) with this proposal. Someone involved in development should let you know.

Best regards!
Michał Filipiec
Highcharts Developer
https://blacklabel.pl/highcharts/

Return to “Highcharts Usage”