fixed_michal
Posts: 9
Joined: Thu Nov 07, 2024 6:35 pm

Multiple Pie Charts Exporting error: Maximum call stack size exceeded

Hello,

I used your "export multiple charts hack" and it worked for bar chart.
Then I tried to use it on single pie chart ( I am planning to use it on two pie charts, but for now just to check if it even works I tried to use it on single pie chart).

But I get an error:
"ERROR RangeError: Maximum call stack size exceeded".

Here are some resources that could help you understand the problem:

CODE:
component.ts code
https://pastebin.com/vYzfDcre

template.html code:
https://pastebin.com/Vg7vZS1n

Error:
Screenshot from 2024-11-08 12-20-20.png
Screenshot from 2024-11-08 12-20-20.png (31.44 KiB) Viewed 1232 times
How my chart looks like:
Screenshot from 2024-11-08 12-21-07.png
Screenshot from 2024-11-08 12-21-07.png (25.9 KiB) Viewed 1232 times
Have a nice day :mrgreen:
jakub.s
Site Moderator
Posts: 1543
Joined: Fri Dec 16, 2022 11:45 am

Re: Multiple Pie Charts Exporting error: Maximum call stack size exceeded

Hi,

Welcome to our forum & thanks for the question!

My observation is the following:

An infinite recursion due to i never incrementing (likely due to a failure in getSVGForLocalExport) causes exportChart to keep calling itself on the same index.

Possible fix: check that getSVGForLocalExport calls the callback or handles errors properly, and ensure charts is not undefined or empty before calling exportChart.

