Skip to content

Commit

Permalink
[charts] Fix tickLabelInterval not working on YAxis (mui#12746)
Browse files Browse the repository at this point in the history
  • Loading branch information
JCQuintas authored Apr 12, 2024
1 parent d097dc7 commit ec2df5c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
10 changes: 5 additions & 5 deletions packages/x-charts/src/ChartsXAxis/ChartsXAxis.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function addLabelDimension(

// Filter label to avoid overlap
let currentTextLimit = 0;
let previouseTextLimit = 0;
let previousTextLimit = 0;
const direction = reverse ? -1 : 1;
return withDimension.map((item, labelIndex) => {
const { width, offset, labelOffset, height } = item;
Expand All @@ -71,12 +71,12 @@ function addLabelDimension(
const gapRatio = 1.2; // Ratio applied to the minimal distance to add some margin.

currentTextLimit = textPosition - (direction * (gapRatio * distance)) / 2;
if (labelIndex > 0 && direction * currentTextLimit < direction * previouseTextLimit) {
if (labelIndex > 0 && direction * currentTextLimit < direction * previousTextLimit) {
// Except for the first label, we skip all label that overlap with the last accepted.
// Notice that the early return prevents `previouseTextLimit` from being updated.
// Notice that the early return prevents `previousTextLimit` from being updated.
return { ...item, skipLabel: true };
}
previouseTextLimit = textPosition + (direction * (gapRatio * distance)) / 2;
previousTextLimit = textPosition + (direction * (gapRatio * distance)) / 2;
return item;
});
}
Expand Down Expand Up @@ -347,7 +347,7 @@ ChartsXAxis.propTypes = {
*/
tickMinStep: PropTypes.number,
/**
* The number of ticks. This number is not guaranted.
* The number of ticks. This number is not guaranteed.
* Not supported by categorical axis (band, points).
*/
tickNumber: PropTypes.number,
Expand Down
9 changes: 6 additions & 3 deletions packages/x-charts/src/ChartsYAxis/ChartsYAxis.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ function ChartsYAxis(inProps: ChartsYAxisProps) {
tickPlacement,
tickLabelPlacement,
tickInterval,
tickLabelInterval,
} = defaultizedProps;

const theme = useTheme();
Expand Down Expand Up @@ -143,9 +144,11 @@ function ChartsYAxis(inProps: ChartsYAxisProps) {
/>
)}

{yTicks.map(({ formattedValue, offset, labelOffset }, index) => {
{yTicks.map(({ formattedValue, offset, labelOffset, value }, index) => {
const xTickLabel = positionSign * (tickSize + 2);
const yTickLabel = labelOffset;
const skipLabel =
typeof tickLabelInterval === 'function' && !tickLabelInterval?.(value, index);
return (
<g key={index} transform={`translate(0, ${offset})`} className={classes.tickContainer}>
{!disableTicks && (
Expand All @@ -156,7 +159,7 @@ function ChartsYAxis(inProps: ChartsYAxisProps) {
/>
)}

{formattedValue !== undefined && (
{formattedValue !== undefined && !skipLabel && (
<TickLabel
x={xTickLabel}
y={yTickLabel}
Expand Down Expand Up @@ -282,7 +285,7 @@ ChartsYAxis.propTypes = {
*/
tickMinStep: PropTypes.number,
/**
* The number of ticks. This number is not guaranted.
* The number of ticks. This number is not guaranteed.
* Not supported by categorical axis (band, points).
*/
tickNumber: PropTypes.number,
Expand Down
2 changes: 1 addition & 1 deletion packages/x-charts/src/hooks/useTicks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export interface TickParams {
*/
tickMinStep?: number;
/**
* The number of ticks. This number is not guaranted.
* The number of ticks. This number is not guaranteed.
* Not supported by categorical axis (band, points).
*/
tickNumber?: number;
Expand Down

0 comments on commit ec2df5c

Please sign in to comment.