Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[charts] Refactor checkScaleErrors to improve readability and simplify axis message logic #13305

Merged
merged 1 commit into from
May 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 10 additions & 15 deletions packages/x-charts/src/BarChart/checkScaleErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,10 @@ import { DEFAULT_X_AXIS_KEY, DEFAULT_Y_AXIS_KEY } from '../constants';
import { AxisDefaultized, isBandScaleConfig, isPointScaleConfig } from '../models/axis';
import { SeriesId } from '../models/seriesType/common';

const getXOrY = (isVertical: boolean, isContinuous: boolean, x: string, y: string) => {
if (isVertical) {
return isContinuous ? y : x;
}
return isContinuous ? x : y;
};

const getAxisMessage = (isVertical: boolean, axisKey: string, isContinuous: boolean = false) => {
const axisName = getXOrY(isVertical, isContinuous, 'x-axis', 'y-axis');
const axisKeyName = getXOrY(isVertical, isContinuous, 'xAxis', 'yAxis');
const axisDefaultKey = getXOrY(isVertical, isContinuous, DEFAULT_X_AXIS_KEY, DEFAULT_Y_AXIS_KEY);

const getAxisMessage = (axisDirection: 'x' | 'y', axisKey: string) => {
const axisName = `${axisDirection}-axis`;
const axisKeyName = `${axisDirection}Axis`;
const axisDefaultKey = axisDirection === 'x' ? DEFAULT_X_AXIS_KEY : DEFAULT_Y_AXIS_KEY;
return axisKey === axisDefaultKey
? `The first \`${axisKeyName}\``
: `The ${axisName} with id "${axisKey}"`;
Expand All @@ -36,19 +28,22 @@ export function checkScaleErrors(
const discreteAxisKey = verticalLayout ? xAxisKey : yAxisKey;
const continuousAxisKey = verticalLayout ? yAxisKey : xAxisKey;

const discreteAxisDirection = verticalLayout ? 'x' : 'y';
const continuousAxisDirection = verticalLayout ? 'y' : 'x';

if (!isBandScaleConfig(discreteAxisConfig)) {
throw new Error(
`MUI X Charts: ${getAxisMessage(verticalLayout, discreteAxisKey)} should be of type "band" to display the bar series of id "${seriesId}".`,
`MUI X Charts: ${getAxisMessage(discreteAxisDirection, discreteAxisKey)} should be of type "band" to display the bar series of id "${seriesId}".`,
);
}
if (discreteAxisConfig.data === undefined) {
throw new Error(
`MUI X Charts: ${getAxisMessage(verticalLayout, discreteAxisKey)} should have data property.`,
`MUI X Charts: ${getAxisMessage(discreteAxisDirection, discreteAxisKey)} should have data property.`,
);
}
if (isBandScaleConfig(continuousAxisConfig) || isPointScaleConfig(continuousAxisConfig)) {
throw new Error(
`MUI X Charts: ${getAxisMessage(verticalLayout, continuousAxisKey, true)} should be a continuous type to display the bar series of id "${seriesId}".`,
`MUI X Charts: ${getAxisMessage(continuousAxisDirection, continuousAxisKey)} should be a continuous type to display the bar series of id "${seriesId}".`,
);
}
}
Loading