@@ -9,7 +9,7 @@ import React, {
99 useRef ,
1010} from 'react'
1111
12- import Chart from 'chart.js/auto'
12+ import Chart , { ChartData , ChartOptions , ChartType , InteractionItem , Plugin } from 'chart.js/auto'
1313import * as chartjs from 'chart.js'
1414import { customTooltips as cuiCustomTooltips } from '@coreui/chartjs'
1515
@@ -31,7 +31,7 @@ export interface CChartProps extends HTMLAttributes<HTMLCanvasElement | HTMLDivE
3131 /**
3232 * The data object that is passed into the Chart.js chart (more info).
3333 */
34- data : Chart . ChartData | ( ( canvas : HTMLCanvasElement ) => Chart . ChartData )
34+ data : ChartData | ( ( canvas : HTMLCanvasElement ) => ChartData )
3535 /**
3636 * A fallback for when the canvas cannot be rendered. Can be used for accessible chart descriptions.
3737 *
@@ -41,15 +41,24 @@ export interface CChartProps extends HTMLAttributes<HTMLCanvasElement | HTMLDivE
4141 /**
4242 * Proxy for Chart.js getDatasetAtEvent. Calls with dataset and triggering event.
4343 */
44- getDatasetAtEvent ?: ( dataset : Array < { } > , event : React . MouseEvent < HTMLCanvasElement > ) => void
44+ getDatasetAtEvent ?: (
45+ dataset : InteractionItem [ ] ,
46+ event : React . MouseEvent < HTMLCanvasElement > ,
47+ ) => void
4548 /**
4649 * Proxy for Chart.js getElementAtEvent. Calls with single element array and triggering event.
4750 */
48- getElementAtEvent ?: ( element : [ { } ] , event : React . MouseEvent < HTMLCanvasElement > ) => void
51+ getElementAtEvent ?: (
52+ element : InteractionItem [ ] ,
53+ event : React . MouseEvent < HTMLCanvasElement > ,
54+ ) => void
4955 /**
5056 * Proxy for Chart.js getElementsAtEvent. Calls with element array and triggering event.
5157 */
52- getElementsAtEvent ?: ( elements : Array < { } > , event : React . MouseEvent < HTMLCanvasElement > ) => void
58+ getElementsAtEvent ?: (
59+ elements : InteractionItem [ ] ,
60+ event : React . MouseEvent < HTMLCanvasElement > ,
61+ ) => void
5362 /**
5463 * Height attribute applied to the rendered canvas.
5564 *
@@ -65,13 +74,13 @@ export interface CChartProps extends HTMLAttributes<HTMLCanvasElement | HTMLDivE
6574 *
6675 * {@link https://www.chartjs.org/docs/latest/general/options.html More Info}
6776 */
68- options ?: Chart . ChartOptions
77+ options ?: ChartOptions
6978 /**
7079 * The plugins array that is passed into the Chart.js chart (more info)
7180 *
7281 * {@link https://www.chartjs.org/docs/latest/developers/plugins.html More Info}
7382 */
74- plugins ?: Chart . PluginServiceRegistrationOptions [ ]
83+ plugins ?: Plugin [ ]
7584 /**
7685 * If true, will tear down and redraw chart on all updates.
7786 *
@@ -81,9 +90,9 @@ export interface CChartProps extends HTMLAttributes<HTMLCanvasElement | HTMLDivE
8190 /**
8291 * Chart.js chart type.
8392 *
84- * @type {'line' | 'bar' | 'horizontalBar' | ' radar' | 'doughnut' | 'polarArea' | 'bubble' | 'pie' | 'scatter' }
93+ * @type {'line' | 'bar' | 'radar' | 'doughnut' | 'polarArea' | 'bubble' | 'pie' | 'scatter' }
8594 */
86- type : Chart . ChartType
95+ type : ChartType
8796 /**
8897 * Width attribute applied to the rendered canvas.
8998 *
@@ -120,9 +129,9 @@ export const CChart = forwardRef<Chart | undefined, CChartProps>((props, ref) =>
120129
121130 const canvasRef = useRef < HTMLCanvasElement > ( null )
122131
123- const computedData = useMemo < Chart . ChartData > ( ( ) => {
132+ const computedData = useMemo ( ( ) => {
124133 if ( typeof data === 'function' ) {
125- return canvasRef . current ? data ( canvasRef . current ) : { }
134+ return canvasRef . current ? data ( canvasRef . current ) : { datasets : [ ] }
126135 } else return merge ( { } , data )
127136 } , [ data , canvasRef . current ] )
128137
@@ -150,7 +159,7 @@ export const CChart = forwardRef<Chart | undefined, CChartProps>((props, ref) =>
150159 )
151160 }
152161
153- const onClick = ( e : React . MouseEvent < HTMLCanvasElement > ) => {
162+ const handleOnClick = ( e : any ) => {
154163 if ( ! chart ) return
155164
156165 getDatasetAtEvent &&
@@ -243,7 +252,9 @@ export const CChart = forwardRef<Chart | undefined, CChartProps>((props, ref) =>
243252 width = { width }
244253 ref = { ref }
245254 id = { id }
246- onClick = { onClick }
255+ onClick = { ( e : React . MouseEvent < HTMLCanvasElement > ) => {
256+ handleOnClick ( e )
257+ } }
247258 data-testid = "canvas"
248259 role = "img"
249260 >
@@ -264,27 +275,26 @@ export const CChart = forwardRef<Chart | undefined, CChartProps>((props, ref) =>
264275CChart . propTypes = {
265276 className : PropTypes . string ,
266277 customTooltips : PropTypes . bool ,
267- data : PropTypes . oneOfType ( [ PropTypes . object , PropTypes . func ] ) , // TODO: check
278+ data : PropTypes . any . isRequired , // TODO: check
268279 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 ,
272283 height : PropTypes . number ,
273284 id : PropTypes . string ,
274- options : PropTypes . object , // TODO: check
275- plugins : PropTypes . array , // TODO: check
285+ options : PropTypes . object ,
286+ plugins : PropTypes . array ,
276287 redraw : PropTypes . bool ,
277- type : PropTypes . oneOf ( [
278- 'line' ,
288+ type : PropTypes . oneOf < ChartType > ( [
279289 'bar' ,
280- 'horizontalBar' ,
281- 'radar' ,
282- 'doughnut' ,
283- 'polarArea' ,
290+ 'line' ,
291+ 'scatter' ,
284292 'bubble' ,
285293 'pie' ,
286- 'scatter' ,
287- ] ) ,
294+ 'doughnut' ,
295+ 'polarArea' ,
296+ 'radar' ,
297+ ] ) . isRequired ,
288298 width : PropTypes . number ,
289299 wrapper : PropTypes . bool ,
290300}
0 commit comments