@mui/x-charts
Version:
The community edition of MUI X Charts components.
108 lines (102 loc) • 4.73 kB
JavaScript
import _formatErrorMessage from "@mui/x-internals/formatErrorMessage";
import _extends from "@babel/runtime/helpers/esm/extends";
import { defaultizeZoom } from "./defaultizeZoom.mjs";
import { DEFAULT_X_AXIS_KEY, DEFAULT_Y_AXIS_KEY, DEFAULT_AXIS_SIZE_HEIGHT, DEFAULT_AXIS_SIZE_WIDTH, AXIS_LABEL_DEFAULT_HEIGHT } from "../../../../constants/index.mjs";
export function defaultizeXAxis(inAxes, dataset, axesGap) {
const offsets = {
top: 0,
bottom: 0,
none: 0
};
const inputAxes = inAxes && inAxes.length > 0 ? inAxes : [{
id: 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 = DEFAULT_AXIS_SIZE_HEIGHT + (axisConfig.label ? AXIS_LABEL_DEFAULT_HEIGHT : 0);
const id = axisConfig.id ?? `defaultized-x-axis-${index}`;
const height = axisConfig.height ?? defaultHeight;
const sharedConfig = _extends({
offset: offsets[position]
}, axisConfig, {
id,
position,
height,
zoom: 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.' : _formatErrorMessage(37));
}
return _extends({}, sharedConfig, {
data: axisConfig.valueGetter ? dataset.map(d => axisConfig.valueGetter(d)) : dataset.map(d => d[dataKey])
});
});
return parsedAxes;
}
export function defaultizeYAxis(inAxes, dataset, axesGap) {
const offsets = {
right: 0,
left: 0,
none: 0
};
const inputAxes = inAxes && inAxes.length > 0 ? inAxes : [{
id: 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 = DEFAULT_AXIS_SIZE_WIDTH + (axisConfig.label ? AXIS_LABEL_DEFAULT_HEIGHT : 0);
const id = axisConfig.id ?? `defaultized-y-axis-${index}`;
const width = axisConfig.width ?? defaultWidth;
const sharedConfig = _extends({
offset: offsets[position]
}, axisConfig, {
id,
position,
width,
zoom: 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.' : _formatErrorMessage(38));
}
return _extends({}, sharedConfig, {
data: axisConfig.valueGetter ? dataset.map(d => axisConfig.valueGetter(d)) : dataset.map(d => d[dataKey])
});
});
return parsedAxes;
}