-
Notifications
You must be signed in to change notification settings - Fork 8.6k
[Lens][XY] Truncate long axis tick labels in horizontal bar charts #268106
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
Merged
biamalveiro
merged 11 commits into
elastic:main
from
biamalveiro:feat/axis-tick-label-truncation
May 25, 2026
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
bd5d097
feat: wire axis tick truncation to elastic-charts in
biamalveiro 029ea56
feat: support relative truncation
biamalveiro 81f105c
feat: use tick label max length with relative size
biamalveiro a99bb0a
Merge branch 'main' into feat/axis-tick-label-truncation
biamalveiro 7fd97bf
remove settings from axis style
biamalveiro 94f91cf
Merge branch 'main' into feat/axis-tick-label-truncation
biamalveiro e0867a8
test: update snapshots
biamalveiro 2455b6d
refactor: revert edits in axis styles
biamalveiro a099966
fix: convert character truncate limits to pixels
biamalveiro 329cf8a
Merge branch 'main' into feat/axis-tick-label-truncation
biamalveiro 814c283
fix: use end trucation for legacy xy charts
biamalveiro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/platform/plugins/private/vis_types/xy/public/utils/truncate.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the "Elastic License | ||
| * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
| * Public License v 1"; you may not use this file except in compliance with, at | ||
| * your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|
|
||
| import { charsToPixels } from './truncate'; | ||
|
|
||
| describe('charsToPixels', () => { | ||
| it('returns undefined when truncate is missing or non-positive', () => { | ||
| expect(charsToPixels(undefined)).toBeUndefined(); | ||
| expect(charsToPixels(null)).toBeUndefined(); | ||
| expect(charsToPixels(0)).toBeUndefined(); | ||
| expect(charsToPixels(-1)).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('converts character counts to an approximate pixel width', () => { | ||
| expect(charsToPixels(100)).toBe(660); | ||
| }); | ||
|
|
||
| it('uses the provided font size', () => { | ||
| expect(charsToPixels(10, 12)).toBe(72); | ||
| }); | ||
| }); |
30 changes: 30 additions & 0 deletions
30
src/platform/plugins/private/vis_types/xy/public/utils/truncate.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the "Elastic License | ||
| * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
| * Public License v 1"; you may not use this file except in compliance with, at | ||
| * your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|
|
||
| /** | ||
| * Average glyph width relative to tick label font size (Open Sans–like axis labels). | ||
| */ | ||
| const CHAR_WIDTH_RATIO = 0.6; | ||
|
|
||
| /** Default Elastic Charts axis tick label font size used for conversion. */ | ||
| const DEFAULT_FONT_SIZE = 11; | ||
|
|
||
| /** | ||
| * Converts character count approximate width in pixels. | ||
| */ | ||
| export const charsToPixels = ( | ||
| truncate: number | null | undefined, | ||
| fontSize: number = DEFAULT_FONT_SIZE | ||
| ): number | undefined => { | ||
| if (truncate == null || truncate <= 0) { | ||
| return undefined; | ||
| } | ||
|
|
||
| return Math.round(truncate * fontSize * CHAR_WIDTH_RATIO); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
...ed/chart_expressions/expression_xy/public/components/__snapshots__/xy_chart.test.tsx.snap
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Legacy xy charts truncated axis tick labels by character count, and then in expression_xy, the formatter sliced tick strings to that length:
kibana/src/platform/plugins/shared/chart_expressions/expression_xy/public/components/xy_chart.tsx
Lines 986 to 992 in e2f0b1e
We now use elastic-charts's
tickLabelMaxLengthwhich expects a value in pixels.Without converting, a saved value like 100 would be treated as 100px instead of 100 characters, so labels would truncate much more aggressively than before.