11import {
22 AfterViewInit ,
3+ ChangeDetectionStrategy ,
4+ ChangeDetectorRef ,
35 Component ,
46 ElementRef ,
57 EventEmitter ,
@@ -17,7 +19,7 @@ import { BooleanInput, coerceBooleanProperty, coerceNumberProperty, NumberInput
1719
1820import merge from 'lodash-es/merge' ;
1921
20- import { Chart , ChartConfiguration , ChartOptions , ChartType , DefaultDataPoint , registerables } from 'chart.js' ;
22+ import { Chart , ChartConfiguration , ChartType , DefaultDataPoint , registerables } from 'chart.js' ;
2123import { customTooltips as cuiCustomTooltips } from '@coreui/chartjs' ;
2224
2325Chart . register ( ...registerables ) ;
@@ -28,7 +30,9 @@ let nextId = 0;
2830 selector : 'c-chart' ,
2931 templateUrl : './chartjs.component.html' ,
3032 styleUrls : [ './chartjs.component.scss' ] ,
31- exportAs : 'cChart'
33+ exportAs : 'cChart' ,
34+ standalone : true ,
35+ changeDetection : ChangeDetectionStrategy . OnPush
3236} )
3337export class ChartjsComponent < TType extends ChartType = ChartType , TData = DefaultDataPoint < TType > , TLabel = unknown > implements AfterViewInit , OnDestroy , OnChanges {
3438
@@ -82,9 +86,11 @@ export class ChartjsComponent<TType extends ChartType = ChartType, TData = Defau
8286
8387 @Input ( ) wrapper = true ;
8488
85- @Output ( ) getDatasetAtEvent = new EventEmitter < any > ( ) ;
86- @Output ( ) getElementAtEvent = new EventEmitter < any > ( ) ;
87- @Output ( ) getElementsAtEvent = new EventEmitter < any > ( ) ;
89+ @Output ( ) readonly getDatasetAtEvent = new EventEmitter < any > ( ) ;
90+ @Output ( ) readonly getElementAtEvent = new EventEmitter < any > ( ) ;
91+ @Output ( ) readonly getElementsAtEvent = new EventEmitter < any > ( ) ;
92+
93+ @Output ( ) readonly chartRef = new EventEmitter < any > ( ) ;
8894
8995 @ViewChild ( 'canvasElement' ) canvasElement ! : ElementRef ;
9096
@@ -100,12 +106,12 @@ export class ChartjsComponent<TType extends ChartType = ChartType, TData = Defau
100106 constructor (
101107 private elementRef : ElementRef ,
102108 private ngZone : NgZone ,
103- private renderer : Renderer2
109+ private renderer : Renderer2 ,
110+ private changeDetectorRef : ChangeDetectorRef
104111 ) { }
105112
106113 ngAfterViewInit ( ) : void {
107114 this . chartRender ( ) ;
108- // this.chartUpdate();
109115 }
110116
111117 ngOnChanges ( changes : SimpleChanges ) : void {
@@ -118,8 +124,10 @@ export class ChartjsComponent<TType extends ChartType = ChartType, TData = Defau
118124 this . chartDestroy ( ) ;
119125 }
120126
121- public handleOnClick ( $event : MouseEvent ) {
122- if ( ! this . chart ) return ;
127+ public handleClick ( $event : MouseEvent ) {
128+ if ( ! this . chart ) {
129+ return ;
130+ }
123131
124132 const datasetAtEvent = this . chart . getElementsAtEventForMode ( $event , 'dataset' , { intersect : true } , false ) ;
125133 this . getDatasetAtEvent . emit ( datasetAtEvent ) ;
@@ -133,10 +141,13 @@ export class ChartjsComponent<TType extends ChartType = ChartType, TData = Defau
133141
134142 public chartDestroy ( ) {
135143 this . chart ?. destroy ( ) ;
144+ this . chartRef . emit ( undefined ) ;
136145 }
137146
138147 public chartRender ( ) {
139- if ( ! this . canvasElement ) return ;
148+ if ( ! this . canvasElement ) {
149+ return ;
150+ }
140151
141152 const ctx : CanvasRenderingContext2D = this . canvasElement . nativeElement . getContext ( '2d' ) ;
142153
@@ -146,13 +157,17 @@ export class ChartjsComponent<TType extends ChartType = ChartType, TData = Defau
146157 setTimeout ( ( ) => {
147158 this . chart = new Chart ( ctx , config ) ;
148159 this . renderer . setStyle ( this . canvasElement . nativeElement , 'display' , 'block' ) ;
160+ this . changeDetectorRef . markForCheck ( ) ;
161+ this . chartRef . emit ( this . chart ) ;
149162 } ) ;
150163 }
151164 } ) ;
152165 }
153166
154167 chartUpdate ( ) {
155- if ( ! this . chart ) return ;
168+ if ( ! this . chart ) {
169+ return ;
170+ }
156171
157172 if ( this . redraw ) {
158173 this . chartDestroy ( ) ;
@@ -165,9 +180,7 @@ export class ChartjsComponent<TType extends ChartType = ChartType, TData = Defau
165180 const config = this . chartConfig ( ) ;
166181
167182 if ( this . options ) {
168- // todo
169- // @ts -ignore
170- Object . assign ( this . chart . options , config . options ) ;
183+ Object . assign ( this . chart . options ?? { } , config . options ?? { } ) ;
171184 }
172185
173186 if ( ! this . chart . config . data ) {
@@ -176,12 +189,8 @@ export class ChartjsComponent<TType extends ChartType = ChartType, TData = Defau
176189 }
177190
178191 if ( this . chart ) {
179- // todo
180- // @ts -ignore
181- Object . assign ( this . chart . config . options , config . options ) ;
182- // todo
183- // @ts -ignore
184- Object . assign ( this . chart . config . plugins , config . plugins ) ;
192+ Object . assign ( this . chart . config . options ?? { } , config . options ?? { } ) ;
193+ Object . assign ( this . chart . config . plugins ?? [ ] , config . plugins ?? [ ] ) ;
185194 Object . assign ( this . chart . config . data , config . data ) ;
186195 }
187196
@@ -192,6 +201,7 @@ export class ChartjsComponent<TType extends ChartType = ChartType, TData = Defau
192201 setTimeout ( ( ) => {
193202 this . ngZone . runOutsideAngular ( ( ) => {
194203 this . chart ?. update ( ) ;
204+ this . changeDetectorRef . markForCheck ( ) ;
195205 } ) ;
196206 } ) ;
197207 }
0 commit comments