-
Notifications
You must be signed in to change notification settings - Fork 849
Charts: Add labelOverflow ellipsis option for bar chart axis labels #46656
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
When bar charts are narrow, long categorical axis labels overlap and become unreadable. This adds a `labelOverflow: 'ellipsis'` option to axis configuration that truncates labels to fit the available bandwidth, with full text shown on hover via tooltip. Co-Authored-By: Claude Opus 4.5 <[email protected]>
|
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 adds a labelOverflow: 'ellipsis' option to the bar chart's axis configuration, enabling automatic truncation of long axis labels to fit within the available bandwidth. The feature uses SVG foreignObject to render HTML-based labels with CSS text-overflow ellipsis and displays the full text on hover via native tooltips.
Changes:
- Add
labelOverflow?: 'ellipsis'type toAxisOptionsfor bar chart axis configuration - Implement
TruncatedTickComponentusing SVG foreignObject with CSS ellipsis for label truncation - Integrate the component conditionally when
labelOverflow: 'ellipsis'is specified - Add Storybook story demonstrating the feature with before/after comparison
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 8 comments.
Show a summary per file
| File | Description |
|---|---|
| projects/js-packages/charts/src/types.ts | Adds labelOverflow property to AxisOptions type with documentation |
| projects/js-packages/charts/src/charts/bar-chart/private/truncated-tick-component.tsx | Implements the truncated tick component using foreignObject and CSS ellipsis |
| projects/js-packages/charts/src/charts/bar-chart/private/use-bar-chart-options.ts | Conditionally applies truncated tick component when labelOverflow is 'ellipsis' |
| projects/js-packages/charts/src/charts/bar-chart/private/index.ts | Exports the new truncated tick component |
| projects/js-packages/charts/src/charts/bar-chart/stories/index.stories.tsx | Adds Storybook story demonstrating the labelOverflow feature |
| projects/js-packages/charts/changelog/add-bar-chart-label-overflow-ellipsis | Adds changelog entry for the new feature |
projects/js-packages/charts/src/charts/bar-chart/private/truncated-tick-component.tsx
Outdated
Show resolved
Hide resolved
projects/js-packages/charts/src/charts/bar-chart/private/truncated-tick-component.tsx
Show resolved
Hide resolved
projects/js-packages/charts/src/charts/bar-chart/private/truncated-tick-component.tsx
Outdated
Show resolved
Hide resolved
projects/js-packages/charts/src/charts/bar-chart/private/truncated-tick-component.tsx
Show resolved
Hide resolved
projects/js-packages/charts/src/charts/bar-chart/private/truncated-tick-component.tsx
Show resolved
Hide resolved
projects/js-packages/charts/src/charts/bar-chart/private/use-bar-chart-options.ts
Outdated
Show resolved
Hide resolved
projects/js-packages/charts/src/charts/bar-chart/private/use-bar-chart-options.ts
Outdated
Show resolved
Hide resolved
projects/js-packages/charts/src/charts/bar-chart/private/truncated-tick-component.tsx
Outdated
Show resolved
Hide resolved
- Fix textAlign mapping to handle 'middle' textAnchor (maps to 'center') - Fix xOffset calculation based on actual text alignment - Add JSDoc for MINI_TICK_LABEL_LENGTH constant - Only set title attribute when formattedValue has content (avoid empty tooltips) - Add aria-label for screen reader accessibility - Improve type safety by explicitly mapping compatible SVG text props to CSS Co-Authored-By: Claude Opus 4.5 <[email protected]>
Code Coverage SummaryCoverage changed in 1 file.
1 file is newly checked for coverage.
|
- Extracted labelOverflow from axis options for both x and y axes. - Updated axis configuration to utilize extracted options for better readability and maintainability. - Ensured compatibility with the existing labelOverflow ellipsis feature. This change enhances the clarity of the axis configuration and prepares for future enhancements.
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
Copilot reviewed 6 out of 6 changed files in this pull request and generated 7 comments.
projects/js-packages/charts/src/charts/bar-chart/private/index.ts
Outdated
Show resolved
Hide resolved
projects/js-packages/charts/changelog/add-bar-chart-label-overflow-ellipsis
Outdated
Show resolved
Hide resolved
projects/js-packages/charts/src/charts/bar-chart/private/truncated-tick-component.tsx
Show resolved
Hide resolved
projects/js-packages/charts/src/charts/bar-chart/private/truncated-tick-component.tsx
Outdated
Show resolved
Hide resolved
projects/js-packages/charts/src/charts/bar-chart/private/use-bar-chart-options.ts
Outdated
Show resolved
Hide resolved
projects/js-packages/charts/src/charts/bar-chart/private/truncated-tick-component.tsx
Outdated
Show resolved
Hide resolved
projects/js-packages/charts/src/charts/bar-chart/private/truncated-tick-component.tsx
Outdated
Show resolved
Hide resolved
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
Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.
projects/js-packages/charts/changelog/add-bar-chart-label-overflow-ellipsis
Show resolved
Hide resolved
projects/js-packages/charts/src/charts/bar-chart/private/truncated-tick-component.tsx
Outdated
Show resolved
Hide resolved
- Renamed constant MINI_TICK_LABEL_LENGTH to MIN_TICK_LABEL_WIDTH for clarity. - Updated logic to use the new constant for determining maximum tick label width. - Introduced a comprehensive test suite for label overflow ellipsis functionality, ensuring proper rendering and accessibility features for long labels in bar charts. This update improves the readability of the code and ensures that long labels are handled gracefully in various chart orientations.
Add documentation explaining the trade-off when bandwidth is less than the minimum label width (20px): labels may overlap on very dense charts. Include mitigation strategies (numTicks, tickFormat, chart sizing). Co-Authored-By: Claude Opus 4.5 <[email protected]>
1d32816 to
ca007fa
Compare
…tion Corrected punctuation in the changelog entry for the labelOverflow ellipsis feature, ensuring clarity in the description of the functionality that truncates long axis labels for bar charts.
Removed the export of TruncatedTickComponent from the bar chart index file, simplifying the module's interface and focusing on the essential exports for better 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.
Pull request overview
Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.
projects/js-packages/charts/src/charts/bar-chart/private/truncated-tick-component.tsx
Outdated
Show resolved
Hide resolved
projects/js-packages/charts/src/charts/bar-chart/test/bar-chart.test.tsx
Outdated
Show resolved
Hide resolved
- Adjusted the transform property to align HTML <div> elements within <foreignObject> to match SVG <text> vertical alignment. - Updated the position style to 'fixed' to enhance compatibility across browsers, particularly Safari. - Removed redundant transform logic to streamline the component's rendering. These changes enhance the visual consistency of tick labels in bar charts, ensuring they are displayed correctly across different environments.
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
Copilot reviewed 7 out of 7 changed files in this pull request and generated 5 comments.
projects/js-packages/charts/src/charts/bar-chart/stories/index.stories.tsx
Outdated
Show resolved
Hide resolved
projects/js-packages/charts/src/charts/bar-chart/private/truncated-tick-component.tsx
Outdated
Show resolved
Hide resolved
projects/js-packages/charts/src/charts/bar-chart/private/truncated-tick-component.tsx
Outdated
Show resolved
Hide resolved
projects/js-packages/charts/src/charts/bar-chart/private/truncated-tick-component.tsx
Show resolved
Hide resolved
projects/js-packages/charts/changelog/add-bar-chart-label-overflow-ellipsis
Show resolved
Hide resolved
….stories.tsx Co-authored-by: Copilot <[email protected]>
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
Copilot reviewed 7 out of 7 changed files in this pull request and generated 4 comments.
projects/js-packages/charts/src/charts/bar-chart/private/truncated-tick-component.tsx
Outdated
Show resolved
Hide resolved
projects/js-packages/charts/changelog/add-bar-chart-label-overflow-ellipsis
Show resolved
Hide resolved
projects/js-packages/charts/src/charts/bar-chart/private/truncated-tick-component.tsx
Outdated
Show resolved
Hide resolved
projects/js-packages/charts/src/charts/bar-chart/stories/index.stories.tsx
Show resolved
Hide resolved
….stories.tsx Co-authored-by: Copilot <[email protected]>
…ated-tick-component.tsx Co-authored-by: Copilot <[email protected]>
…ated-tick-component.tsx Co-authored-by: Copilot <[email protected]>
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
Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.
projects/js-packages/charts/src/charts/bar-chart/private/truncated-tick-component.tsx
Outdated
Show resolved
Hide resolved
- Pre-create TruncatedXTickComponent and TruncatedYTickComponent at module level to prevent component recreation on every render - Apply Safari position:fixed workaround only on Safari browsers using existing isSafari() utility - Update exports and imports to use memoized component instances Co-Authored-By: Claude Opus 4.5 <[email protected]>
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
Copilot reviewed 7 out of 7 changed files in this pull request and generated 4 comments.
projects/js-packages/charts/src/charts/bar-chart/private/truncated-tick-component.tsx
Outdated
Show resolved
Hide resolved
projects/js-packages/charts/src/charts/bar-chart/private/truncated-tick-component.tsx
Show resolved
Hide resolved
projects/js-packages/charts/src/charts/bar-chart/private/truncated-tick-component.tsx
Outdated
Show resolved
Hide resolved
…ated-tick-component.tsx Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
…ated-tick-component.tsx Co-authored-by: Copilot <[email protected]>
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
Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.
dognose24
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.
|
|
||
| // Map SVG textAnchor to CSS textAlign | ||
| let textAlign: 'left' | 'right' | 'center' = 'center'; | ||
| if ( textAnchor === 'start' ) { |
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.
Just curious: how do we set the textAnchor prop for a Bar Chart Label when testing?
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.
Our interface doesn’t currently support the textAnchor prop, but you can apply it by customizing the bar chart:
<Axis
{ ...chartOptions.axis.x }
tickLabelProps={ {
textAnchor: 'start',
} }
/>

Fixes WOOA7S-973
Proposed changes:
labelOverflow: 'ellipsis'option toAxisOptionstype for bar chartsTruncatedTickComponentthat uses SVGforeignObjectto render HTML-based labels with CSS text-overflow ellipsistitleattribute tooltipWhy a custom
TruncatedTickComponent?SVG
<text>elements (used by visx for axis labels) don't support CSStext-overflow: ellipsis. There's no pure CSS solution for truncating SVG text with ellipsis.Approaches considered:
tickLabelPropswithscaleToFit: visx's<Text>component supportsscaleToFit: 'shrink-only'which scales text to fit a width. However, it could cause labels to be too small to be readable.Custom
tickComponent: This approach gives us access toDataContextfrom@visx/xychart, which providesxScaleandyScale. From the scale, we can callbandwidth()to get the exact pixel width available for each category label.Solution:
We use
tickComponentwith SVG<foreignObject>to embed an HTML<div>inside SVG. This allows us to use CSStext-overflow: ellipsisfor proper text truncation, while accessing the scale's bandwidth for dynamic width calculation.Screenshots:
Other information:
Jetpack product discussion
N/A - This is a charts library enhancement.
Does this pull request change what data or activity we track or use?
No.
Testing instructions:
pnpm --filter @automattic/charts run storybook