( currentData: ChartData<TType, TData, TLabel>, nextDatasets: ChartDataset<TType, TData>[], datasetIdKey = defaultDatasetIdKey )
| 44 | } |
| 45 | |
| 46 | export function setDatasets< |
| 47 | TType extends ChartType = ChartType, |
| 48 | TData = DefaultDataPoint<TType>, |
| 49 | TLabel = unknown, |
| 50 | >( |
| 51 | currentData: ChartData<TType, TData, TLabel>, |
| 52 | nextDatasets: ChartDataset<TType, TData>[], |
| 53 | datasetIdKey = defaultDatasetIdKey |
| 54 | ) { |
| 55 | const addedDatasets: ChartDataset<TType, TData>[] = []; |
| 56 | |
| 57 | currentData.datasets = nextDatasets.map( |
| 58 | (nextDataset: Record<string, unknown>) => { |
| 59 | // given the new set, find it's current match |
| 60 | const currentDataset = currentData.datasets.find( |
| 61 | (dataset: Record<string, unknown>) => |
| 62 | dataset[datasetIdKey] === nextDataset[datasetIdKey] |
| 63 | ); |
| 64 | |
| 65 | // There is no original to update, so simply add new one |
| 66 | if ( |
| 67 | !currentDataset || |
| 68 | !nextDataset.data || |
| 69 | addedDatasets.includes(currentDataset) |
| 70 | ) { |
| 71 | return { ...nextDataset } as ChartDataset<TType, TData>; |
| 72 | } |
| 73 | |
| 74 | addedDatasets.push(currentDataset); |
| 75 | |
| 76 | Object.assign(currentDataset, nextDataset); |
| 77 | |
| 78 | return currentDataset; |
| 79 | } |
| 80 | ); |
| 81 | } |
| 82 | |
| 83 | export function cloneData< |
| 84 | TType extends ChartType = ChartType, |
no outgoing calls
no test coverage detected
searching dependent graphs…