@@ -9,7 +9,7 @@ import React, {
9
9
useRef ,
10
10
} from 'react'
11
11
12
- import Chart from 'chart.js/auto'
12
+ import Chart , { ChartData , ChartOptions , ChartType , InteractionItem , Plugin } from 'chart.js/auto'
13
13
import * as chartjs from 'chart.js'
14
14
import { customTooltips as cuiCustomTooltips } from '@coreui/chartjs'
15
15
@@ -31,7 +31,7 @@ export interface CChartProps extends HTMLAttributes<HTMLCanvasElement | HTMLDivE
31
31
/**
32
32
* The data object that is passed into the Chart.js chart (more info).
33
33
*/
34
- data : Chart . ChartData | ( ( canvas : HTMLCanvasElement ) => Chart . ChartData )
34
+ data : ChartData | ( ( canvas : HTMLCanvasElement ) => ChartData )
35
35
/**
36
36
* A fallback for when the canvas cannot be rendered. Can be used for accessible chart descriptions.
37
37
*
@@ -41,15 +41,24 @@ export interface CChartProps extends HTMLAttributes<HTMLCanvasElement | HTMLDivE
41
41
/**
42
42
* Proxy for Chart.js getDatasetAtEvent. Calls with dataset and triggering event.
43
43
*/
44
- getDatasetAtEvent ?: ( dataset : Array < { } > , event : React . MouseEvent < HTMLCanvasElement > ) => void
44
+ getDatasetAtEvent ?: (
45
+ dataset : InteractionItem [ ] ,
46
+ event : React . MouseEvent < HTMLCanvasElement > ,
47
+ ) => void
45
48
/**
46
49
* Proxy for Chart.js getElementAtEvent. Calls with single element array and triggering event.
47
50
*/
48
- getElementAtEvent ?: ( element : [ { } ] , event : React . MouseEvent < HTMLCanvasElement > ) => void
51
+ getElementAtEvent ?: (
52
+ element : InteractionItem [ ] ,
53
+ event : React . MouseEvent < HTMLCanvasElement > ,
54
+ ) => void
49
55
/**
50
56
* Proxy for Chart.js getElementsAtEvent. Calls with element array and triggering event.
51
57
*/
52
- getElementsAtEvent ?: ( elements : Array < { } > , event : React . MouseEvent < HTMLCanvasElement > ) => void
58
+ getElementsAtEvent ?: (
59
+ elements : InteractionItem [ ] ,
60
+ event : React . MouseEvent < HTMLCanvasElement > ,
61
+ ) => void
53
62
/**
54
63
* Height attribute applied to the rendered canvas.
55
64
*
@@ -65,13 +74,13 @@ export interface CChartProps extends HTMLAttributes<HTMLCanvasElement | HTMLDivE
65
74
*
66
75
* {@link https://www.chartjs.org/docs/latest/general/options.html More Info}
67
76
*/
68
- options ?: Chart . ChartOptions
77
+ options ?: ChartOptions
69
78
/**
70
79
* The plugins array that is passed into the Chart.js chart (more info)
71
80
*
72
81
* {@link https://www.chartjs.org/docs/latest/developers/plugins.html More Info}
73
82
*/
74
- plugins ?: Chart . PluginServiceRegistrationOptions [ ]
83
+ plugins ?: Plugin [ ]
75
84
/**
76
85
* If true, will tear down and redraw chart on all updates.
77
86
*
@@ -81,9 +90,9 @@ export interface CChartProps extends HTMLAttributes<HTMLCanvasElement | HTMLDivE
81
90
/**
82
91
* Chart.js chart type.
83
92
*
84
- * @type {'line' | 'bar' | 'horizontalBar' | ' radar' | 'doughnut' | 'polarArea' | 'bubble' | 'pie' | 'scatter' }
93
+ * @type {'line' | 'bar' | 'radar' | 'doughnut' | 'polarArea' | 'bubble' | 'pie' | 'scatter' }
85
94
*/
86
- type : Chart . ChartType
95
+ type : ChartType
87
96
/**
88
97
* Width attribute applied to the rendered canvas.
89
98
*
@@ -120,9 +129,9 @@ export const CChart = forwardRef<Chart | undefined, CChartProps>((props, ref) =>
120
129
121
130
const canvasRef = useRef < HTMLCanvasElement > ( null )
122
131
123
- const computedData = useMemo < Chart . ChartData > ( ( ) => {
132
+ const computedData = useMemo ( ( ) => {
124
133
if ( typeof data === 'function' ) {
125
- return canvasRef . current ? data ( canvasRef . current ) : { }
134
+ return canvasRef . current ? data ( canvasRef . current ) : { datasets : [ ] }
126
135
} else return merge ( { } , data )
127
136
} , [ data , canvasRef . current ] )
128
137
@@ -150,7 +159,7 @@ export const CChart = forwardRef<Chart | undefined, CChartProps>((props, ref) =>
150
159
)
151
160
}
152
161
153
- const onClick = ( e : React . MouseEvent < HTMLCanvasElement > ) => {
162
+ const handleOnClick = ( e : any ) => {
154
163
if ( ! chart ) return
155
164
156
165
getDatasetAtEvent &&
@@ -243,7 +252,9 @@ export const CChart = forwardRef<Chart | undefined, CChartProps>((props, ref) =>
243
252
width = { width }
244
253
ref = { ref }
245
254
id = { id }
246
- onClick = { onClick }
255
+ onClick = { ( e : React . MouseEvent < HTMLCanvasElement > ) => {
256
+ handleOnClick ( e )
257
+ } }
247
258
data-testid = "canvas"
248
259
role = "img"
249
260
>
@@ -264,27 +275,26 @@ export const CChart = forwardRef<Chart | undefined, CChartProps>((props, ref) =>
264
275
CChart . propTypes = {
265
276
className : PropTypes . string ,
266
277
customTooltips : PropTypes . bool ,
267
- data : PropTypes . oneOfType ( [ PropTypes . object , PropTypes . func ] ) , // TODO: check
278
+ data : PropTypes . any . isRequired , // TODO: check
268
279
fallbackContent : PropTypes . node ,
269
- getDatasetAtEvent : PropTypes . func , // TODO: check
270
- getElementAtEvent : PropTypes . func , // TODO: check
271
- getElementsAtEvent : PropTypes . func , // TODO: check
280
+ getDatasetAtEvent : PropTypes . func ,
281
+ getElementAtEvent : PropTypes . func ,
282
+ getElementsAtEvent : PropTypes . func ,
272
283
height : PropTypes . number ,
273
284
id : PropTypes . string ,
274
- options : PropTypes . object , // TODO: check
275
- plugins : PropTypes . array , // TODO: check
285
+ options : PropTypes . object ,
286
+ plugins : PropTypes . array ,
276
287
redraw : PropTypes . bool ,
277
- type : PropTypes . oneOf ( [
278
- 'line' ,
288
+ type : PropTypes . oneOf < ChartType > ( [
279
289
'bar' ,
280
- 'horizontalBar' ,
281
- 'radar' ,
282
- 'doughnut' ,
283
- 'polarArea' ,
290
+ 'line' ,
291
+ 'scatter' ,
284
292
'bubble' ,
285
293
'pie' ,
286
- 'scatter' ,
287
- ] ) ,
294
+ 'doughnut' ,
295
+ 'polarArea' ,
296
+ 'radar' ,
297
+ ] ) . isRequired ,
288
298
width : PropTypes . number ,
289
299
wrapper : PropTypes . bool ,
290
300
}
0 commit comments