Skip to content

Commit 03c32eb

Browse files
[charts] Split defaultizeAxis function into two (#16745)
1 parent b83be87 commit 03c32eb

File tree

3 files changed

+69
-42
lines changed

3 files changed

+69
-42
lines changed

packages/x-charts/src/internals/plugins/featurePlugins/useChartCartesianAxis/defaultizeAxis.ts

Lines changed: 63 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,64 +10,91 @@ import { AxisConfig, ScaleName } from '../../../../models';
1010
import { ChartsXAxisProps, ChartsYAxisProps } from '../../../../models/axis';
1111
import { DatasetType } from '../../../../models/seriesType/config';
1212

13-
export function defaultizeAxis(
13+
export function defaultizeXAxis(
1414
inAxis: readonly MakeOptional<AxisConfig<ScaleName, any, ChartsXAxisProps>, 'id'>[] | undefined,
1515
dataset: Readonly<DatasetType> | undefined,
16-
axisName: 'x',
17-
): AxisConfig<ScaleName, any, ChartsXAxisProps>[];
18-
export function defaultizeAxis(
19-
inAxis: readonly MakeOptional<AxisConfig<ScaleName, any, ChartsYAxisProps>, 'id'>[] | undefined,
20-
dataset: Readonly<DatasetType> | undefined,
21-
axisName: 'y',
22-
): AxisConfig<ScaleName, any, ChartsYAxisProps>[];
23-
export function defaultizeAxis(
24-
inAxis: readonly MakeOptional<AxisConfig, 'id'>[] | undefined,
25-
dataset: Readonly<DatasetType> | undefined,
26-
axisName: 'x' | 'y',
27-
): AxisConfig[] {
28-
const DEFAULT_AXIS_KEY = axisName === 'x' ? DEFAULT_X_AXIS_KEY : DEFAULT_Y_AXIS_KEY;
29-
16+
): Array<AxisConfig<ScaleName, any, ChartsXAxisProps>> {
3017
const offsets = {
3118
top: 0,
32-
right: 0,
3319
bottom: 0,
34-
left: 0,
3520
none: 0,
3621
};
3722

3823
const inputAxes =
39-
inAxis && inAxis.length > 0 ? inAxis : [{ id: DEFAULT_AXIS_KEY, scaleType: 'linear' as const }];
24+
inAxis && inAxis.length > 0
25+
? inAxis
26+
: [{ id: DEFAULT_X_AXIS_KEY, scaleType: 'linear' as const }];
4027

4128
const parsedAxes = inputAxes.map((axisConfig, index) => {
4229
const dataKey = axisConfig.dataKey;
43-
const defaultPosition = axisName === 'x' ? ('bottom' as const) : ('left' as const);
4430

4531
const position = axisConfig.position ?? 'none';
46-
const dimension = axisName === 'x' ? 'height' : 'width';
47-
48-
const height =
49-
axisName === 'x'
50-
? DEFAULT_AXIS_SIZE_HEIGHT + (axisConfig.label ? AXIS_LABEL_DEFAULT_HEIGHT : 0)
51-
: 0;
52-
const width =
53-
axisName === 'y'
54-
? DEFAULT_AXIS_SIZE_WIDTH + (axisConfig.label ? AXIS_LABEL_DEFAULT_HEIGHT : 0)
55-
: 0;
32+
const defaultHeight =
33+
DEFAULT_AXIS_SIZE_HEIGHT + (axisConfig.label ? AXIS_LABEL_DEFAULT_HEIGHT : 0);
5634

5735
const sharedConfig = {
58-
id: `defaultized-${axisName}-axis-${index}`,
36+
id: `defaultized-x-axis-${index}`,
5937
// The fist axis is defaultized to the bottom/left
60-
...(index === 0 ? { position: defaultPosition } : {}),
61-
height,
62-
width,
38+
...(index === 0 ? ({ position: 'bottom' } as const) : {}),
39+
height: defaultHeight,
40+
offset: offsets[position],
41+
...axisConfig,
42+
};
43+
44+
// Increment the offset for the next axis
45+
if (position !== 'none') {
46+
offsets[position] += axisConfig.height ?? defaultHeight;
47+
}
48+
49+
// If `dataKey` is NOT provided
50+
if (dataKey === undefined || axisConfig.data !== undefined) {
51+
return sharedConfig;
52+
}
53+
54+
if (dataset === undefined) {
55+
throw new Error(`MUI X: x-axis uses \`dataKey\` but no \`dataset\` is provided.`);
56+
}
57+
58+
// If `dataKey` is provided
59+
return {
60+
...sharedConfig,
61+
data: dataset.map((d) => d[dataKey]),
62+
};
63+
});
64+
65+
return parsedAxes;
66+
}
67+
68+
export function defaultizeYAxis(
69+
inAxis: readonly MakeOptional<AxisConfig<ScaleName, any, ChartsYAxisProps>, 'id'>[] | undefined,
70+
dataset: Readonly<DatasetType> | undefined,
71+
): Array<AxisConfig<ScaleName, any, ChartsYAxisProps>> {
72+
const offsets = { right: 0, left: 0, none: 0 };
73+
74+
const inputAxes =
75+
inAxis && inAxis.length > 0
76+
? inAxis
77+
: [{ id: DEFAULT_Y_AXIS_KEY, scaleType: 'linear' as const }];
78+
79+
const parsedAxes = inputAxes.map((axisConfig, index) => {
80+
const dataKey = axisConfig.dataKey;
81+
82+
const position = axisConfig.position ?? 'none';
83+
const defaultWidth =
84+
DEFAULT_AXIS_SIZE_WIDTH + (axisConfig.label ? AXIS_LABEL_DEFAULT_HEIGHT : 0);
85+
86+
const sharedConfig = {
87+
id: `defaultized-y-axis-${index}`,
88+
// The first axis is defaultized to the left
89+
...(index === 0 ? ({ position: 'left' } as const) : {}),
90+
width: defaultWidth,
6391
offset: offsets[position],
6492
...axisConfig,
6593
};
6694

6795
// Increment the offset for the next axis
6896
if (position !== 'none') {
69-
offsets[position] +=
70-
(axisConfig as any)[dimension] ?? (dimension === 'height' ? height : width);
97+
offsets[position] += axisConfig.width ?? defaultWidth;
7198
}
7299

73100
// If `dataKey` is NOT provided
@@ -76,7 +103,7 @@ export function defaultizeAxis(
76103
}
77104

78105
if (dataset === undefined) {
79-
throw new Error(`MUI X: ${axisName}-axis uses \`dataKey\` but no \`dataset\` is provided.`);
106+
throw new Error(`MUI X: y-axis uses \`dataKey\` but no \`dataset\` is provided.`);
80107
}
81108

82109
// If `dataKey` is provided
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export { useChartCartesianAxis } from './useChartCartesianAxis';
22
export type * from './useChartCartesianAxis.types';
33
export * from './useChartCartesianAxisRendering.selectors';
4-
export { defaultizeAxis } from './defaultizeAxis';
4+
export { defaultizeXAxis, defaultizeYAxis } from './defaultizeAxis';
55
export * from './computeAxisValue';
66
export * from './createZoomLookup';
77
export * from './zoom.types';

packages/x-charts/src/internals/plugins/featurePlugins/useChartCartesianAxis/useChartCartesianAxis.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { rainbowSurgePalette } from '../../../../colorPalettes';
77
import { useSelector } from '../../../store/useSelector';
88
import { selectorChartDrawingArea } from '../../corePlugins/useChartDimensions/useChartDimensions.selectors';
99
import { selectorChartSeriesProcessed } from '../../corePlugins/useChartSeries/useChartSeries.selectors';
10-
import { defaultizeAxis } from './defaultizeAxis';
10+
import { defaultizeXAxis, defaultizeYAxis } from './defaultizeAxis';
1111
import { selectorChartXAxis, selectorChartYAxis } from './useChartCartesianAxisRendering.selectors';
1212
import { getAxisValue } from './getAxisValue';
1313
import { getSVGPoint } from '../../../getSVGPoint';
@@ -62,8 +62,8 @@ export const useChartCartesianAxis: ChartPlugin<UseChartCartesianAxisSignature<a
6262
...prev,
6363
cartesianAxis: {
6464
...prev.cartesianAxis,
65-
x: defaultizeAxis(xAxis, dataset, 'x'),
66-
y: defaultizeAxis(yAxis, dataset, 'y'),
65+
x: defaultizeXAxis(xAxis, dataset),
66+
y: defaultizeYAxis(yAxis, dataset),
6767
},
6868
}));
6969
}, [seriesConfig, drawingArea, xAxis, yAxis, dataset, store]);
@@ -244,8 +244,8 @@ useChartCartesianAxis.getDefaultizedParams = ({ params }) => {
244244
...params,
245245
colors: params.colors ?? rainbowSurgePalette,
246246
theme: params.theme ?? 'light',
247-
defaultizedXAxis: defaultizeAxis(params.xAxis, params.dataset, 'x'),
248-
defaultizedYAxis: defaultizeAxis(params.yAxis, params.dataset, 'y'),
247+
defaultizedXAxis: defaultizeXAxis(params.xAxis, params.dataset),
248+
defaultizedYAxis: defaultizeYAxis(params.yAxis, params.dataset),
249249
};
250250
};
251251

0 commit comments

Comments
 (0)