11import {
22AfterViewInit ,
3+ ChangeDetectionStrategy ,
4+ ChangeDetectorRef ,
35Component ,
46ElementRef ,
57EventEmitter ,
@@ -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;
2830selector :'c-chart' ,
2931templateUrl :'./chartjs.component.html' ,
3032styleUrls :[ './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
100106constructor (
101107private elementRef :ElementRef ,
102108private ngZone :NgZone ,
103- private renderer :Renderer2
109+ private renderer :Renderer2 ,
110+ private changeDetectorRef :ChangeDetectorRef
104111) { }
105112
106113ngAfterViewInit ( ) :void {
107114this . chartRender ( ) ;
108- // this.chartUpdate();
109115}
110116
111117ngOnChanges ( changes :SimpleChanges ) :void {
@@ -118,8 +124,10 @@ export class ChartjsComponent<TType extends ChartType = ChartType, TData = Defau
118124this . 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
124132const datasetAtEvent = this . chart . getElementsAtEventForMode ( $event , 'dataset' , { intersect :true } , false ) ;
125133this . getDatasetAtEvent . emit ( datasetAtEvent ) ;
@@ -133,10 +141,13 @@ export class ChartjsComponent<TType extends ChartType = ChartType, TData = Defau
133141
134142public chartDestroy ( ) {
135143this . chart ?. destroy ( ) ;
144+ this . chartRef . emit ( undefined ) ;
136145}
137146
138147public chartRender ( ) {
139- if ( ! this . canvasElement ) return ;
148+ if ( ! this . canvasElement ) {
149+ return ;
150+ }
140151
141152const ctx :CanvasRenderingContext2D = this . canvasElement . nativeElement . getContext ( '2d' ) ;
142153
@@ -146,13 +157,17 @@ export class ChartjsComponent<TType extends ChartType = ChartType, TData = Defau
146157setTimeout ( ( ) => {
147158this . chart = new Chart ( ctx , config ) ;
148159this . renderer . setStyle ( this . canvasElement . nativeElement , 'display' , 'block' ) ;
160+ this . changeDetectorRef . markForCheck ( ) ;
161+ this . chartRef . emit ( this . chart ) ;
149162} ) ;
150163}
151164} ) ;
152165}
153166
154167chartUpdate ( ) {
155- if ( ! this . chart ) return ;
168+ if ( ! this . chart ) {
169+ return ;
170+ }
156171
157172if ( this . redraw ) {
158173this . chartDestroy ( ) ;
@@ -165,9 +180,7 @@ export class ChartjsComponent<TType extends ChartType = ChartType, TData = Defau
165180const config = this . chartConfig ( ) ;
166181
167182if ( 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
173186if ( ! this . chart . config . data ) {
@@ -176,12 +189,8 @@ export class ChartjsComponent<TType extends ChartType = ChartType, TData = Defau
176189}
177190
178191if ( 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 ?? [ ] ) ;
185194Object . assign ( this . chart . config . data , config . data ) ;
186195}
187196
@@ -192,6 +201,7 @@ export class ChartjsComponent<TType extends ChartType = ChartType, TData = Defau
192201setTimeout ( ( ) => {
193202this . ngZone . runOutsideAngular ( ( ) => {
194203this . chart ?. update ( ) ;
204+ this . changeDetectorRef . markForCheck ( ) ;
195205} ) ;
196206} ) ;
197207}