Skip to content

Conversation

@chihsuan
Copy link
Member

@chihsuan chihsuan commented Jan 19, 2026

Fixes WOOA7S-973

Proposed changes:

  • Add labelOverflow: 'ellipsis' option to AxisOptions type for bar charts
  • Create TruncatedTickComponent that uses SVG foreignObject to render HTML-based labels with CSS text-overflow ellipsis
  • Labels are automatically truncated to fit the available bandwidth from the scale
  • Full label text is shown on hover via native title attribute tooltip
  • Add Storybook story demonstrating the feature with before/after comparison

Why a custom TruncatedTickComponent?

SVG <text> elements (used by visx for axis labels) don't support CSS text-overflow: ellipsis. There's no pure CSS solution for truncating SVG text with ellipsis.

Approaches considered:

  1. tickLabelProps with scaleToFit: visx's <Text> component supports scaleToFit: 'shrink-only' which scales text to fit a width. However, it could cause labels to be too small to be readable.

  2. Custom tickComponent: This approach gives us access to DataContext from @visx/xychart, which provides xScale and yScale. From the scale, we can call bandwidth() to get the exact pixel width available for each category label.

Solution:
We use tickComponent with SVG <foreignObject> to embed an HTML <div> inside SVG. This allows us to use CSS text-overflow: ellipsis for proper text truncation, while accessing the scale's bandwidth for dynamic width calculation.

// Usage
<BarChart
  data={data}
  options={{
    axis: {
      x: { labelOverflow: 'ellipsis' }
    }
  }}
/>

Screenshots:

Screenshot 2026-01-19 at 15 20 20

Other information:

  • 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)?

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:

  1. Run Storybook: pnpm --filter @automattic/charts run storybook
  2. Navigate to JS Packages / Charts Library / Charts / Bar Chart / LabelOverflowEllipsis
  3. Verify:
    • The first chart shows overlapping labels (default behavior)
    • The second chart shows truncated labels with ellipsis
    • Hovering over truncated labels shows the full text in a tooltip
    • Test in safari

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]>
Copilot AI review requested due to automatic review settings January 19, 2026 07:21
@github-actions
Copy link
Contributor

github-actions bot commented Jan 19, 2026

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 add/bar-chart-label-overflow-ellipsis branch.
  • To test on Simple, run the following command on your sandbox:
bin/jetpack-downloader test jetpack add/bar-chart-label-overflow-ellipsis

Interested in more tips and information?

  • In your local development environment, use the jetpack rsync command 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

@github-actions
Copy link
Contributor

github-actions bot commented Jan 19, 2026

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • ✅ Add a "[Status]" label (In Progress, Needs Review, ...).
  • ✅ Add testing instructions.
  • ✅ Specify whether this PR includes any changes to data or privacy.
  • ✅ 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 🤖


Follow this PR Review Process:

  1. Ensure all required checks appearing at the bottom of this PR are passing.
  2. Make sure to test your changes on all platforms that it applies to. You're responsible for the quality of the code you ship.
  3. You can use GitHub's Reviewers functionality to request a review.
  4. 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!

@github-actions github-actions bot added the [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. label Jan 19, 2026
Copy link
Contributor

Copilot AI left a 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 to AxisOptions for bar chart axis configuration
  • Implement TruncatedTickComponent using 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

@chihsuan chihsuan self-assigned this Jan 19, 2026
chihsuan and others added 2 commits January 19, 2026 15:34
- 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]>
@jp-launch-control
Copy link

jp-launch-control bot commented Jan 19, 2026

Code Coverage Summary

Coverage changed in 1 file.

File Coverage Δ% Δ Uncovered
projects/js-packages/charts/src/charts/bar-chart/private/use-bar-chart-options.ts 23/25 (92.00%) 0.70% 0 💚

1 file is newly checked for coverage.

File Coverage
projects/js-packages/charts/src/charts/bar-chart/private/truncated-tick-component.tsx 26/27 (96.30%) 💚

Full summary · PHP report · JS report

- 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.
Copilot AI review requested due to automatic review settings January 19, 2026 07:51
Copy link
Contributor

Copilot AI left a 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.

Copilot AI review requested due to automatic review settings January 19, 2026 07:59
Copy link
Contributor

Copilot AI left a 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.

chihsuan and others added 2 commits January 19, 2026 16:06
- 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]>
@chihsuan chihsuan force-pushed the add/bar-chart-label-overflow-ellipsis branch from 1d32816 to ca007fa Compare January 19, 2026 08:11
…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.
Copilot AI review requested due to automatic review settings January 19, 2026 08:12
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.
Copy link
Contributor

Copilot AI left a 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.

- 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.
@chihsuan chihsuan added [Status] Needs Review This PR is ready for review. [Type] Task labels Jan 19, 2026
Copy link
Contributor

Copilot AI left a 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.

Copilot AI review requested due to automatic review settings January 19, 2026 08:54
Copy link
Contributor

Copilot AI left a 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.

Copilot AI review requested due to automatic review settings January 19, 2026 09:10
Copy link
Contributor

Copilot AI left a 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.

chihsuan and others added 2 commits January 19, 2026 17:19
- 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]>
Copilot AI review requested due to automatic review settings January 19, 2026 09:31
Copy link
Contributor

Copilot AI left a 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.

@chihsuan chihsuan requested review from a team January 19, 2026 09:35
Copilot AI review requested due to automatic review settings January 19, 2026 09:36
Copy link
Contributor

Copilot AI left a 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.

Copy link
Contributor

@dognose24 dognose24 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM and works as described on both Chrome and Safari! 👍🏼

Chrome Safari
截圖 2026-01-20 下午4 43 58 截圖 2026-01-20 下午4 43 46


// Map SVG textAnchor to CSS textAlign
let textAlign: 'left' | 'right' | 'center' = 'center';
if ( textAnchor === 'start' ) {
Copy link
Contributor

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?

Copy link
Member Author

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',
  } }
/>

@chihsuan chihsuan merged commit a871d06 into trunk Jan 20, 2026
91 checks passed
@chihsuan chihsuan deleted the add/bar-chart-label-overflow-ellipsis branch January 20, 2026 09:52
@github-actions github-actions bot removed the [Status] Needs Review This PR is ready for review. label Jan 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants