@@ -17,10 +17,10 @@ import { BooleanInput, coerceBooleanProperty, coerceNumberProperty, NumberInput
1717
1818import { merge } from 'lodash' ;
1919
20- import Chart , { ChartData , ChartOptions , ChartType , Plugin } from 'chart.js/auto' ;
21-
20+ import { Chart , ChartConfiguration , ChartType , DefaultDataPoint , registerables } from 'chart.js' ;
2221import { customTooltips as cuiCustomTooltips } from '@coreui/chartjs' ;
23- import { IChartjs } from './chartjs.interface' ;
22+
23+ Chart . register ( ...registerables ) ;
2424
2525let nextId = 0 ;
2626
@@ -30,48 +30,54 @@ let nextId = 0;
3030 styleUrls : [ './chartjs.component.scss' ] ,
3131 exportAs : 'cChart'
3232} )
33- export class ChartjsComponent implements IChartjs , AfterViewInit , OnDestroy , OnChanges {
33+ export class ChartjsComponent < TType extends ChartType = ChartType , TData = DefaultDataPoint < TType > , TLabel = unknown > implements AfterViewInit , OnDestroy , OnChanges {
3434
3535 static ngAcceptInputType_height : NumberInput ;
3636 static ngAcceptInputType_width : NumberInput ;
3737 static ngAcceptInputType_redraw : BooleanInput ;
3838
3939 @Input ( ) customTooltips = true ;
40- @Input ( ) data : ChartData | ( ( canvas : HTMLCanvasElement ) => ChartData ) | undefined ;
40+ @Input ( ) data ?: ChartConfiguration < TType , TData , TLabel > [ 'data' ] ;
4141
4242 @HostBinding ( 'style.height.px' )
4343 @Input ( )
44- set height ( value : number ) {
44+ set height ( value : number | undefined ) {
4545 this . _height = coerceNumberProperty ( value ) ;
4646 }
47+
4748 get height ( ) {
4849 return this . _height ;
4950 }
50- private _height = 150 ;
51+
52+ private _height : number | undefined ;
5153
5254 @Input ( ) id = `c-chartjs-${ nextId ++ } ` ;
53- @Input ( ) options ?: ChartOptions ;
54- @Input ( ) plugins : Plugin [ ] = [ ] ;
55+ @Input ( ) options ?: ChartConfiguration < TType , TData , TLabel > [ 'options' ] ;
56+ @Input ( ) plugins : ChartConfiguration < TType , TData , TLabel > [ 'plugins' ] = [ ] ;
5557
5658 @Input ( )
5759 set redraw ( value : boolean ) {
5860 this . _redraw = coerceBooleanProperty ( value ) ;
5961 }
62+
6063 get redraw ( ) : boolean {
6164 return this . _redraw ;
6265 }
66+
6367 private _redraw = false ;
6468
65- @Input ( ) type : ChartType = 'bar' ;
69+ @Input ( ) type : ChartConfiguration < TType , TData , TLabel > [ 'type' ] = 'bar' as TType ;
6670
6771 @HostBinding ( 'style.width.px' )
6872 @Input ( )
6973 set width ( value : number | undefined ) {
7074 this . _width = coerceNumberProperty ( value ) ;
7175 }
76+
7277 get width ( ) {
7378 return this . _width ;
7479 }
80+
7581 private _width : number | undefined ;
7682
7783 @Input ( ) wrapper = true ;
@@ -82,7 +88,7 @@ export class ChartjsComponent implements IChartjs, AfterViewInit, OnDestroy, OnC
8288
8389 @ViewChild ( 'canvasElement' ) canvasElement ! : ElementRef ;
8490
85- chart : Chart | undefined ;
91+ chart ! : Chart < TType , TData , TLabel > ;
8692
8793 @HostBinding ( 'class' )
8894 get hostClasses ( ) {
@@ -91,15 +97,6 @@ export class ChartjsComponent implements IChartjs, AfterViewInit, OnDestroy, OnC
9197 } ;
9298 }
9399
94- get chartData ( ) {
95- const canvasRef = this . canvasElement . nativeElement ;
96- return typeof this . data === 'function'
97- ? canvasRef . value
98- ? this . data ( canvasRef . value )
99- : { labels : [ ] , datasets : [ ] }
100- : merge ( { labels : [ ] , datasets : [ ] } , { ...this . data } ) ;
101- }
102-
103100 constructor (
104101 private elementRef : ElementRef ,
105102 private ngZone : NgZone ,
@@ -141,31 +138,14 @@ export class ChartjsComponent implements IChartjs, AfterViewInit, OnDestroy, OnC
141138 public chartRender ( ) {
142139 if ( ! this . canvasElement ) return ;
143140
144- if ( this . customTooltips ) {
145- const options = this . options
146- this . options = {
147- ...options ,
148- plugins : {
149- ...options ?. plugins ,
150- tooltip : {
151- ...options ?. plugins ?. tooltip ,
152- enabled : false ,
153- mode : 'index' ,
154- position : 'nearest' ,
155- external : cuiCustomTooltips
156- }
157- }
158- } ;
159- }
160-
161141 const ctx : CanvasRenderingContext2D = this . canvasElement . nativeElement . getContext ( '2d' ) ;
162142
163143 this . ngZone . runOutsideAngular ( ( ) => {
164144 const config = this . chartConfig ( ) ;
165145 if ( config ) {
166146 this . chart = new Chart ( ctx , config ) ;
167147 setTimeout ( ( ) => {
168- this . renderer . setStyle ( this . canvasElement . nativeElement , 'display' , 'block' ) ;
148+ this . renderer . setStyle ( this . canvasElement . nativeElement , 'display' , 'block' ) ;
169149 } ) ;
170150 }
171151 } ) ;
@@ -182,7 +162,6 @@ export class ChartjsComponent implements IChartjs, AfterViewInit, OnDestroy, OnC
182162 return ;
183163 }
184164
185-
186165 const config = this . chartConfig ( ) ;
187166
188167 if ( this . options ) {
@@ -215,39 +194,47 @@ export class ChartjsComponent implements IChartjs, AfterViewInit, OnDestroy, OnC
215194 return this . chart ?. toBase64Image ( ) ;
216195 }
217196
218- private chartDataConfig ( ) {
197+ private chartDataConfig ( ) : ChartConfiguration < TType , TData , TLabel > [ 'data' ] {
219198 return {
220- labels : this . chartData ?. labels ?? [ ] ,
221- datasets : [ ... this . chartData ?. datasets ] ?? [ ]
199+ labels : this . data ?. labels ?? [ ] ,
200+ datasets : this . data ?. datasets ?? [ ]
222201 } ;
223202 }
224203
225- private chartConfig ( ) {
204+ private chartOptions ( ) : ChartConfiguration < TType , TData , TLabel > [ 'options' ] {
205+ return this . options ;
206+ }
207+
208+ private chartConfig ( ) : ChartConfiguration < TType , TData , TLabel > {
226209 this . chartCustomTooltips ( ) ;
227210 return {
228211 data : this . chartDataConfig ( ) ,
229- options : this . options as ChartOptions ,
212+ options : this . chartOptions ( ) ,
230213 plugins : this . plugins ,
231214 type : this . type
232215 } ;
233216 }
234217
235218 private chartCustomTooltips ( ) {
236219 if ( this . customTooltips ) {
237- const options = this . options
238- this . options = {
220+ const options = this . options ;
221+ // @ts -ignore
222+ const plugins = this . options ?. plugins ;
223+ // @ts -ignore
224+ const tooltip = this . options ?. plugins ?. tooltip ;
225+ this . options = merge ( {
239226 ...options ,
240227 plugins : {
241- ...options ?. plugins ,
228+ ...plugins ,
242229 tooltip : {
243- ...options ?. plugins ?. tooltip ,
230+ ...tooltip ,
244231 enabled : false ,
245232 mode : 'index' ,
246233 position : 'nearest' ,
247234 external : cuiCustomTooltips
248235 }
249236 }
250- } ;
251- } ;
237+ } ) ;
238+ }
252239 } ;
253240}
0 commit comments