-
Notifications
You must be signed in to change notification settings - Fork 842
Charts: Fix chart height calculation to include legend height #45175
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: trunk
Are you sure you want to change the base?
Charts: Fix chart height calculation to include legend height #45175
Conversation
|
Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.
Interested in more tips and information?
|
|
Thank you for your PR! When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:
This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖 Follow this PR Review Process:
If you have questions about anything, reach out in #jetpack-developers for guidance! |
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 fixes chart height calculation in chart components to properly accommodate legend height. When a legend is displayed, the container height is now calculated as height + legendHeight instead of just height, ensuring the chart content maintains its specified height while the legend appears in additional space.
Key changes:
- Updated container height calculation in BarChart, LineChart, PieChart, and PieSemiCircleChart components
- Modified the height style property to include legend height when
showLegendis true - Added test coverage to verify proper height behavior when legend is displayed
Reviewed Changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| bar-chart.tsx | Updated container height to include legend height |
| line-chart.tsx | Modified height calculation to account for legend space |
| pie-chart.tsx | Fixed container height to accommodate legend |
| pie-semi-circle-chart.tsx | Added legend height to container height calculation |
| bar-chart.test.tsx | Added test to verify container height accounts for legend |
| changelog | Added changelog entry for the height calculation fix |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
Code Coverage SummaryThis PR did not change code coverage! That could be good or bad, depending on the situation. Everything covered before, and still is? Great! Nothing was covered before? Not so great. 🤷 |
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.
The chart content area maintains the specified height
In my testing, the .bar-chart container is expanded (420px) to the specified height (400px) plus the legend height (20px), but the chart section remains the original height (380px) of the specified height minus the legend height, showing an extra gap at the bottom. Should the chart height be expanded to 400px instead?
| Before | After |
|---|---|
![]() |
![]() |
Thanks for testing this out! I think you might have a good point, I am going to revisit this :) |
|
Could it be related to the ratio of the withResponsive decorator? jetpack/projects/js-packages/charts/src/components/private/with-responsive/with-responsive.tsx Line 49 in 710c54f
|
I don't know whether we should be after this to be honest. AFAICS, the height we accept should be the overall height of all visuals, including both the chart and the legend. Thoughts 🤔 |
|
Thanks for your thoughts Jasper. I am still trying to wrap my head around exactly what we want. But I think it makes the most semantic sense to me that the height we accept should be the overall heigh of all visuals, so both chart + legend. |
bd883e1 to
b677a8b
Compare
|
I would expect something like the following, basically what bar chart is doing before this PR. However when the provided hight is greater than the required hight, taking up partial the space should be fine. Screen.Recording.2025-09-22.at.1.15.34.PM.movScreen.Recording.2025-09-22.at.1.20.08.PM.movHow the margins are calculated should probably be revisited too I think 🤔 |
This is essentially what it's already doing without this PR. I'm trying to understand if there's value in pursuing this one right now or if we should close it up. The issue it stems from the statement "Height of chart isn't respecting the props, meaning height of legend isn't included in calculating the height " I'm wondering if this would be better off as a very minor one-liner change to be something like only updating container height to be height + (showLegend ? legendHeight : 0) or potentially combining that with re-evaluaton of margin calculations? |
|
I've been thinking about this and I feel like there are 2 scenarios:
In the case of Woo Analytics it may be desirable to be able to specify the exact height of just the chart, so that for example all the pie charts in the dashboard are the same size, rather than differing due to the number of legend items: In this case where they have not specified an overall size but have specified a chart size, we should respect that height up until the point where it doesn't fit the parent space, and then reduce it to fit. Does this make sense? I think we can achieve this using some sort of shared layout wrapper using a flexbox column and a ResizeObserver. It will need to know about legend visibility and position. I'm not sure at this point how exactly that will play with the existing withResponsive wrapper. Related: |
|
This PR has been marked as stale. This happened because:
If this PR is still useful, please do a [trunk merge or rebase](https://github.com/Automattic/jetpack/blob/trunk/docs/git-workflow.md#keeping-your-branch-up-to-date) and otherwise make sure it's up to date and has clear testing instructions. You may also want to ping possible reviewers in case they've forgotten about it. Please close this PR if you think it's not valid anymore — if you do, please add a brief explanation. If the PR is not updated (or at least commented on) in another month, it will be automatically closed. |



Proposed changes:
height + (showLegend ? legendHeight : 0)to properly accommodate legendOther information:
Does this pull request change what data or activity we track or use?
No, it does not.
Testing instructions:
showLegend: truelegendPosition: 'top') without reducing chart heighttopandbottom)