11import { isPlatformBrowser } from "@angular/common" ;
22import {
33 afterEveryRender ,
4+ afterNextRender ,
45 AfterRenderRef ,
5- AfterViewInit ,
66 ChangeDetectionStrategy ,
77 Component ,
88 ElementRef ,
@@ -56,7 +56,7 @@ declare global {
5656 changeDetection : ChangeDetectionStrategy . OnPush ,
5757 standalone : true ,
5858} )
59- export class ChartComponent implements OnChanges , AfterViewInit , OnDestroy {
59+ export class ChartComponent implements OnChanges , OnDestroy {
6060 readonly chart = input < ApexChart > ( ) ;
6161 readonly annotations = input < ApexAnnotations > ( ) ;
6262 readonly colors = input < any [ ] > ( ) ;
@@ -105,14 +105,7 @@ export class ChartComponent implements OnChanges, AfterViewInit, OnDestroy {
105105 ngOnChanges ( changes : SimpleChanges ) : void {
106106 if ( ! this . isBrowser ) return ;
107107
108- if ( this . chartElement ( ) . nativeElement ) {
109- this . hydrate ( changes ) ;
110- }
111- }
112-
113- ngAfterViewInit ( ) {
114- if ( ! this . isBrowser ) return ;
115- this . createElement ( ) ;
108+ this . hydrate ( changes ) ;
116109 }
117110
118111 ngOnDestroy ( ) {
@@ -124,7 +117,12 @@ export class ChartComponent implements OnChanges, AfterViewInit, OnDestroy {
124117 private get isConnected ( ) {
125118 return this . chartElement ( ) ?. nativeElement . isConnected ;
126119 }
120+
127121 private hydrate ( changes : SimpleChanges ) : void {
122+ if ( this . waitingForConnectedRef ) {
123+ return ;
124+ }
125+
128126 const shouldUpdateSeries =
129127 this . chartInstance ( ) &&
130128 this . autoUpdateSeries ( ) &&
@@ -135,7 +133,10 @@ export class ChartComponent implements OnChanges, AfterViewInit, OnDestroy {
135133 return ;
136134 }
137135
138- this . createElement ( ) ;
136+ // Create the chart after the layout is finalized and ready to be measured.
137+ afterNextRender ( {
138+ read : ( ) => this . createElement ( ) ,
139+ } , { injector : this . _injector } ) ;
139140 }
140141
141142 private async createElement ( ) {
0 commit comments