Skip to content

Commit c889253

Browse files
Avoid measuring text if tick is outside visible area
1 parent bc0ef42 commit c889253

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

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

+16-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import PropTypes from 'prop-types';
44
import useSlotProps from '@mui/utils/useSlotProps';
55
import composeClasses from '@mui/utils/composeClasses';
66
import { useThemeProps, useTheme, Theme, styled } from '@mui/material/styles';
7+
import { ChartInstance } from '@mui/x-charts/internals';
78
import { useTicks, TickItemType } from '../hooks/useTicks';
89
import { AxisDefaultized, ChartsXAxisProps } from '../models/axis';
910
import { getAxisUtilityClass } from '../ChartsAxis/axisClasses';
@@ -41,8 +42,12 @@ function addLabelDimension(
4142
tickLabelInterval,
4243
reverse,
4344
isMounted,
45+
isPointInside,
4446
}: Pick<ChartsXAxisProps, 'tickLabelInterval' | 'tickLabelStyle'> &
45-
Pick<AxisDefaultized, 'reverse'> & { isMounted: boolean },
47+
Pick<AxisDefaultized, 'reverse'> & {
48+
isMounted: boolean;
49+
isPointInside: ChartInstance<unknown>['isPointInside'];
50+
},
4651
): (TickItemType & LabelExtraData)[] {
4752
const getTickSize = (tick: TickItemType) => {
4853
if (!isMounted || tick.formattedValue === undefined) {
@@ -78,6 +83,10 @@ function addLabelDimension(
7883
return { ...item, width: 0, height: 0, skipLabel: true };
7984
}
8085

86+
if (!isPointInside({ x: textPosition, y: -1 }, { direction: 'x' })) {
87+
return { ...item, width: 0, height: 0, skipLabel: true };
88+
}
89+
8190
const { width, height } = getTickSize(item);
8291

8392
const distance = getMinXTranslation(width, height, style?.angle);
@@ -190,6 +199,12 @@ function ChartsXAxis(inProps: ChartsXAxisProps) {
190199
tickLabelInterval,
191200
reverse,
192201
isMounted,
202+
isPointInside: instance.isPointInside,
203+
});
204+
205+
console.log({
206+
xTicksWithDimension,
207+
measuredTicks: xTicksWithDimension.filter(({ width }) => width > 0),
193208
});
194209

195210
const labelRefPoint = {

0 commit comments

Comments
 (0)