Skip to content

feat(axis): add option to truncate axis tick labels#2829

Merged
biamalveiro merged 17 commits into
elastic:mainfrom
biamalveiro:feat/trim-bar-labels
May 19, 2026
Merged

feat(axis): add option to truncate axis tick labels#2829
biamalveiro merged 17 commits into
elastic:mainfrom
biamalveiro:feat/trim-bar-labels

Conversation

@biamalveiro
Copy link
Copy Markdown
Contributor

@biamalveiro biamalveiro commented May 4, 2026

Summary

Adds ability to set a maximum length for axis tick labels, and truncate in case of overflow. Truncation can be configured to happen at start, middle or end of the string.

Details

AxisStyle.tickLabel config has two new optional properties: maxLength (Pixels, or a string such as 80%, in relation to the container's width) and truncate (end, start, or middle). Formatters are wrapped with withTickLabelTruncation, so they are truncated if they overflow.

Additionally, this PR extends fitText to support truncation at start or middle of string, not only trailing ellipsis (end). This updated implementation searches for fit based on what is drawn (including the ... when text is clipped), while the previous used the length of the prefix before swapping last character with an ellipsis. This led to ~1 character differences in snapshots that used text truncation, (when width(slice(0, v)) is different from width(slice(0, v−1) + …)). Updated VRT to fix these misses.

Added a story where this behavior can be tested.

Scope / follow-ups

This is intentionally a first pass: truncation is applied at the formatter using a max length in pixels (and optional ellipsis position), not a full label-box sizing model.

An idea of having minSize / size / maxSize on axis labels for more control was discussed but needs more thought, as it goes into labelBox calculations and we need to consider rotated labels and vertical/horizontal axis. Scoped out for this change, but to be revisited later.

Issues

Related to elastic/kibana#265517

Checklist

  • The proper feature labels have been added (e.g. :interactions, :axis)
  • All related issues have been linked (i.e. closes #123, fixes #123)
  • New public API exports have been added to packages/charts/src/index.ts
  • Unit tests have been added or updated to match the most common scenarios
  • The proper documentation and/or storybook story has been added or updated

@biamalveiro biamalveiro force-pushed the feat/trim-bar-labels branch from f68416b to b1ce36e Compare May 5, 2026 07:52
@biamalveiro
Copy link
Copy Markdown
Contributor Author

buildkite update vrt

@biamalveiro biamalveiro added the :axis Axis related issue label May 5, 2026
@biamalveiro biamalveiro marked this pull request as ready for review May 5, 2026 11:15
@biamalveiro biamalveiro requested a review from a team as a code owner May 5, 2026 11:15
Copy link
Copy Markdown
Collaborator

@nickofthyme nickofthyme left a comment

Choose a reason for hiding this comment

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

Sorry for the delay, I wanted to really understand the logic and test it out.

I left a few things to check out and resolve before merging but nothing real major.

Really nice PR! 🚀

Comment thread packages/charts/api/charts.api.md
Comment thread packages/charts/src/common/text_utils.ts Outdated
Comment thread packages/charts/src/utils/themes/theme.ts Outdated
Comment thread packages/charts/src/renderers/canvas/primitives/text.ts Outdated
@biamalveiro biamalveiro marked this pull request as draft May 11, 2026 13:48
@biamalveiro biamalveiro force-pushed the feat/trim-bar-labels branch from 99aac32 to 647d820 Compare May 11, 2026 14:14
@biamalveiro biamalveiro marked this pull request as ready for review May 11, 2026 14:21
@biamalveiro
Copy link
Copy Markdown
Contributor Author

buildkite test this

@biamalveiro
Copy link
Copy Markdown
Contributor Author

buildkite update vrt

@biamalveiro
Copy link
Copy Markdown
Contributor Author

buildkite update vrt

Copy link
Copy Markdown
Collaborator

@nickofthyme nickofthyme left a comment

Choose a reason for hiding this comment

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

I added a few more comments. I'm a little more doubtful about applying this to both axes instead of just the vertical axis, at least for this first pass to close the kibana issue. Also think it may be better to split the setting to control horizontal and vertical truncation separately.

Comment thread packages/charts/src/chart_types/xy_chart/state/selectors/axis_tick_formatter.ts Outdated
Comment thread storybook/stories/axes/16_tick_label_truncation.story.tsx Outdated
Comment thread storybook/stories/axes/16_tick_label_truncation.story.tsx Outdated
@nickofthyme nickofthyme self-requested a review May 13, 2026 03:54
@biamalveiro biamalveiro force-pushed the feat/trim-bar-labels branch 3 times, most recently from 058c5a0 to 907e944 Compare May 13, 2026 13:50
@biamalveiro biamalveiro force-pushed the feat/trim-bar-labels branch from 907e944 to 2792327 Compare May 13, 2026 14:24
@biamalveiro biamalveiro force-pushed the feat/trim-bar-labels branch from 2f05ea9 to 5bd1830 Compare May 15, 2026 14:06
@elastic-datavis
Copy link
Copy Markdown
Contributor

Copy link
Copy Markdown
Collaborator

@nickofthyme nickofthyme left a comment

Choose a reason for hiding this comment

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

This looks great! Sorry for all the back and forth. In the end, I think it ended up in a good spot.

@biamalveiro
Copy link
Copy Markdown
Contributor Author

Thanks @nickofthyme for having a thorough look at this + helping think through and improve the solution!

@biamalveiro biamalveiro merged commit e0c8e72 into elastic:main May 19, 2026
14 checks passed
nickofthyme pushed a commit that referenced this pull request May 20, 2026
# [71.7.0](v71.6.1...v71.7.0) (2026-05-20)

### Features

* **axis:** add option to truncate axis tick labels ([#2829](#2829)) ([e0c8e72](e0c8e72))
biamalveiro added a commit to elastic/kibana that referenced this pull request May 21, 2026
## Summary

This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [@elastic/charts](https://togithub.com/elastic/elastic-charts) |
dependencies | minor | [`71.6.1` ->
`71.7.0`](elastic/elastic-charts@v71.6.1...v71.7.0)
|

### Release Notes
####
[71.7.0](elastic/elastic-charts@v71.6.1...v71.7.0)
(2026-05-20)

Features

•   ​axis:​ add option to truncate axis tick labels
([#2829](elastic/elastic-charts#2829))
([e0c8e72](elastic/elastic-charts@e0c8e72))
paulinashakirova pushed a commit to paulinashakirova/kibana that referenced this pull request May 22, 2026
## Summary

This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [@elastic/charts](https://togithub.com/elastic/elastic-charts) |
dependencies | minor | [`71.6.1` ->
`71.7.0`](elastic/elastic-charts@v71.6.1...v71.7.0)
|

### Release Notes
####
[71.7.0](elastic/elastic-charts@v71.6.1...v71.7.0)
(2026-05-20)

Features

•   ​axis:​ add option to truncate axis tick labels
([elastic#2829](elastic/elastic-charts#2829))
([e0c8e72](elastic/elastic-charts@e0c8e72))
jcger pushed a commit to elastic/kibana that referenced this pull request May 26, 2026
## Summary

This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [@elastic/charts](https://togithub.com/elastic/elastic-charts) |
dependencies | minor | [`71.6.1` ->
`71.7.0`](elastic/elastic-charts@v71.6.1...v71.7.0)
|

### Release Notes
####
[71.7.0](elastic/elastic-charts@v71.6.1...v71.7.0)
(2026-05-20)

Features

•   ​axis:​ add option to truncate axis tick labels
([#2829](elastic/elastic-charts#2829))
([e0c8e72](elastic/elastic-charts@e0c8e72))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

:axis Axis related issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants