I understand why this error is being thrown in dev mode and why.
(for those who don't yet: ExpressionChangedAfterItHasBeenCheckedError Explained)
What i don't understand is how i should fix it in my scenario.
I have a highchart rendering stacked columns and i'm overlaying the highchart with an absolute positioned label on top of the column. I do not know how tall those stacked columns going to be prior to the rendering as the value of the series is dynamic.
So i'm using the render
event of highcarts to fetch the height of each point in the series and align the label accordingly.
Those are the chart options:
chartOptions: Highcharts.Options = {
chart: {
type: 'column',
events: {
render: (e) => {
if ((e.target as any).series[0]) {
this.stackColumnHeight += ((e.target as any).series[0].points[0].shapeArgs.height as number);
this.stackColumnHeight += ((e.target as any).series[1].points[0].shapeArgs.height as number);
}
},
},
},
plotOptions: {
column: {
stacking: 'normal',
borderWidth: 0,
pointWidth: 60
}
},
legend: {
enabled: false
},
tooltip: {
enabled: false
},
series: [],
title: null,
xAxis: {
visible: false
},
yAxis: {
visible: false
}
};
I'm adding the data after some API fetch has completed:
ngAfterContentInit(): void {
this.service.getData().subscribe(
res => {
// chart series
const series = [{
data: [res],
type: 'column',
color: '#C2C2C2',
stack: 'other'
}, {
data: [9500],
type: 'column',
color: '#868686',
stack: 'other'
}, {
data: [9500],
type: 'column',
color: '#F0A619'
}];
for (const s of series) {
this.chartOptions.series.push(s as Highcharts.SeriesOptionsType);
}
this.updateChart = true;
},
err => console.log(err)
)
}
And this is how i've bound the position properties to the height being set in the render
event.
<div style="position: absolute; z-index: 1;left: 262px;"
[ngStyle]="{'bottom': stackColumnHeight + (somePadding) + 'px' }">
SOME LABEL<br>
(depending on the column height)
</div>
<highcharts-chart style="width: 60%; height: 320px; display: block;" [Highcharts]="Highcharts" [options]="chartOptions"
[(update)]="updateChart" [oneToOne]="true"></highcharts-chart>
stackColumnHeight
is 0 at the beginning and gets it value once the data fetch finishes and the chart re-renders, thus realigning the position of the label. This causes the ExpressionChangedAfterItHasBeenCheckedError
.
How can i get around this?