@mui/x-charts
Version:
The community edition of MUI X Charts components.
104 lines (102 loc) • 3.27 kB
JavaScript
;
'use client';
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.createIdentifierWithType = createIdentifierWithType;
exports.useChartSeries = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _formatErrorMessage2 = _interopRequireDefault(require("@mui/x-internals/formatErrorMessage"));
var _useEffectAfterFirstRender = require("@mui/x-internals/useEffectAfterFirstRender");
var _colorPalettes = require("../../../../colorPalettes");
var _processSeries = require("./processSeries");
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.' : (0, _formatErrorMessage2.default)(36, identifier.seriesId));
}
return (0, _extends2.default)({}, identifier, {
type
});
}
return identifierWithType;
}
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
(0, _useEffectAfterFirstRender.useEffectAfterFirstRender)(() => {
const {
defaultizedSeries,
idToType
} = (0, _processSeries.defaultizeSeries)({
series,
colors: typeof colors === 'function' ? colors(theme) : colors,
theme,
seriesConfig: store.state.seriesConfig.config
});
store.set('series', (0, _extends2.default)({}, store.state.series, {
defaultizedSeries,
idToType,
dataset
}));
}, [colors, dataset, series, theme, store]);
const identifierWithType = createIdentifierWithType(store.state);
return {
instance: {
identifierWithType
}
};
};
exports.useChartSeries = useChartSeries;
useChartSeries.params = {
dataset: true,
series: true,
colors: true,
theme: true
};
const EMPTY_ARRAY = [];
useChartSeries.getDefaultizedParams = ({
params
}) => (0, _extends2.default)({}, params, {
series: params.series?.length ? params.series : EMPTY_ARRAY,
colors: params.colors ?? _colorPalettes.rainbowSurgePalette,
theme: params.theme ?? 'light'
});
useChartSeries.getInitialState = ({
series = [],
colors,
theme,
dataset
}, currentState) => {
const seriesConfig = currentState.seriesConfig.config;
const {
defaultizedSeries,
idToType
} = (0, _processSeries.defaultizeSeries)({
series,
colors: typeof colors === 'function' ? colors(theme) : colors,
theme,
seriesConfig
});
return {
series: {
defaultizedSeries,
idToType,
dataset
}
};
};