Charts: Fix chart height calculation to include legend height
Proposed changes:
- Fix container height calculation in BarChart, LineChart, PieChart, and PieSemiCircleChart components
- Update container height to be
height + (showLegend ? legendHeight : 0)to properly accommodate legend - Ensure chart content respects the specified height prop while legend is rendered in additional space
- Add test to verify proper height behavior when legend is displayed
Other information:
- [x] Have you written new tests for your changes, if applicable?
- [ ] Have you checked the E2E test CI results, and verified that your changes do not break them?
- [ ] Have you tested your changes on WordPress.com, if applicable (if so, you'll see a generated comment below with a script to run)?
Does this pull request change what data or activity we track or use?
No, it does not.
Testing instructions:
- Go to Storybook for any of the affected charts (BarChart, LineChart, PieChart, PieSemiCircleChart)
- Set a specific height value (e.g., 300px)
- Enable the legend via
showLegend: true - Verify that:
- The chart content area maintains the specified height
- The legend appears below (or above if
legendPosition: 'top') without reducing chart height - The total container height is taller than the specified height to accommodate the legend
- Test with different legend positions (
topandbottom) - Verify no visual regressions in chart rendering
- Confirm that charts without legends maintain their original height behavior
- Run the test suite to ensure all tests pass
Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.
- To test on WoA, go to the Plugins menu on a WoA dev site. Click on the "Upload" button and follow the upgrade flow to be able to upload, install, and activate the Jetpack Beta plugin. Once the plugin is active, go to Jetpack > Jetpack Beta, select your plugin (Jetpack), and enable the
fix/charts-105-fix-chart-height-calculation-to-include-legend-heightbranch. - To test on Simple, run the following command on your sandbox:
bin/jetpack-downloader test jetpack fix/charts-105-fix-chart-height-calculation-to-include-legend-height
Interested in more tips and information?
- In your local development environment, use the
jetpack rsynccommand to sync your changes to a WoA dev blog. - Read more about our development workflow here: PCYsg-eg0-p2
- Figure out when your changes will be shipped to customers here: PCYsg-eg5-p2
Thank you for your PR!
When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:
- :white_check_mark: Include a description of your PR changes.
- :white_check_mark: Add a "[Status]" label (In Progress, Needs Review, ...).
- :white_check_mark: Add a "[Type]" label (Bug, Enhancement, Janitorial, Task).
- :white_check_mark: Add testing instructions.
- :white_check_mark: Specify whether this PR includes any changes to data or privacy.
- :white_check_mark: Add changelog entries to affected projects
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 :robot:
Follow this PR Review Process:
- Ensure all required checks appearing at the bottom of this PR are passing.
- Make sure to test your changes on all platforms that it applies to. You're responsible for the quality of the code you ship.
- You can use GitHub's Reviewers functionality to request a review.
- When it's reviewed and merged, you will be pinged in Slack to deploy the changes to WordPress.com simple once the build is done.
If you have questions about anything, reach out in #jetpack-developers for guidance!
Code Coverage Summary
This 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. 🤷
In my testing, the
.bar-chartcontainer 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 to400pxinstead?
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?
https://github.com/Automattic/jetpack/blob/710c54f9faf19dfc7159d867edeafa56f5e7af6c/projects/js-packages/charts/src/components/private/with-responsive/with-responsive.tsx#L49
Set a specific height value (e.g., 300px) Enable the legend via showLegend: true Verify that: The chart content area maintains the specified height The legend appears below (or above if legendPosition: 'top') without reducing chart height
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.
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.
https://github.com/user-attachments/assets/d18a2ffd-e66b-4e4f-95f7-f4b1ae550d30
https://github.com/user-attachments/assets/3965e706-4220-45f5-8c02-81963d979054
How the margins are calculated should probably be revisited too I think 🤔
I would expect something like the following, basically what bar chart is doing before this PR.
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:
- The consumer has a flexible space that the chart needs to fit in (eg. a Woo Analytics dashboard tile). They don't want to specify a height or width, and expect the chart to fill the available area. The size of the chart will need to be calculated based on the parent element size and the legend visibility and size. The chart should be responsive to changes in the parent size.
- The consumer wants a fixed height and/or width. They expect the chart to fill this space and not exceed it. The size of the chart will need to be calculated based on the specified dimension(s) and the legend visibility and size. The chart should not be responsive to changes in the parent size, and it is the consumer's responsibility to update the specified size.
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:
- It has been inactive for the past 3 months.
- It hasn't been labeled `[Pri] BLOCKER`, `[Pri] High`, `[Status] Keep Open`, etc.
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.