I can reproduce the problem in a very simple Angular-Highcharts application. When I retrieve the data from the server, and then leave the page with Highcharts chart, I am getting this error:
Cannot read property 'forExport' of undefined at a.destroy (highcharts.js:394) at HighchartsChartComponent.ngOnDestroy (highcharts-angular.js:44)
Not surprisingly, HighchartsChartComponent.ngOnDestroy
is
ngOnDestroy() {
if (this.chart) { // #56
this.chart.destroy();
this.chart = null;
}
}
Here is the minimal example:
export class WeatherForecastComponent implements OnInit {
highcharts: typeof Highcharts = Highcharts;
chartOptions: Highcharts.Options = { ... };
constructor(private http: HttpClient) { }
ngOnInit(): void {
this.http.get<any[]>(`http://localhost:57432/WeatherForecast`, {
headers: new HttpHeaders({
"Content-Type": "application/json"
})})
.subscribe(result => {
let categories: string[] = [];
let dataSeries: number[] = [];
for (let i = 0; i < result.length; i++) {
categories.push(result[i]['location']);
dataSeries.push(result[i]['temperatureC']);
}
this.chartOptions.xAxis = {
categories: categories
};
this.chartOptions.series = [{
type: "column",
name: "Cities",
data: dataSeries
}];
this.highcharts.chart('container', this.chartOptions);
}, error => {
console.log(error);
});
}
}
I saw a number of similar issues both on Stack Overflow and on Github. I understand that the root cause is that it's trying delete the chart that is already deleted. But all of them are deleting (or attempting to delete) the charts in application code.
I tried to delete the chart in ngOnDestroy()
, or to unsubscribe - but no difference.
Here is the repo, including Server-side code.
Note, that the problem doesn't happen when it is a "hardcoded" chart.
Angular: 12.2; Highchars 9.2.2; Highcharts-Angular: 2.10.0