-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add support for responsive y-axis width #5880
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
base: 3.x
Are you sure you want to change the base?
Add support for responsive y-axis width #5880
Conversation
Can you add some unit tests |
Before looking into code, is it possible to have the API as |
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.
Pull Request Overview
This PR adds support for a responsive y-axis width by dynamically calculating the required width based on label and tick dimensions. Key changes include the addition of new utility functions in YAxisUtils.tsx, an update action in cartesianAxisSlice.ts, and corresponding adjustments in the Text, Label, YAxis, and CartesianAxis components to handle responsive labeling.
Reviewed Changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.
Show a summary per file
File | Description |
---|---|
src/util/YAxisUtils.tsx | New helper functions to modify label props and calculate y-axis width. |
src/state/cartesianAxisSlice.ts | Added updateYAxisWidth action to update state with new width values. |
src/component/Text.tsx | Refactored Text component to use forwardRef for better ref handling. |
src/component/Label.tsx | Enhanced Label component with labelRef support for width calculation. |
src/cartesian/YAxis.tsx | Integrated isWidthResponsive prop and responsive width calculation. |
src/cartesian/CartesianAxis.tsx | Updated ref handling to collect tick refs used for width measurement. |
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.
Okay the "after" variant looks much better. Let's have that as a new breaking change in 3.x @ckifer?
@saurabhraj123 thank you for the PR. Would you mind to fix the conflicts? Then we can have a look at what the CI build looks like and what is the visual diff.
You're right. The |
I will work on it @ckifer. |
Run |
Also, the YAxis label can be defined two different ways, 1. with |
The command runs successfully in local but produces no change. Do I need to do something else? |
If it's not working you can also add to the snapshots manually if you look at the failing build |
Ah sorry I forgot. First |
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## 3.x #5880 +/- ##
==========================================
- Coverage 95.54% 95.38% -0.16%
==========================================
Files 184 187 +3
Lines 18776 19076 +300
Branches 3790 3831 +41
==========================================
+ Hits 17939 18196 +257
- Misses 831 874 +43
Partials 6 6 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Label width calculation will be handled in another PR
I am done with all other changes and was trying to add some test cases but I don't know why, but the render method is not rendering the y-axis properly when This is the test code I am using: it('should render y-axis with dynamically calculated width', async () => {
const ticks = [0, 400, 1200, 1600];
const { container } = render(
<BarChart width={100} height={100}>
<YAxis width="auto" ticks={ticks} />
<CartesianGrid />
</BarChart>,
);
const yAxis = container.querySelector('.yAxis');
const yAxisWidth = yAxis.getBoundingClientRect().width;
const yAxisLine = yAxis.querySelector('line');
expect(yAxis).toBeVisible();
expect(yAxisLine).toHaveAttribute('width', String(Math.round(yAxisWidth)));
}); |
You're going to need to mock getBoundingClientRect. We should have a utility to do that somewhere |
Bundle ReportChanges will increase total bundle size by 17.33kB (0.77%) ⬆️. This is within the configured threshold ✅ Detailed changes
Affected Assets, Files, and Routes:view changes for bundle: recharts/bundle-cjsAssets Changed:
|
But YAxis itself is not being rendered when I also used Am I missing something or is it some limitation of the testing library? |
Do you have any insight as of why is it not being rendered? If you add We have tests that show that YAxis does in fact render, for example in Coltin was recommending to use |
Summary
A fixed width of 60px is given to the y-axis by default. If the label size increases, it overflows. Added a prop named
isWidthResponsive
, which dynamically calculates the yAxis width based on the width of the ticks.Refer to this issue: #5861
Description
I have added a new prop named
isWidthResponsive
. When this is made true, it dynamically calculates the width of the y-axis when:label
prop is either a string/number or an object.label
prop is a function or a react element, default width of 60px is used as the old flow.The following logic is used to achieve the same:
label
prop is present, it calculates its width as well along withtickWidth
andtickMargin
.calculated width = maxWidth + tickWidth + tickMargin + 5px
. A 5px gap is for gap between the tick and the y-axis label.This PR rotates the y-axis label to 270 degrees by default as its the most commonly used layout.
Related Issue
#5861 #2027
Motivation and Context
This feature is long awaited and being asked for since 2017 - Refer #947. When I worked on it, I had to do a lot of customisations to achieve this but I feel this feature should be present by default.
How Has This Been Tested?
I have done manual testing by examining different charts in the storybook. Also, the automated test cases are used.
Screenshots (if appropriate):
Before:
After:

Without labels:

Types of changes
Checklist: