-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
[charts] Add prop for positioning a bar label #20194
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
Conversation
Sigdriv
commented
Nov 4, 2025
- I have followed (at least) the PR section of the contributing guide.
|
Deploy preview: https://deploy-preview-20194--material-ui-x.netlify.app/ Updated pages: Bundle size report
|
8fb20bc to
7fda73e
Compare
|
Thanks for your contribution, @Sigdriv! We're currently evaluating whether it makes sense for Nevertheless, I think we can already review this PR and deal with that change if needed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Sigdriv what's the intended behavior when rendering negative bars?
Here's how it looks like now:
I'm wondering if the label should be shown below the bar when the bar is negative. What do you think?
CodSpeed Performance ReportMerging #20194 will not alter performanceComparing Summary
Footnotes |
Hmm, that's a good question. I think it will look great if we put it down below the bar, but I don't know how we should handle it if the bar is on the axis like in the picture below (see the bar with value -4):
|
|
@bernardobelchior One thing that I haven't thought about is how we should handle the "stacking" functionality with this. The way it's now it will be at the top of the bar, which is not looking good. Do you have any input on how we can solve this?
|
7fda73e to
6f189e1
Compare
a00a9d5 to
2198677
Compare
Just to keep in mind that in future we may have to support showing total at the top of the stacked bar as requested in #19458. |
|
This pull request has conflicts, please resolve those before we can evaluate the pull request. |
That's an edge case that'll likely also happen if we have an axis on top, so it doesn't happen only in negative bars. I think we can live with that edge case. There are multiple ways to solve it: use the
That's what happens currently when the bar label is centered, so I think this is a bit out of scope for this PR. One thing I'm thinking of is that we could add stacking information to the
I think this is a valid solution for now. We can always improve on it later if there are many requests. We can also consider adding a slot for |
2198677 to
2659553
Compare
e0d3e86 to
4dcdeff
Compare
bernardobelchior
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There seems to be an issue with this visual regression test.
4dcdeff to
cb54ae7
Compare
1ea9806 to
eabad03
Compare
I have reverted the changes to the |
| const shouldPlaceBelow = props.y > props.yOrigin; | ||
| const shouldPlaceAbove = props.y < props.yOrigin; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Sigdriv based the implementation on the x/y coordinates instead of the bar's value.
The benefits of this approach are that it handles axis reversion gracefully and that we don't need useAnimateBarLabel to depend on value, so there's no breaking change.
The prior implementation looked like this:
Notice that in the layout: vertical, reverse: true case, the -1 and -4 values are rendering below the bar when should be rendering above.
The new implementation fixes the issue:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch
Yes, that's expected. Does this affect your use case? I see several ways to fix it, but I think we should do it as a follow-up:
|
| const minValueCoord = Math.min(...valueCoordinates); | ||
| const maxValueCoord = Math.max(...valueCoordinates); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was able to trace this change back to this PR. Still need to check what implications this might have, but it's creating issues because we aren't rounded the origin coordinates, making it impossible to compare to the x/y coordinates.
If we still need to round these values, we can do so when rendering instead of doing it here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reverted because it caused gaps between bars: https://app.argos-ci.com/mui/mui-x/builds/43580/218980533
Opted for rounding the origin instead.
|
@Sigdriv created this issue to track the bar label clipping issue |
JCQuintas
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice works, there is still a small bug, also left some documentation suggestions
docs/data/charts/bars/bars.md
Outdated
|
|
||
| :::info | ||
| When using `outside` placement, if the label does not fit in the chart area, it will be clipped. | ||
| To avoid this, you can decrease/increase the min/max respectively so that there's enough space for the labels. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Which min/max is this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let me know if it's clearer now
|
Once again, thank you @Sigdriv for the initiative 🙏 |
This reverts commit 28f7e77.
This reverts commit 28f7e77.
This reverts commit 28f7e77.
This reverts commit 28f7e77.






