@@ -17,10 +17,10 @@ import { BooleanInput, coerceBooleanProperty, coerceNumberProperty, NumberInput
17
17
18
18
import { merge } from 'lodash' ;
19
19
20
- import Chart , { ChartData , ChartOptions , ChartType , Plugin } from 'chart.js/auto' ;
21
-
20
+ import { Chart , ChartConfiguration , ChartType , DefaultDataPoint , registerables } from 'chart.js' ;
22
21
import { customTooltips as cuiCustomTooltips } from '@coreui/chartjs' ;
23
- import { IChartjs } from './chartjs.interface' ;
22
+
23
+ Chart . register ( ...registerables ) ;
24
24
25
25
let nextId = 0 ;
26
26
@@ -30,48 +30,54 @@ let nextId = 0;
30
30
styleUrls : [ './chartjs.component.scss' ] ,
31
31
exportAs : 'cChart'
32
32
} )
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 {
34
34
35
35
static ngAcceptInputType_height : NumberInput ;
36
36
static ngAcceptInputType_width : NumberInput ;
37
37
static ngAcceptInputType_redraw : BooleanInput ;
38
38
39
39
@Input ( ) customTooltips = true ;
40
- @Input ( ) data : ChartData | ( ( canvas : HTMLCanvasElement ) => ChartData ) | undefined ;
40
+ @Input ( ) data ?: ChartConfiguration < TType , TData , TLabel > [ 'data' ] ;
41
41
42
42
@HostBinding ( 'style.height.px' )
43
43
@Input ( )
44
- set height ( value : number ) {
44
+ set height ( value : number | undefined ) {
45
45
this . _height = coerceNumberProperty ( value ) ;
46
46
}
47
+
47
48
get height ( ) {
48
49
return this . _height ;
49
50
}
50
- private _height = 150 ;
51
+
52
+ private _height : number | undefined ;
51
53
52
54
@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' ] = [ ] ;
55
57
56
58
@Input ( )
57
59
set redraw ( value : boolean ) {
58
60
this . _redraw = coerceBooleanProperty ( value ) ;
59
61
}
62
+
60
63
get redraw ( ) : boolean {
61
64
return this . _redraw ;
62
65
}
66
+
63
67
private _redraw = false ;
64
68
65
- @Input ( ) type : ChartType = 'bar' ;
69
+ @Input ( ) type : ChartConfiguration < TType , TData , TLabel > [ 'type' ] = 'bar' as TType ;
66
70
67
71
@HostBinding ( 'style.width.px' )
68
72
@Input ( )
69
73
set width ( value : number | undefined ) {
70
74
this . _width = coerceNumberProperty ( value ) ;
71
75
}
76
+
72
77
get width ( ) {
73
78
return this . _width ;
74
79
}
80
+
75
81
private _width : number | undefined ;
76
82
77
83
@Input ( ) wrapper = true ;
@@ -82,7 +88,7 @@ export class ChartjsComponent implements IChartjs, AfterViewInit, OnDestroy, OnC
82
88
83
89
@ViewChild ( 'canvasElement' ) canvasElement ! : ElementRef ;
84
90
85
- chart : Chart | undefined ;
91
+ chart ! : Chart < TType , TData , TLabel > ;
86
92
87
93
@HostBinding ( 'class' )
88
94
get hostClasses ( ) {
@@ -91,15 +97,6 @@ export class ChartjsComponent implements IChartjs, AfterViewInit, OnDestroy, OnC
91
97
} ;
92
98
}
93
99
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
-
103
100
constructor (
104
101
private elementRef : ElementRef ,
105
102
private ngZone : NgZone ,
@@ -141,31 +138,14 @@ export class ChartjsComponent implements IChartjs, AfterViewInit, OnDestroy, OnC
141
138
public chartRender ( ) {
142
139
if ( ! this . canvasElement ) return ;
143
140
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
-
161
141
const ctx : CanvasRenderingContext2D = this . canvasElement . nativeElement . getContext ( '2d' ) ;
162
142
163
143
this . ngZone . runOutsideAngular ( ( ) => {
164
144
const config = this . chartConfig ( ) ;
165
145
if ( config ) {
166
146
this . chart = new Chart ( ctx , config ) ;
167
147
setTimeout ( ( ) => {
168
- this . renderer . setStyle ( this . canvasElement . nativeElement , 'display' , 'block' ) ;
148
+ this . renderer . setStyle ( this . canvasElement . nativeElement , 'display' , 'block' ) ;
169
149
} ) ;
170
150
}
171
151
} ) ;
@@ -182,7 +162,6 @@ export class ChartjsComponent implements IChartjs, AfterViewInit, OnDestroy, OnC
182
162
return ;
183
163
}
184
164
185
-
186
165
const config = this . chartConfig ( ) ;
187
166
188
167
if ( this . options ) {
@@ -215,39 +194,47 @@ export class ChartjsComponent implements IChartjs, AfterViewInit, OnDestroy, OnC
215
194
return this . chart ?. toBase64Image ( ) ;
216
195
}
217
196
218
- private chartDataConfig ( ) {
197
+ private chartDataConfig ( ) : ChartConfiguration < TType , TData , TLabel > [ 'data' ] {
219
198
return {
220
- labels : this . chartData ?. labels ?? [ ] ,
221
- datasets : [ ... this . chartData ?. datasets ] ?? [ ]
199
+ labels : this . data ?. labels ?? [ ] ,
200
+ datasets : this . data ?. datasets ?? [ ]
222
201
} ;
223
202
}
224
203
225
- private chartConfig ( ) {
204
+ private chartOptions ( ) : ChartConfiguration < TType , TData , TLabel > [ 'options' ] {
205
+ return this . options ;
206
+ }
207
+
208
+ private chartConfig ( ) : ChartConfiguration < TType , TData , TLabel > {
226
209
this . chartCustomTooltips ( ) ;
227
210
return {
228
211
data : this . chartDataConfig ( ) ,
229
- options : this . options as ChartOptions ,
212
+ options : this . chartOptions ( ) ,
230
213
plugins : this . plugins ,
231
214
type : this . type
232
215
} ;
233
216
}
234
217
235
218
private chartCustomTooltips ( ) {
236
219
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 ( {
239
226
...options ,
240
227
plugins : {
241
- ...options ?. plugins ,
228
+ ...plugins ,
242
229
tooltip : {
243
- ...options ?. plugins ?. tooltip ,
230
+ ...tooltip ,
244
231
enabled : false ,
245
232
mode : 'index' ,
246
233
position : 'nearest' ,
247
234
external : cuiCustomTooltips
248
235
}
249
236
}
250
- } ;
251
- } ;
237
+ } ) ;
238
+ }
252
239
} ;
253
240
}
0 commit comments