If you reproduce this error in an online code editor (ideally in VanillaJS, in JSFiddle, starting from one of the demos listed here: https://www.highcharts.com/demo), I will gladly take a look and see what can be done to fix it.

Best regards!
Jakub
Highcharts Developer
fixed_michal
Posts: 9
Joined: Thu Nov 07, 2024 6:35 pm

Re: Multiple Pie Charts Exporting error: Maximum call stack size exceeded

Thanks for your reply.

I have kind of figured out what is the reason why I get this error.
It is because of the fact that I use function that adjust chart labels font size (depending on chart areas size) on "afterAnimate" event.
When I stop using this function, chart is properly exported.

Do you have any idea what to do to preserve this function and also make it errorless?

Code: Select all

private adjustLabelsFontSizes = function (this: Highcharts.Series) {
    this.points.forEach((point) => {
      if (point?.percentage) {
        let fontSize = 28;

        if (point?.percentage < 20) {
          fontSize = 22;
        }
        if (point?.percentage < 15) {
          fontSize = 18;
        }
        if (point?.percentage < 10) {
          fontSize = 15;
        }
        if (point?.percentage < 6) {
          fontSize = 13;
        }

        point.update({
          dataLabels: {
            style: {
              fontSize: `${fontSize}px`,
              color: 'contrast',
            },
          },
        });
      }
    });
  };

Code: Select all

  private generateSeries(
    banknotesAndCoinsSeriesConfig: CirculationSeriesConfig[],
    circulationData: CirculationPieChartSeriesData,
    config?: { center?: [string | number, string | number]; isDonut?: boolean }
  ): Highcharts.SeriesOptionsType {
    return config?.center
      ? {
          type: 'pie',
          events: {
            afterAnimate: this.adjustLabelsFontSizes,
          },
          center: config.center,
          data: banknotesAndCoinsSeriesConfig.map((series) => ({
            name: this.translateService.instant('banknotes_and_coins_page.circulation.series_names.' + series.key),
            y: circulationData[series.key]!,
            color: series.color,
          })),
        }
      : {
          type: 'pie',
          events: {
            afterAnimate: this.adjustLabelsFontSizes,
          },
          innerSize: config?.isDonut ? '30%' : '0',
          data: banknotesAndCoinsSeriesConfig.map((series) => ({
            name: this.translateService.instant('banknotes_and_coins_page.circulation.series_names.' + series.key),
            y: circulationData[series.key]!,
            color: series.color,
          })),
        };
  }
jakub.s
Site Moderator
Posts: 1543
Joined: Fri Dec 16, 2022 11:45 am

Re: Multiple Pie Charts Exporting error: Maximum call stack size exceeded

Hi @fixed_michal,

I've investigated your demo and it seems that the problem lies in the adjustLabelsFontSizes method.

Each point.update triggers a chart redraw. Each chart.redraw triggers the afterAnimate event which triggers adjustLabelsFontSizes. This becomes an infinite loop.

Please take a look at this demo, where we force adjustLabelsFontSizes to be called maximum once per series: https://jsfiddle.net/BlackLabel/5L8nhxay/ - the problem disappears.

As you have a static width & height of the chart, you could just use the load event instead of the afterAnimate as there's no need for that. You can take care of all of that in the chart.events (load/render).

Here's a demo: https://jsfiddle.net/BlackLabel/smva13ph/

Let me know if you have any more questions about this.

Best regards!
Jakub
Highcharts Developer
fixed_michal
Posts: 9
Joined: Thu Nov 07, 2024 6:35 pm

Re: Multiple Pie Charts Exporting error: Maximum call stack size exceeded

Thanks. But I dont quite understand where do you define field "_labelsAnimated". I see that you assign some value to it and also read its value, but there is no place where it is defined?
fixed_michal
Posts: 9
Joined: Thu Nov 07, 2024 6:35 pm

Re: Multiple Pie Charts Exporting error: Maximum call stack size exceeded

Ok, I defined it in load function of chart.
But I had to make it like this:
(this as any)._labelsAnimated = false;
Otherwise Typescript would not let me do it this way.

But now I get error:
Screenshot from 2024-11-21 14-48-46.png
Screenshot from 2024-11-21 14-48-46.png (55.63 KiB) Viewed 953 times
jakub.s
Site Moderator
Posts: 1543
Joined: Fri Dec 16, 2022 11:45 am

Re: Multiple Pie Charts Exporting error: Maximum call stack size exceeded

Hi @fixed_michal,

Yes, I've defined this _labelsAnimated property on the series just to demonstrate that this issue does not happen if we call this method only once. As after the first call I've set the custom flag _labelsAnimated to true, I made sure that this function does not execute for the second time. This was only to show that this particular method was the culprit.

You can get rid of this flag. And if you'd like to use any custom flag, you can always extend the standard Highcharts.Chart interface: https://api.highcharts.com/class-refere ... arts.Chart

And regarding your error, why is there Highcharts4 instead of Highcharts? Please try to reproduce that in JSFiddle so I can investigate.

Regards!
Jakub
Highcharts Developer
fixed_michal
Posts: 9
Joined: Thu Nov 07, 2024 6:35 pm

Re: Multiple Pie Charts Exporting error: Maximum call stack size exceeded

Ok, I made some changes and don't get this error anymore.

But get another one ( CORS ERROR):
CORS
CORS
Screenshot from 2024-11-26 03-15-22.png (60.38 KiB) Viewed 834 times
I guess that "post" method uses online exporting. I would like to export it locally. If it would be done localy, then this CORS error would not occur.
fixed_michal
Posts: 9
Joined: Thu Nov 07, 2024 6:35 pm

Re: Multiple Pie Charts Exporting error: Maximum call stack size exceeded

I am not sure why it is called Highcharts4. But maybe it is how Angular manages imports. Not sure.
jakub.s
Site Moderator
Posts: 1543
Joined: Fri Dec 16, 2022 11:45 am

Re: Multiple Pie Charts Exporting error: Maximum call stack size exceeded

Hi @fixed_michal,

The CORS issue is related to this reported issue: https://github.com/highcharts/node-expo ... issues/602

I would suggest you look into:
- Highcharts Offline Exporting: https://www.highcharts.com/docs/export- ... ide-export
- setting up your own Export Server: https://github.com/highcharts/node-expo ... ort-server

Best regards!
Jakub
Highcharts Developer
fixed_michal
Posts: 9
Joined: Thu Nov 07, 2024 6:35 pm

Re: Multiple Pie Charts Exporting error: Maximum call stack size exceeded

Thanks, but as I said Iam using workaround for exporting multiple charts, so default offline exporting API will not in such situation.

Is there a way to use workaround but with offline exporting? I have no idea how.
jakub.s
Site Moderator
Posts: 1543
Joined: Fri Dec 16, 2022 11:45 am

Re: Multiple Pie Charts Exporting error: Maximum call stack size exceeded

Hi,

Why not use the exportChartLocal method?

API: https://api.highcharts.com/class-refere ... ChartLocal

I believe that it would allow you to export multiple charts without having to worry about getting blocked.

Let me know once you try this method out.

Best regards!
Jakub
Highcharts Developer
fixed_michal
Posts: 9
Joined: Thu Nov 07, 2024 6:35 pm

Re: Multiple Pie Charts Exporting error: Maximum call stack size exceeded

Workaround uses some private methods of global Highcharts object, where you mentioned method of Highcharts.Chart object.

I tried to used method u just mentioned, in workaround, but failed.
jakub.s
Site Moderator
Posts: 1543
Joined: Fri Dec 16, 2022 11:45 am

Re: Multiple Pie Charts Exporting error: Maximum call stack size exceeded

Hi,

Please create a minimal reproducible example in JSFiddle where this happens so I can investigate.

Regards!
Jakub
Highcharts Developer

Return to “Highcharts Usage”