-
Notifications
You must be signed in to change notification settings - Fork 134
feat(axis): add option to truncate axis tick labels #2829
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
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
30727ea
feat: add tick label truncation from maxWidth
biamalveiro 898edca
feat: allow truncate in start, end or middle
biamalveiro 578fc7c
chore: move story from bar to axes group
biamalveiro b1ce36e
feat: add screenshot test
biamalveiro 9999a25
test(vrt): update screenshots [skip ci]
elastic-datavis[bot] d208aa0
test: add unit tests for fitText
biamalveiro 1a42ab9
chore: update api
biamalveiro c78ad3d
fix: export ELLIPSIS definition
biamalveiro 4ddbf89
fix: remove ellipsis adjustment in partition
biamalveiro eb3db71
feat: support relative tick label truncation limits
biamalveiro 7fdaf55
feat: simplify length limit implementation
biamalveiro 647d820
fix: circular dep
biamalveiro 3e93aa4
Merge branch 'main' into feat/trim-bar-labels
biamalveiro f22ab83
test(vrt): update screenshots [skip ci]
elastic-datavis[bot] edbfcf1
test: add tests for withAxisTickLabelTruncation
biamalveiro 2792327
test: split knobs in axis tick truncation story
biamalveiro 5bd1830
refactor: remove tick label truncation parameters from theme to axis …
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
Binary file added
BIN
+14.3 KB
...ots/all.test.ts-snapshots/baselines/axes/tick-label-truncation-chrome-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+69 Bytes
(100%)
...hots/all.test.ts-snapshots/baselines/stylings/partition-labels-chrome-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-162 Bytes
(100%)
...shots/all.test.ts-snapshots/baselines/sunburst/bold-link-value-chrome-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+53 Bytes
(100%)
...test.ts-snapshots/baselines/sunburst/counter-clockwise-special-chrome-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+406 Bytes
(100%)
...t.ts-snapshots/baselines/sunburst/donut-chart-with-fill-labels-chrome-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+588 Bytes
(100%)
...ts/all.test.ts-snapshots/baselines/sunburst/linked-labels-only-chrome-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+36 Bytes
(100%)
...creenshots/all.test.ts-snapshots/baselines/sunburst/most-basic-chrome-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+354 Bytes
(100%)
e2e/screenshots/all.test.ts-snapshots/baselines/sunburst/negative-chrome-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+354 Bytes
(100%)
...eenshots/all.test.ts-snapshots/baselines/sunburst/not-a-number-chrome-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+287 Bytes
(100%)
...all.test.ts-snapshots/baselines/sunburst/some-zero-value-slice-chrome-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+200 Bytes
(100%)
...elines/sunburst/value-formatted-with-categorical-color-palette-chrome-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+341 Bytes
(100%)
...hots/all.test.ts-snapshots/baselines/sunburst/with-fill-labels-chrome-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+419 Bytes
(100%)
...l.test.ts-snapshots/baselines/sunburst/with-linked-text-labels-chrome-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+40 Bytes
(100%)
...l-label-textcolor-sunburst/should-show-custom-black-text-color-chrome-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+39 Bytes
(100%)
...color-sunburst/should-show-custom-black-translucent-text-color-chrome-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+43 Bytes
(100%)
...ill-label-textcolor-sunburst/should-show-custom-red-text-color-chrome-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+48 Bytes
(100%)
...l-label-textcolor-sunburst/should-show-custom-white-text-color-chrome-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+45 Bytes
(100%)
...color-sunburst/should-show-custom-white-translucent-text-color-chrome-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+546 Bytes
(100%)
...label-textcolor-dark-theme/should-show-custom-black-text-color-chrome-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+425 Bytes
(100%)
...lor-dark-theme/should-show-custom-black-translucent-text-color-chrome-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+680 Bytes
(100%)
...k-label-textcolor-dark-theme/should-show-custom-red-text-color-chrome-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+684 Bytes
(100%)
...label-textcolor-dark-theme/should-show-custom-white-text-color-chrome-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+619 Bytes
(100%)
...lor-dark-theme/should-show-custom-white-translucent-text-color-chrome-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+733 Bytes
(100%)
...link-label-textcolor-dark-theme/should-show-default-text-color-chrome-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+588 Bytes
(100%)
...abel-textcolor-light-theme/should-show-custom-black-text-color-chrome-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+537 Bytes
(100%)
...or-light-theme/should-show-custom-black-translucent-text-color-chrome-linux.png
Oops, something went wrong.
Binary file modified
BIN
+615 Bytes
(100%)
...-label-textcolor-light-theme/should-show-custom-red-text-color-chrome-linux.png
Oops, something went wrong.
Binary file modified
BIN
+686 Bytes
(100%)
...ink-label-textcolor-light-theme/should-show-default-text-color-chrome-linux.png
Oops, something went wrong.
Binary file modified
BIN
+135 Bytes
(100%)
...uld-render-link-labels-with-fallback-text-color-for-dark-theme-chrome-linux.png
Oops, something went wrong.
Binary file modified
BIN
+200 Bytes
(100%)
...ld-render-link-labels-with-fallback-text-color-for-light-theme-chrome-linux.png
Oops, something went wrong.
Binary file modified
BIN
+434 Bytes
(100%)
...art-type/lang-ar/ltr-text/should-render-with-correct-direction-chrome-linux.png
Oops, something went wrong.
Binary file modified
BIN
+434 Bytes
(100%)
...tr-text/should-render-with-correct-direction-clockwise-sectors-chrome-linux.png
Oops, something went wrong.
Binary file modified
BIN
+379 Bytes
(100%)
...e/lang-ar/mostly-ltr-text/should-render-with-correct-direction-chrome-linux.png
Oops, something went wrong.
Binary file modified
BIN
+458 Bytes
(100%)
...tr-text/should-render-with-correct-direction-clockwise-sectors-chrome-linux.png
Oops, something went wrong.
Binary file modified
BIN
+278 Bytes
(100%)
...e/lang-ar/mostly-rtl-text/should-render-with-correct-direction-chrome-linux.png
Oops, something went wrong.
Binary file modified
BIN
+261 Bytes
(100%)
...tl-text/should-render-with-correct-direction-clockwise-sectors-chrome-linux.png
Oops, something went wrong.
Binary file modified
BIN
+252 Bytes
(100%)
...art-type/lang-ar/rtl-text/should-render-with-correct-direction-chrome-linux.png
Oops, something went wrong.
Binary file modified
BIN
+262 Bytes
(100%)
...tl-text/should-render-with-correct-direction-clockwise-sectors-chrome-linux.png
Oops, something went wrong.
Binary file modified
BIN
+434 Bytes
(100%)
...art-type/lang-he/ltr-text/should-render-with-correct-direction-chrome-linux.png
Oops, something went wrong.
Binary file modified
BIN
+434 Bytes
(100%)
...tr-text/should-render-with-correct-direction-clockwise-sectors-chrome-linux.png
Oops, something went wrong.
Binary file modified
BIN
+455 Bytes
(100%)
...e/lang-he/mostly-ltr-text/should-render-with-correct-direction-chrome-linux.png
Oops, something went wrong.
Binary file modified
BIN
+460 Bytes
(100%)
...tr-text/should-render-with-correct-direction-clockwise-sectors-chrome-linux.png
Oops, something went wrong.
Binary file modified
BIN
+455 Bytes
(100%)
...e/lang-he/mostly-rtl-text/should-render-with-correct-direction-chrome-linux.png
Oops, something went wrong.
Binary file modified
BIN
+397 Bytes
(100%)
...tl-text/should-render-with-correct-direction-clockwise-sectors-chrome-linux.png
Oops, something went wrong.
Binary file modified
BIN
+398 Bytes
(100%)
...art-type/lang-he/rtl-text/should-render-with-correct-direction-chrome-linux.png
Oops, something went wrong.
Binary file modified
BIN
+381 Bytes
(100%)
...tl-text/should-render-with-correct-direction-clockwise-sectors-chrome-linux.png
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
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
58 changes: 58 additions & 0 deletions
58
packages/charts/src/chart_types/xy_chart/state/selectors/axis_tick_formatter.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,58 @@ | ||
| /* | ||
| * 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 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 or the Server | ||
| * Side Public License, v 1. | ||
| */ | ||
|
|
||
| import { withTickLabelTruncation } from './axis_tick_formatter'; | ||
| import * as textUtils from '../../../../common/text_utils'; | ||
| import { MockGlobalSpec } from '../../../../mocks/specs'; | ||
| import { LIGHT_THEME } from '../../../../utils/themes/light_theme'; | ||
|
|
||
| const { | ||
| axes: { tickLabel }, | ||
| } = LIGHT_THEME; | ||
|
|
||
| describe('withTickLabelTruncation', () => { | ||
| const measure = jest.fn((text: string) => ({ width: text.length, height: tickLabel.fontSize })); | ||
| const fitTextSpy = jest.spyOn(textUtils, 'fitText').mockReturnValue({ width: 0, text: 'tickLabel' }); | ||
| it('does not call fitText when maxLength is undefined', () => { | ||
| const axisSpec = MockGlobalSpec.yAxis({ tickLabelMaxLength: undefined, tickLabelTruncate: 'end' }); | ||
|
|
||
| withTickLabelTruncation(measure, tickLabel, axisSpec, 200)((v: number) => `${v}`); | ||
| expect(fitTextSpy).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("calls fitText with half the container width when maxLength is '50%'", () => { | ||
| const containerWidth = 200; | ||
| const axisSpec = MockGlobalSpec.yAxis({ tickLabelMaxLength: '50%', tickLabelTruncate: 'end' }); | ||
| const format = withTickLabelTruncation(measure, tickLabel, axisSpec, containerWidth)((v) => `${v}`); | ||
| format('tickLabel'); | ||
| expect(fitTextSpy).toHaveBeenCalledWith( | ||
| measure, | ||
| 'tickLabel', | ||
| containerWidth / 2, | ||
| tickLabel.fontSize, | ||
| expect.any(Object), | ||
| axisSpec.tickLabelTruncate ?? 'end', | ||
| ); | ||
| }); | ||
|
|
||
| it('calls fitText with the numeric maxLength as maximum width', () => { | ||
| const maxLengthPx = 72; | ||
| const axisSpec = MockGlobalSpec.yAxis({ tickLabelMaxLength: maxLengthPx, tickLabelTruncate: 'end' }); | ||
| const format = withTickLabelTruncation(measure, tickLabel, axisSpec, 400)((v) => `${v}`); | ||
| format('tickLabel'); | ||
|
|
||
| expect(fitTextSpy).toHaveBeenCalledWith( | ||
| measure, | ||
| 'tickLabel', | ||
| maxLengthPx, | ||
| tickLabel.fontSize, | ||
| expect.any(Object), | ||
| axisSpec.tickLabelTruncate ?? 'end', | ||
| ); | ||
| }); | ||
| }); |
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
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
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
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
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,56 @@ | ||
| /* | ||
| * 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 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 or the Server | ||
| * Side Public License, v 1. | ||
| */ | ||
|
|
||
| import type { Font } from './text_utils'; | ||
| import { fitText } from './text_utils'; | ||
| import type { TextMeasure } from '../utils/bbox/canvas_text_bbox_calculator'; | ||
|
|
||
| const monospaceMeasure: TextMeasure = (text) => ({ | ||
| width: text.length, | ||
| height: 12, | ||
| }); | ||
|
|
||
| const font: Font = { | ||
| fontStyle: 'normal', | ||
| fontVariant: 'normal', | ||
| fontWeight: 400, | ||
| fontFamily: 'sans-serif', | ||
| textColor: '#000', | ||
| }; | ||
|
|
||
| const fontSize = 12; | ||
|
|
||
| describe('fitText', () => { | ||
| it('returns the full string when it already fits (end)', () => { | ||
| expect(fitText(monospaceMeasure, 'abc', 10, fontSize, font, 'end')).toEqual({ | ||
| width: 3, | ||
| text: 'abc', | ||
| }); | ||
| }); | ||
|
|
||
| it('truncates at the end with an ellipsis when width is constrained', () => { | ||
| expect(fitText(monospaceMeasure, 'abcdef', 4, fontSize, font, 'end')).toEqual({ | ||
| width: 4, | ||
| text: 'abc…', | ||
| }); | ||
| }); | ||
|
|
||
| it('truncates at the start when position is start', () => { | ||
| expect(fitText(monospaceMeasure, 'abcdef', 4, fontSize, font, 'start')).toEqual({ | ||
| width: 4, | ||
| text: '…def', | ||
| }); | ||
| }); | ||
|
|
||
| it('truncates in the middle when position is middle', () => { | ||
| expect(fitText(monospaceMeasure, 'abcdef', 4, fontSize, font, 'middle')).toEqual({ | ||
| width: 4, | ||
| text: 'ab…f', | ||
| }); | ||
| }); | ||
| }); |
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.
Uh oh!
There was an error while loading. Please reload this page.