import type { MouseEvent } from 'react' ; import type { ChartType , ChartData , DefaultDataPoint , ChartDataset , ChartOptions , Chart , } from 'chart.js' ; import type { ForwardedRef } from './types' ; const defaultDatasetIdKey = 'label' ; export function reforwardRef < T > ( ref , value ) { if ( typeof ref === 'function' ) { ref ( value ) ; } else if ( ref ) { ref . current = value ; } } export function setOptions < TType extends ChartType = ChartType , TData = DefaultDataPoint < TType > , TLabel = unknown > ( chart , nextOptions ) { chart . options = { ... nextOptions } ; } export function setLabels < TType extends ChartType = ChartType , TData = DefaultDataPoint < TType > , TLabel = unknown > ( currentData , nextLabels ) { currentData . labels = nextLabels ; } export function setDatasets < TType extends ChartType = ChartType , TData = DefaultDataPoint < TType > , TLabel = unknown > ( currentData , nextDatasets , datasetIdKey = defaultDatasetIdKey ) { const addedDatasets = [ ] ; currentData . datasets = nextDatasets . map ( nextDataset => { const currentDataset = currentData . datasets . find ( dataset => dataset [ datasetIdKey ] === nextDataset [ datasetIdKey ] ) ; if ( ! currentDataset || ! nextDataset . data || addedDatasets . includes ( currentDataset ) ) { return { ... nextDataset } ; } addedDatasets . push ( currentDataset ) ; Object . assign ( currentDataset , nextDataset ) ; return currentDataset ; } ) ; } export function cloneData < TType extends ChartType = ChartType , TData = DefaultDataPoint < TType > , TLabel = unknown > ( data , datasetIdKey = defaultDatasetIdKey ) { const nextData = { labels : [ ] , datasets : [ ] , } ; setLabels ( nextData , data . labels ) ; setDatasets ( nextData , data . datasets , datasetIdKey ) ; return nextData ; } param chart param event returns export function getDatasetAtEvent ( chart , event ) { return chart . getElementsAtEventForMode ( event . nativeEvent , 'dataset' , { intersect : true } , false ) ; } param chart param event returns export function getElementAtEvent ( chart , event ) { return chart . getElementsAtEventForMode ( event . nativeEvent , 'nearest' , { intersect : true } , false ) ; } param chart param event returns export function getElementsAtEvent ( chart , event ) { return chart . getElementsAtEventForMode ( event . nativeEvent , 'index' , { intersect : true } , false ) ; }