Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e7fa935

Browse files
committedMar 6, 2025·
Use default text placement fns
1 parent 3745130 commit e7fa935

File tree

3 files changed

+65
-80
lines changed

3 files changed

+65
-80
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { clampAngle } from '../internals/clampAngle';
2+
import { ChartsTextStyle } from '../internals/getWordsByLines';
3+
4+
/**
5+
* Provide the text-anchor based on the angle between the text and the associated element.
6+
* - 0 means the element is on top of the text, 180 bellow, and 90 on the right of the text.
7+
* @param {number} angle The angle between the text and the element.
8+
* @returns
9+
*/
10+
export function getDefaultTextAnchor(angle: number): ChartsTextStyle['textAnchor'] {
11+
const adjustedAngle = clampAngle(angle);
12+
13+
if (adjustedAngle <= 30 || adjustedAngle >= 330) {
14+
// +/-30° around 0°
15+
return 'middle';
16+
}
17+
18+
if (adjustedAngle <= 210 && adjustedAngle >= 150) {
19+
// +/-30° around 180°
20+
return 'middle';
21+
}
22+
23+
if (adjustedAngle <= 180) {
24+
return 'end';
25+
}
26+
27+
return 'start';
28+
}
29+
30+
export function getDefaultBaseline(angle: number): ChartsTextStyle['dominantBaseline'] {
31+
const adjustedAngle = clampAngle(angle);
32+
33+
if (adjustedAngle <= 30 || adjustedAngle >= 330) {
34+
// +/-60° around 0°
35+
return 'hanging';
36+
}
37+
38+
if (adjustedAngle <= 210 && adjustedAngle >= 150) {
39+
// +/-60° around 180°
40+
return 'auto';
41+
}
42+
43+
return 'central';
44+
}

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

+11-38
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 { useIsClient } from '../hooks/useIsClient';
1011
import { ellipsize } from '../internals/ellipsize';
@@ -13,7 +14,7 @@ import { useTicks, TickItemType } from '../hooks/useTicks';
1314
import { AxisConfig, AxisDefaultized, ChartsXAxisProps, ScaleName } from '../models/axis';
1415
import { getAxisUtilityClass } from '../ChartsAxis/axisClasses';
1516
import { AxisRoot } from '../internals/components/AxisSharedComponents';
16-
import { ChartsText, ChartsTextProps, ChartsTextStyle } from '../ChartsText';
17+
import { ChartsText, ChartsTextProps } from '../ChartsText';
1718
import { getMinXTranslation } from '../internals/geometry';
1819
import { useMounted } from '../hooks/useMounted';
1920
import { ChartDrawingArea, useDrawingArea } from '../hooks/useDrawingArea';
@@ -118,37 +119,6 @@ function getVisibleLabels(
118119
);
119120
}
120121

121-
function getDefaultTextAnchor(angle: number): ChartsTextStyle['textAnchor'] {
122-
const adjustedAngle = clampAngle(angle);
123-
124-
if (adjustedAngle === 0 || adjustedAngle === 180) {
125-
return 'middle';
126-
}
127-
128-
if (adjustedAngle < 180) {
129-
return 'start';
130-
}
131-
132-
return 'end';
133-
}
134-
135-
function getDefaultBaseline(
136-
angle: number,
137-
position: 'top' | 'bottom' | 'none' | undefined,
138-
): ChartsTextStyle['dominantBaseline'] {
139-
const adjustedAngle = clampAngle(angle);
140-
141-
if (adjustedAngle === 0) {
142-
return position === 'bottom' ? 'hanging' : 'auto';
143-
}
144-
145-
if (adjustedAngle === 180) {
146-
return position === 'bottom' ? 'auto' : 'hanging';
147-
}
148-
149-
return 'central';
150-
}
151-
152122
function shortenLabels(
153123
visibleLabels: Set<TickItemType>,
154124
drawingArea: Pick<ChartDrawingArea, 'left' | 'width' | 'right'>,
@@ -277,8 +247,13 @@ function ChartsXAxis(inProps: ChartsXAxisProps) {
277247
const TickLabel = slots?.axisTickLabel ?? ChartsText;
278248
const Label = slots?.axisLabel ?? ChartsText;
279249

280-
const defaultTextAnchor = getDefaultTextAnchor(tickLabelStyle?.angle ?? 0);
281-
const shouldInvertTextAnchor = (isRtl && position !== 'top') || (!isRtl && position === 'top');
250+
const defaultTextAnchor = getDefaultTextAnchor(
251+
(position === 'bottom' ? 0 : 180) - (tickLabelStyle?.angle ?? 0),
252+
);
253+
const defaultDominantBaseline = getDefaultBaseline(
254+
(position === 'bottom' ? 0 : 180) - (tickLabelStyle?.angle ?? 0),
255+
);
256+
282257
const axisTickLabelProps = useSlotProps({
283258
elementType: TickLabel,
284259
externalSlotProps: slotProps?.axisTickLabel,
@@ -287,10 +262,8 @@ function ChartsXAxis(inProps: ChartsXAxisProps) {
287262
...theme.typography.caption,
288263
fontSize: 12,
289264
lineHeight: 1.25,
290-
textAnchor: shouldInvertTextAnchor
291-
? invertTextAnchor(defaultTextAnchor)
292-
: defaultTextAnchor,
293-
dominantBaseline: getDefaultBaseline(tickLabelStyle?.angle ?? 0, position),
265+
textAnchor: isRtl ? invertTextAnchor(defaultTextAnchor) : defaultTextAnchor,
266+
dominantBaseline: defaultDominantBaseline,
294267
...tickLabelStyle,
295268
},
296269
} as Partial<ChartsTextProps>,

‎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)
Please sign in to comment.