UNPKG

@mui/x-charts

Version:

The community edition of MUI X Charts components.

116 lines (109 loc) 5.1 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default; Object.defineProperty(exports, "__esModule", { value: true }); exports.defaultizeXAxis = defaultizeXAxis; exports.defaultizeYAxis = defaultizeYAxis; var _formatErrorMessage2 = _interopRequireDefault(require("@mui/x-internals/formatErrorMessage")); var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); var _defaultizeZoom = require("./defaultizeZoom"); var _constants = require("../../../../constants"); function defaultizeXAxis(inAxes, dataset, axesGap) { const offsets = { top: 0, bottom: 0, none: 0 }; const inputAxes = inAxes && inAxes.length > 0 ? inAxes : [{ id: _constants.DEFAULT_X_AXIS_KEY, scaleType: 'linear' }]; const parsedAxes = inputAxes.map((axisConfig, index) => { const dataKey = axisConfig.dataKey; // The first x-axis is defaultized to the bottom const defaultPosition = index === 0 ? 'bottom' : 'none'; const position = axisConfig.position ?? defaultPosition; const defaultHeight = _constants.DEFAULT_AXIS_SIZE_HEIGHT + (axisConfig.label ? _constants.AXIS_LABEL_DEFAULT_HEIGHT : 0); const id = axisConfig.id ?? `defaultized-x-axis-${index}`; const height = axisConfig.height ?? defaultHeight; const sharedConfig = (0, _extends2.default)({ offset: offsets[position] }, axisConfig, { id, position, height, zoom: (0, _defaultizeZoom.defaultizeZoom)(axisConfig.zoom, id, 'x', axisConfig.reverse) }); // Increment the offset for the next axis // For 'auto' height, use default height for initial offset calculation // The actual auto-size will be computed by selectors if (position !== 'none') { const heightForOffset = height === 'auto' ? defaultHeight : height; offsets[position] += heightForOffset + axesGap; if (sharedConfig.zoom?.slider.enabled) { offsets[position] += sharedConfig.zoom.slider.size; } } // If data is already provided or no dataset extraction is needed if (axisConfig.data !== undefined || dataKey === undefined && !axisConfig.valueGetter) { return sharedConfig; } if (dataset === undefined) { throw new Error(process.env.NODE_ENV !== "production" ? 'MUI X Charts: The x-axis uses `dataKey` or `valueGetter` but no `dataset` is provided. ' + 'When using dataKey or valueGetter, a dataset must be provided to retrieve the axis data. ' + 'Either provide a dataset prop or use the data property directly on the x-axis.' : (0, _formatErrorMessage2.default)(37)); } return (0, _extends2.default)({}, sharedConfig, { data: axisConfig.valueGetter ? dataset.map(d => axisConfig.valueGetter(d)) : dataset.map(d => d[dataKey]) }); }); return parsedAxes; } function defaultizeYAxis(inAxes, dataset, axesGap) { const offsets = { right: 0, left: 0, none: 0 }; const inputAxes = inAxes && inAxes.length > 0 ? inAxes : [{ id: _constants.DEFAULT_Y_AXIS_KEY, scaleType: 'linear' }]; const parsedAxes = inputAxes.map((axisConfig, index) => { const dataKey = axisConfig.dataKey; // The first y-axis is defaultized to the left const defaultPosition = index === 0 ? 'left' : 'none'; const position = axisConfig.position ?? defaultPosition; const defaultWidth = _constants.DEFAULT_AXIS_SIZE_WIDTH + (axisConfig.label ? _constants.AXIS_LABEL_DEFAULT_HEIGHT : 0); const id = axisConfig.id ?? `defaultized-y-axis-${index}`; const width = axisConfig.width ?? defaultWidth; const sharedConfig = (0, _extends2.default)({ offset: offsets[position] }, axisConfig, { id, position, width, zoom: (0, _defaultizeZoom.defaultizeZoom)(axisConfig.zoom, id, 'y', axisConfig.reverse) }); // Increment the offset for the next axis // For 'auto' width, use default width for initial offset calculation // The actual auto-size will be computed by selectors if (position !== 'none') { const widthForOffset = width === 'auto' ? defaultWidth : width; offsets[position] += widthForOffset + axesGap; if (sharedConfig.zoom?.slider.enabled) { offsets[position] += sharedConfig.zoom.slider.size; } } // If data is already provided or no dataset extraction is needed if (axisConfig.data !== undefined || dataKey === undefined && !axisConfig.valueGetter) { return sharedConfig; } if (dataset === undefined) { throw new Error(process.env.NODE_ENV !== "production" ? 'MUI X Charts: The y-axis uses `dataKey` or `valueGetter` but no `dataset` is provided. ' + 'When using dataKey or valueGetter, a dataset must be provided to retrieve the axis data. ' + 'Either provide a dataset prop or use the data property directly on the y-axis.' : (0, _formatErrorMessage2.default)(38)); } return (0, _extends2.default)({}, sharedConfig, { data: axisConfig.valueGetter ? dataset.map(d => axisConfig.valueGetter(d)) : dataset.map(d => d[dataKey]) }); }); return parsedAxes; }