Skip to content

Commit ff6e1cb

Browse files
[charts] Fix undefined behaving differently than missing value for axis size
1 parent b83be87 commit ff6e1cb

File tree

1 file changed

+11
-15
lines changed
  • packages/x-charts/src/internals/plugins/featurePlugins/useChartCartesianAxis

1 file changed

+11
-15
lines changed

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

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ export function defaultizeAxis(
2020
dataset: Readonly<DatasetType> | undefined,
2121
axisName: 'y',
2222
): AxisConfig<ScaleName, any, ChartsYAxisProps>[];
23-
export function defaultizeAxis(
23+
export function defaultizeAxis<Axis = 'x' | 'y'>(
2424
inAxis: readonly MakeOptional<AxisConfig, 'id'>[] | undefined,
2525
dataset: Readonly<DatasetType> | undefined,
26-
axisName: 'x' | 'y',
26+
axisName: Axis,
2727
): AxisConfig[] {
2828
const DEFAULT_AXIS_KEY = axisName === 'x' ? DEFAULT_X_AXIS_KEY : DEFAULT_Y_AXIS_KEY;
2929

@@ -45,29 +45,25 @@ export function defaultizeAxis(
4545
const position = axisConfig.position ?? 'none';
4646
const dimension = axisName === 'x' ? 'height' : 'width';
4747

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;
56-
5748
const sharedConfig = {
5849
id: `defaultized-${axisName}-axis-${index}`,
5950
// The fist axis is defaultized to the bottom/left
6051
...(index === 0 ? { position: defaultPosition } : {}),
61-
height,
62-
width,
6352
offset: offsets[position],
6453
...axisConfig,
6554
};
6655

56+
if (axisName === 'x') {
57+
const height = DEFAULT_AXIS_SIZE_HEIGHT + (axisConfig.label ? AXIS_LABEL_DEFAULT_HEIGHT : 0);
58+
(sharedConfig as any).height = (axisConfig as any).height ?? height;
59+
} else if (axisName === 'y') {
60+
const width = DEFAULT_AXIS_SIZE_WIDTH + (axisConfig.label ? AXIS_LABEL_DEFAULT_HEIGHT : 0);
61+
(sharedConfig as any).width = (axisConfig as any).width ?? width;
62+
}
63+
6764
// Increment the offset for the next axis
6865
if (position !== 'none') {
69-
offsets[position] +=
70-
(axisConfig as any)[dimension] ?? (dimension === 'height' ? height : width);
66+
offsets[position] += (sharedConfig as any)[dimension];
7167
}
7268

7369
// If `dataKey` is NOT provided

0 commit comments

Comments
 (0)