MCPcopy Index your code
hub / github.com/reactchartjs/react-chartjs-2 / setDatasets

Function setDatasets

src/utils.ts:46–81  ·  view source on GitHub ↗
(
  currentData: ChartData<TType, TData, TLabel>,
  nextDatasets: ChartDataset<TType, TData>[],
  datasetIdKey = defaultDatasetIdKey
)

Source from the content-addressed store, hash-verified

44}
45
46export 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
83export function cloneData<
84 TType extends ChartType = ChartType,

Callers 4

cloneDataFunction · 0.85
ChartComponentFunction · 0.85
onAddFunction · 0.85
onRemoveFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…