Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[charts] fix y-axis tick label overflow #16846

Open
wants to merge 25 commits into
base: master
Choose a base branch
from

Conversation

bernardobelchior
Copy link
Member

@bernardobelchior bernardobelchior commented Mar 6, 2025

Part of #10928.

Depends on:

Fixes tick label overflow for the y-axis. It follows the same approach as #16709, but the y-axis doesn't hide overlapping labels at the moment, which is why in the "after" image below you'll see that there's overlapping labels, namely when the labels are rotated -90 and 90 degrees.

The algorithm for hiding overlapping labels in the y-axis is out of scope for this PR.

Before:

localhost_3001_playground_long-y-axis-ticks_ (4)

After:
localhost_3001_playground_long-y-axis-ticks_ (3)

Changelog

Tick labels in the y-axis of cartesian charts will now have an ellipsis applied to prevent overflow.
If your tick labels are being clipped sooner than you would like, you can increase the y-axis size by increasing its width property.

@bernardobelchior bernardobelchior added enhancement This is not a bug, nor a new feature component: charts This is the name of the generic UI component, not the React module! labels Mar 6, 2025
@mui-bot
Copy link

mui-bot commented Mar 6, 2025

Deploy preview: https://deploy-preview-16846--material-ui-x.netlify.app/

Generated by 🚫 dangerJS against ebce504

Copy link

codspeed-hq bot commented Mar 6, 2025

CodSpeed Performance Report

Merging #16846 will not alter performance

Comparing bernardobelchior:fix-y-axis-tick-overflow (ebce504) with master (4877ff1)

Summary

✅ 7 untouched benchmarks
🆕 1 new benchmarks

Benchmarks breakdown

Benchmark BASE HEAD Change
🆕 PieChart with big data amount N/A 182.8 ms N/A

@bernardobelchior
Copy link
Member Author

@alexfauquette @JCQuintas should we consider once again updating the default axis size? Now for the y-axis 😛

The default axis size of 30px isn't enough to render "1,000" without ellipsis:

image

To do so, we'd need a width of 42px:

image

Should we increase the axis size? Would it make sense to reduce the margin or should we keep it as is? 🤔

@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Mar 7, 2025
Copy link

github-actions bot commented Mar 7, 2025

This pull request has conflicts, please resolve those before we can evaluate the pull request.

@bernardobelchior bernardobelchior force-pushed the fix-y-axis-tick-overflow branch from e7fa935 to 1819759 Compare March 12, 2025 12:38
@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged label Mar 12, 2025
@bernardobelchior bernardobelchior force-pushed the fix-y-axis-tick-overflow branch 3 times, most recently from 68a7125 to 0747a89 Compare March 18, 2025 13:27
@bernardobelchior
Copy link
Member Author

bernardobelchior commented Mar 18, 2025

@alexfauquette @JCQuintas should we consider once again updating the default axis size? Now for the y-axis 😛

The default axis size of 30px isn't enough to render "1,000" without ellipsis:

image

To do so, we'd need a width of 42px:

image

Should we increase the axis size? Would it make sense to reduce the margin or should we keep it as is? 🤔

Increased it to 50px and solved the overflows of most demos. Let me know if you think that's acceptable

@JCQuintas
Copy link
Member

Increased it to 50px and solved the overflows of most demos. Let me know if you think that's acceptable

That's probably for the better if it makes it so the defaults are better for everyone, I'll take a look at the screenshots

@JCQuintas
Copy link
Member

Increased it to 50px and solved the overflows of most demos. Let me know if you think that's acceptable

It seems 40 would fit an entire 4 character number, though I suppose it doesn't because there are some margins expected to exist?

@bernardobelchior
Copy link
Member Author

Increased it to 50px and solved the overflows of most demos. Let me know if you think that's acceptable

It seems 40 would fit an entire 4 character number, though I suppose it doesn't because there are some margins expected to exist?

To fit "1,000" we would need 42px as indicated in this comment. Without the comma, 40px might fit, yeah. Do you prefer if we chose 40px? It seems adding a comma is the default format.

