Skip to content

Commit

Permalink
Refactor checkScaleErrors to improve readability and simplify axis me…
Browse files Browse the repository at this point in the history
…ssage generation
  • Loading branch information
JCQuintas committed May 30, 2024
1 parent e796d10 commit 19a8c17
Showing 1 changed file with 10 additions and 15 deletions.
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}".`,
);
}
}

0 comments on commit 19a8c17

Please sign in to comment.