-
Notifications
You must be signed in to change notification settings - Fork 841
Charts: Minimal dynamic X-axis margin in LineChart to fit tick labels #45815
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?
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! |
Code Coverage SummaryCoverage changed in 1 file.
|
| expect( result.current.top ).toBe( 20 ); | ||
| // 12px font → ~15px line-height + 8 tick length + 8 padding = 31 | ||
| expect( result.current.top ).toBe( 31 ); | ||
| expect( result.current.bottom ).toBe( 10 ); |
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.
FAIL src/hooks/test/use-chart-margin.test.tsx
● useChartMargin › sets top and bottom margin for top x axis
expect(received).toBe(expected) // Object.is equality
Expected: 10
Received: 20
105 | // 12px font → ~15px line-height + 8 tick length + 8 padding = 31
106 | expect( result.current.top ).toBe( 31 );
> 107 | expect( result.current.bottom ).toBe( 10 );
| ^
108 | } );
109 |
110 | it( 'returns default margin if no axis options', () => {
at Object.toBe (src/hooks/test/use-chart-margin.test.tsx:107:35)
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.
Feel like bottom should be 20 here, but I'll leave it to @chihsuan for a second opinion.
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 think we should set it to 10, since having the orientation as top eliminates the need for a large bottom margin.
IMO, this line is unnecessary.
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.
Thanks for flagging this. The X‑axis margin logic has been updated so that for a top x‑axis the bottom margin is always DEFAULT_BOTTOM_FOR_TOP_AXIS = 10, independent of the dynamic computation; the test now asserts top === 20 (12px font + 8px tick) and bottom === 10, and it passes with the new implementation.
chihsuan
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.
Thanks for the PR! @chriskmnds I like the idea of respecting theming and different font sizes. 🙂
However, I think we should think more carefully about whether we want to add extra padding.
Before this PR, what we had was:
Now we have:
It does make sense to add padding between each chart and its container (card). However, IMO, it's better to let consumers decide how much padding they want between the chart and its container.
Additionally, this change might cause issues with Woo Analytics widgets, since we don't currently define explicit margins for the charts.
In other words, I think we should adjust the x-axis margin dynamically, but keep it just sufficient to prevent the chart from overlapping its container. If additional space is needed, consumers can add padding to the container as desired. Does that make sense?
| const xTickLength = | ||
| // Prefer axis-specific tick length when present, else theme fallback | ||
| xAxisStyles?.tickLength ?? themeWithX.tickLength ?? 8; | ||
| const xPadding = 8; |
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.
Would it be possible to move the hardcoded values (such as padding, line height, and default settings) into named constants, and add comments that clarify their purposes? This would improve readability and maintainability.
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.
Done. All previously hardcoded values have been moved into documented constants at the top, with brief comments describing what each controls. The hook body now refers to these constants rather than inline numbers.
| return isNaN( parsed ) ? undefined : parsed; | ||
| } | ||
| return undefined; | ||
| }; |
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.
Can we move this function out of useChartMargin? The hook is getting a bit too complex. It would be nice to split it up into smaller functions. An idea would be to extract all font size calculation logic into a separate function, similar to how we handle the Y axis.
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.
Agreed, that block was getting heavy. I’ve extracted the X‑axis font/tick logic into resolveFontSize( ... ) and getXAxisLabelMetrics( theme, orientation ), so useChartMargin now just calls the helper and then combines the metrics into margins. This mirrors the way Y‑axis width is delegated to getLongestTickWidth and keeps the hook itself much simpler.
| if ( xOrientation === 'top' ) { | ||
| // Preserve a small bottom margin for layout breathing room | ||
| defaultMargin.top = Math.max( defaultMargin.top, computedXMargin ); | ||
| defaultMargin.bottom = Math.max( defaultMargin.bottom, 10 ); |
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.
This is consistently assigned the value of defaultMargin.bottom because defaultMargin.bottom is 20. I think we should set the defaultMargin.bottom to 10.
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.
You’re right. Math.max( defaultMargin.bottom, 10 ) was effectively always 20. I’ve introduced DEFAULT_BOTTOM_FOR_TOP_AXIS = 10 and, in the top‑axis case, we now assign defaultMargin.bottom = DEFAULT_BOTTOM_FOR_TOP_AXIS directly. The bottom margin for a top X‑axis is now 10px instead of 20px.
|
Thanks for reviewing! @kangzj @chihsuan
There is a point to that, how a "raw" chart component would have zero paddings/margins. I am just proposing this as something done similarly for y-axis. Happy to abandon direction - in fact, I'd love it if one of you would take over if you'd like to entertain this further (being as it is that a shole team is in charge of the charts). I created a PR as I feared just an issue might sit longer in backlog. Let me know what your final take is - push forward, abandon, or leave it to your hands? :-) |
|
I tracked it with CHARTS-134: Optimize bottom/top margin to fit tick labels. I'll take a look this week. Thanks! |
9432b9d to
bc041d8
Compare
|
Thanks again for the review and for raising the concerns about extra padding and theme consistency. The Please review. Next, will run some tests and update the test cases to cover. |
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 PR look good and tests as described 👍
I don't see we allow axisStyles in our theme - the option is available for XYChartTheme only the moment, which means customized fontSize is not yet supported with our exposed API. However, if supported, we will need this.
I think we can merge the PR and then we could consider support for the ability to customize styles for axis in some ways(might not be necessary though. not 100% sure). It seems we will need our version of buildChartTheme. We should probably get rid of XYChart in the long run because we don't have full control over some functionalities otherwise.
Not sure what you think @adamwoodnz.
3666bcd to
a700d6b
Compare
|
Thanks again for working on this.
I increased the axis label font size using Axis labels aren't something we've spent much time on yet in terms of theming, so that's the only lever we have at present. As @kangzj mentioned we don't support theme |
|
Thanks @kangzj @adamwoodnz ! I did notice something odd about It sounds like I may need to give better focus here. Most definitely didn't test much. Thanks for testing! |

Fixes https://linear.app/a8c/issue/M4R73CH-1201/v2-hd-charts-on-monitoring-page-touch-the-card-border.
Proposed changes:
fontSize + tickLengthbased on the theme'saxisStyles.x.*/svgLabelSmall, with documented defaults when not present.bottom = max(DEFAULT_MARGIN_BOTTOM, fontSize + tickLength); for a top X-axis, usetop = max(DEFAULT_MARGIN_TOP, fontSize + tickLength)and a small fixedbottom = DEFAULT_BOTTOM_FOR_TOP_AXIS(10px).getLongestTickWidth) but introduce documented constants (DEFAULT_Y_TICK_WIDTH,DEFAULT_MARGIN_*) instead of magic numbers.resolveFontSize,getXAxisLabelMetrics) to simplifyuseChartMarginand align the structure with the Y-axis logic.Other information:
use-chart-margintests to cover bottom/top X-axis cases.)Jetpack product discussion
Does this pull request change what data or activity we track or use?
No.
Testing instructions:
pnpm -C projects/js-packages/charts testand verify tests pass.pnpm -C projects/js-packages/charts storybookand open Storybook.axis.x.orientation = 'top'in a LineChart story and verify the top margin increases appropriately while the bottom margin is a small 10px buffer.