@mui/x-charts
Version:
The community edition of MUI X Charts components.
96 lines (94 loc) • 2.81 kB
JavaScript
'use client';
import _extends from "@babel/runtime/helpers/esm/extends";
import _formatErrorMessage from "@mui/x-internals/formatErrorMessage";
import { useEffectAfterFirstRender } from '@mui/x-internals/useEffectAfterFirstRender';
import { rainbowSurgePalette } from "../../../../colorPalettes/index.mjs";
import { defaultizeSeries } from "./processSeries.mjs";
export function createIdentifierWithType(state) {
function identifierWithType(identifier) {
if (identifier.type !== undefined) {
return identifier;
}
const type = state.series.idToType.get(identifier.seriesId);
if (type === undefined) {
throw new Error(process.env.NODE_ENV !== "production" ? `MUI X Charts: The id "${identifier.seriesId}" is not associated with any series. ` + 'This may indicate the series was not properly registered or the id is incorrect. ' + 'Verify the series id matches one defined in your chart configuration.' : _formatErrorMessage(36, identifier.seriesId));
}
return _extends({}, identifier, {
type
});
}
return identifierWithType;
}
export const useChartSeries = ({
params,
store
}) => {
const {
series,
dataset,
theme,
colors
} = params;
// The effect do not track any value defined synchronously during the 1st render by hooks called after `useChartSeries`
// As a consequence, the state generated by the 1st run of this useEffect will always be equal to the initialization one
useEffectAfterFirstRender(() => {
const {
defaultizedSeries,
idToType
} = defaultizeSeries({
series,
colors: typeof colors === 'function' ? colors(theme) : colors,
theme,
seriesConfig: store.state.seriesConfig.config
});
store.set('series', _extends({}, store.state.series, {
defaultizedSeries,
idToType,
dataset
}));
}, [colors, dataset, series, theme, store]);
const identifierWithType = createIdentifierWithType(store.state);
return {
instance: {
identifierWithType
}
};
};
useChartSeries.params = {
dataset: true,
series: true,
colors: true,
theme: true
};
const EMPTY_ARRAY = [];
useChartSeries.getDefaultizedParams = ({
params
}) => _extends({}, params, {
series: params.series?.length ? params.series : EMPTY_ARRAY,
colors: params.colors ?? rainbowSurgePalette,
theme: params.theme ?? 'light'
});
useChartSeries.getInitialState = ({
series = [],
colors,
theme,
dataset
}, currentState) => {
const seriesConfig = currentState.seriesConfig.config;
const {
defaultizedSeries,
idToType
} = defaultizeSeries({
series,
colors: typeof colors === 'function' ? colors(theme) : colors,
theme,
seriesConfig
});
return {
series: {
defaultizedSeries,
idToType,
dataset
}
};
};