Skip to content

Commit 94d52da

Browse files
Use default text placement fns
1 parent bb1dad3 commit 94d52da

File tree

2 files changed

+11
-42
lines changed

2 files changed

+11
-42
lines changed

packages/x-charts/src/ChartsXAxis/ChartsXAxis.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import useSlotProps from '@mui/utils/useSlotProps';
55
import composeClasses from '@mui/utils/composeClasses';
66
import { useThemeProps, useTheme, styled } from '@mui/material/styles';
77
import { useRtl } from '@mui/system/RtlProvider';
8+
import { getDefaultBaseline, getDefaultTextAnchor } from '../ChartsText/defaultTextPlacement';
89
import { clampAngle } from '../internals/clampAngle';
910
import { useIsHydrated } from '../hooks/useIsHydrated';
1011
import { doesTextFitInRect, ellipsize } from '../internals/ellipsize';

packages/x-charts/src/ChartsYAxis/ChartsYAxis.tsx

+10-42
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import useSlotProps from '@mui/utils/useSlotProps';
55
import composeClasses from '@mui/utils/composeClasses';
66
import { useThemeProps, styled, useTheme } from '@mui/material/styles';
77
import { useRtl } from '@mui/system/RtlProvider';
8+
import { getDefaultBaseline, getDefaultTextAnchor } from '../ChartsText/defaultTextPlacement';
89
import { ellipsize } from '../internals/ellipsize';
910
import { useIsClient } from '../hooks/useIsClient';
1011
import { getStringSize } from '../internals/domUtils';
@@ -94,41 +95,6 @@ function shortenLabels(
9495
return shortenedLabels;
9596
}
9697

97-
function getDefaultTextAnchor(angle: number): ChartsTextStyle['textAnchor'] {
98-
const adjustedAngle = clampAngle(angle);
99-
100-
if (adjustedAngle === 90 || adjustedAngle === 270) {
101-
return 'middle';
102-
}
103-
104-
if (adjustedAngle < 90) {
105-
return 'end';
106-
}
107-
108-
if (adjustedAngle < 270) {
109-
return 'start';
110-
}
111-
112-
return 'end';
113-
}
114-
115-
function getDefaultBaseline(
116-
angle: number,
117-
position: 'left' | 'right' | 'none' | undefined,
118-
): ChartsTextStyle['dominantBaseline'] {
119-
const adjustedAngle = clampAngle(angle);
120-
121-
if (adjustedAngle === 90 || adjustedAngle === 270) {
122-
return position === 'left' ? 'auto' : 'hanging';
123-
}
124-
125-
if (adjustedAngle > 270) {
126-
return position === 'left' ? 'auto' : 'hanging';
127-
}
128-
129-
return 'central';
130-
}
131-
13298
function invertTextAnchor(
13399
textAnchor: ChartsTextStyle['textAnchor'],
134100
): ChartsTextStyle['textAnchor'] {
@@ -224,20 +190,22 @@ function ChartsYAxis(inProps: ChartsYAxisProps) {
224190
const TickLabel = slots?.axisTickLabel ?? ChartsText;
225191
const Label = slots?.axisLabel ?? ChartsText;
226192

227-
const defaultTextAnchor = getDefaultTextAnchor(tickLabelStyle?.angle ?? 0);
228-
const shouldInvertTextAnchor =
229-
(isRtl && position !== 'right') || (!isRtl && position === 'right');
193+
const defaultTextAnchor = getDefaultTextAnchor(
194+
(position === 'right' ? -90 : 90) - (tickLabelStyle?.angle ?? 0),
195+
);
196+
const defaultDominantBaseline = getDefaultBaseline(
197+
(position === 'right' ? -90 : 90) - (tickLabelStyle?.angle ?? 0),
198+
);
199+
230200
const axisTickLabelProps = useSlotProps({
231201
elementType: TickLabel,
232202
externalSlotProps: slotProps?.axisTickLabel,
233203
additionalProps: {
234204
style: {
235205
...theme.typography.caption,
236206
fontSize: tickFontSize,
237-
textAnchor: shouldInvertTextAnchor
238-
? invertTextAnchor(defaultTextAnchor)
239-
: defaultTextAnchor,
240-
dominantBaseline: getDefaultBaseline(tickLabelStyle?.angle ?? 0, position),
207+
textAnchor: isRtl ? invertTextAnchor(defaultTextAnchor) : defaultTextAnchor,
208+
dominantBaseline: defaultDominantBaseline,
241209
...tickLabelStyle,
242210
},
243211
} as Partial<ChartsTextProps>,

0 commit comments

Comments
 (0)