@JCQuintas
Copy link
Member

My brain didn't compute the comma 😅

I believe the best approach would be the lowest size we can have that solves most of the cases we have. Ofc my brain would prefer a round number, but If 42 is the lowest, then we might want to use that instead.

@bernardobelchior
Copy link
Member Author

My brain didn't compute the comma 😅

I believe the best approach would be the lowest size we can have that solves most of the cases we have. Ofc my brain would prefer a round number, but If 42 is the lowest, then we might want to use that instead.

Ok, but I'd increase a couple pixels to maybe 45px. Text rendering can be different across different OSes, so it's possible that the same font and same text have different sizes.

@bernardobelchior bernardobelchior force-pushed the fix-y-axis-tick-overflow branch from e14bb2d to 8872087 Compare March 19, 2025 15:58
@bernardobelchior bernardobelchior marked this pull request as ready for review March 21, 2025 11:06
@bernardobelchior bernardobelchior force-pushed the fix-y-axis-tick-overflow branch from 8872087 to 5b00547 Compare March 21, 2025 11:06
@bernardobelchior bernardobelchior requested review from alexfauquette and JCQuintas and removed request for alexfauquette March 21, 2025 11:06
Copy link
Member

@alexfauquette alexfauquette left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice port from x-axis to the y-axis 👍

Maybe a small message in the description of the PR to be sure we don't forget to mention it in the changelog

@@ -174,6 +247,15 @@ function ChartsYAxis(inProps: ChartsYAxisProps) {
x: positionSign * (axisWidth - labelHeight),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not directly related to this PR, but I just noticed this x position could be replaced by

Suggested change
x: positionSign * (axisWidth - labelHeight),
x: positionSign * axisWidth,

and the label placement could be modified with

- dominantBaseline: 'auto',
+ dominantBaseline: 'text-before-edge',

(or text-after-edge I never remember who is who)

Such that the axis label does not depend on text measure anymore

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated 👍 it doesn't allow us to get rid of the label height measurement, though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it doesn't allow us to get rid of the label height measurement, though.

Not for the clean renderµ. But at least the axis main label should not move between the server-side rendering and the hydration since labelHeight does not impact it anymore

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, good point 👍

@bernardobelchior bernardobelchior force-pushed the fix-y-axis-tick-overflow branch from dd289df to 9367f14 Compare March 24, 2025 11:16
Comment on lines +72 to +74
if (isRtl) {
[topBoundFactor, bottomBoundFactor] = [bottomBoundFactor, topBoundFactor];
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixes RTL:
image

Comment on lines +152 to +154
if (isRtl) {
[leftBoundFactor, rightBoundFactor] = [rightBoundFactor, leftBoundFactor];
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixes RTL:

Screen.Recording.2025-03-24.at.12.25.02.mov

@bernardobelchior bernardobelchior force-pushed the fix-y-axis-tick-overflow branch from 1dd7314 to c69a00f Compare March 24, 2025 14:36
@@ -148,6 +149,10 @@ function shortenLabels(
[leftBoundFactor, rightBoundFactor] = [rightBoundFactor, leftBoundFactor];
}

if (isRtl) {
[leftBoundFactor, rightBoundFactor] = [rightBoundFactor, leftBoundFactor];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably good to leave a small comment on this code to mention it is to prevent an intermediate assignment 😅

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From my point of view, it's clear what's happening, so I saw no need for a comment.

Nevertheless, I can add it, but this is used in 4 different places, should I leave the comment in all 4 places?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just add it to the first usage on each of the files

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or you could extract the logic somehwere, they seem the same

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But this is a nitpick, can be done after

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll do it later as a follow-up, then 👍

@bernardobelchior bernardobelchior force-pushed the fix-y-axis-tick-overflow branch from a29e07c to ebce504 Compare March 25, 2025 16:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: charts This is the name of the generic UI component, not the React module! enhancement This is not a bug, nor a new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants