-
Notifications
You must be signed in to change notification settings - Fork 19.7k
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
feat(label): support labels auto-rotation #19348
base: master
Are you sure you want to change the base?
feat(label): support labels auto-rotation #19348
Conversation
Thanks for your contribution! Document changes are required in this PR. Please also make a PR to apache/echarts-doc for document changes and update the issue id in the PR description. When the doc PR is merged, the maintainers will remove the |
@@ -599,11 +599,6 @@ function isTwoLabelOverlapped( | |||
return firstRect.intersect(nextRect); | |||
} | |||
|
|||
function isNameLocationCenter(nameLocation: string) { | |||
return nameLocation === 'middle' || nameLocation === 'center'; | |||
} |
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.
Moved to coord/axisHelper
so it can be reused elsewhere.
@@ -740,13 +735,10 @@ function buildAxisLabel( | |||
|
|||
const labelModel = axisModel.getModel('axisLabel'); | |||
const labelMargin = labelModel.get('margin'); | |||
const labels = axis.getViewLabels(); | |||
const { labels, rotation } = axis.getViewLabelsAndRotation(); |
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.
Labels + calculated rotation.
} | ||
|
||
getNameGap(): ReturnType<typeof getAxisNameGap> { | ||
return getAxisNameGap(this); |
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.
As correctly pointed out in #14996 (comment), the utility of the auto-rotate feature would be severely diminished unless we also gave users a way to automatically manage the location of the centered axis names. This is accomplished through the introduction of the nameLayout: 'auto'
option -- please see below.
nameGap?: number; | ||
nameLayout?: 'auto'; |
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.
An alternative was adding 'auto'
as one of the nameGap
's allowed values, but that would take away the ability to control the automatically managed distance between the name and the labels.
e35cb75
to
a75ed79
Compare
@@ -219,6 +220,8 @@ interface AxisLabelBaseOption extends Omit<TextCommonOption, 'color'> { | |||
// Whether axisLabel is inside the grid or outside the grid. | |||
inside?: boolean, | |||
rotate?: number, | |||
autoRotate?: boolean | [number, ...number[]], |
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.
An attempted alternative was allowing the auto
and number[]
options for axisLabel.rotate
, but that created too many rippling changes through the codebase. The implemented behavior is that specifying "hard-coded" label rotation option through rotate
disables autorotation.
@@ -219,6 +220,8 @@ interface AxisLabelBaseOption extends Omit<TextCommonOption, 'color'> { | |||
// Whether axisLabel is inside the grid or outside the grid. | |||
inside?: boolean, | |||
rotate?: number, | |||
autoRotate?: boolean | [number, ...number[]], | |||
minDistance?: number, |
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.
minDistance
is the minimal-allowed distance between the labels before they are rotated/collapsed.
@@ -232,7 +235,11 @@ interface AxisLabelBaseOption extends Omit<TextCommonOption, 'color'> { | |||
// Color can be callback | |||
color?: ColorString | ((value?: string | number, index?: number) => ColorString) | |||
overflow?: TextStyleProps['overflow'] | |||
// approximate auto-layout computations (autoRotate, hideOverlap) if the total number of axis labels is over | |||
// the trheshold; defaults to 40 | |||
layoutApproximationThreshold?: number |
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.
This was previously hard-coded in the code. Elevated it into an option so that users can choose when/whether they want to trigger this optimization, since it could lead to less-than-precise layout.
@@ -83,6 +83,7 @@ const defaultOption: AxisBaseOption = { | |||
// Whether axisLabel is inside the grid or outside the grid. | |||
inside: false, | |||
rotate: 0, | |||
minDistance: 10, |
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.
Default labels' minDistance
to 10px to match the current observable auto-interval behavior.
* @param axis | ||
* @return Be null/undefined if no labels. | ||
*/ | ||
export function estimateLabelUnionRect(axis: Axis) { |
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.
Moved to axisTickLabelBuilder.ts
(the only place this was called from) so we can reuse label size computations we do there.
@@ -399,3 +335,7 @@ export function unionAxisExtentFromData(dataExtent: number[], data: SeriesData, | |||
}); | |||
} | |||
} | |||
|
|||
export function isNameLocationCenter(nameLocation: string) { |
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.
Moved here from AxisBuilder.ts
for cleaner reuse.
a75ed79
to
eb35c0b
Compare
if (axesModel.get('nameLayout') === 'auto' | ||
&& isNameLocationCenter(axis.model.get('nameLocation')) | ||
) { | ||
const labelUnionRect = estimateLabelUnionRect(axis); |
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.
y: bboxOffset * cos | ||
}; | ||
|
||
return { axesIntersection, bounds, offset }; |
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.
See https://codepen.io/agurtovoy/pen/WNPyqWx for the visualization of the math here.
); | ||
// Magic number | ||
width = rect.width * 1.3; | ||
height = rect.height * 1.3; |
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.
The magic 1.3 coefficient here was effectively a workaround for a subtle bug that caused an incorrect axis pixel extent to be used when calculating the category axis' unit dimensions. Please see below for the bug fix.
adjustAxes(); | ||
// sort axes to make sure we estimate ordinal axes' label dimensions after we adjusted | ||
// the available grid space based on the value axes' label dimensions | ||
const axesList = [...this._axesList].sort((a, b) => axisOrder(a) - axisOrder(b)); |
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.
Part 1 of the "magic coefficient" bug fix referenced earlier...
|
||
// adjust axes after each potential change to grid dimensions | ||
// so that the next (ordinal) axis' label estimations are more precise | ||
adjustAxes(); |
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.
... part 2 of the "magic coefficient" bug fix.
test/axis-labelAutoRotate.html
Outdated
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.
Tests for the new functionality.
The changes brought by this PR can be previewed at: https://echarts.apache.org/examples/editor?version=PR-19348@fcd1590 |
627ad46
to
a2a6008
Compare
a2a6008
to
fcd1590
Compare
@Ovilia @plainheart What can I do to help you to review this? Would love to get this in soon. |
I'd love to begin using this feature. Please review. |
Thank you for your work @agurtovoy. |
Trying to migrate from highcharts where similar feature exists... |
@mermonkey I think it's still planned to be included in 6.0: https://github.com/apache/echarts/milestone/38. That said, I'm unsure of what specific steps need to be taken for that to happen. |
Brief Information
This pull request is in the type of:
What does this PR do?
Adds an option to auto-rotate category labels based on available space, specified category interval, and the list of provided rotations.
Fixed issues
Details
Before: What was the problem?
The axis label rotation option enhances usability of charts with a large number of categories and/or long category names by sparing the user the effort of hovering over specific value just to see its corresponding category; the usability enhancement is even greater for non-interactive, statically rendered charts. The rotation option currently has to be chosen beforehand, though, which is suboptimal when a chart is populated with dynamic data -- depending on the data, the labels might or might need to be rotated, and the optimal rotation angle might vary.
After: How does it behave after the fixing?
This PR introduces a new
axisLabel.autoRotate: boolean | number[]
option, which works in concert with theaxisLabel.interval: 'auto'
andgrid.containLabel: true
options to automatically manage (category) label rotation, interval, and layout:Kapture.2023-11-30.at.10.55.37.mp4
Kapture.2023-11-30.at.10.58.31.mp4
Kapture.2023-11-30.at.11.00.44.mp4
Document Info
I'll update the documentation once the API is settled.
Misc
Related test cases or examples to use the new APIs
See the new
axis-labelAutoRotate.html
test cases.Others
Merging options