Skip to content

Commit

Permalink
Merge pull request #19176 from apache/fix-sunburst-label
Browse files Browse the repository at this point in the history
fix(sunburst): fix label rotation flipping
  • Loading branch information
plainheart committed Nov 1, 2023
2 parents 367610f + d9e67dd commit 818853e
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 42 deletions.
56 changes: 18 additions & 38 deletions src/chart/sunburst/SunburstPiece.ts
Expand Up @@ -215,14 +215,20 @@ class SunburstPiece extends graphic.Sector {
let r;
const labelPadding = getLabelAttr(labelStateModel, 'distance') || 0;
let textAlign = getLabelAttr(labelStateModel, 'align');
const rotateType = getLabelAttr(labelStateModel, 'rotate');
const flipStartAngle = Math.PI * 0.5;
const flipEndAngle = Math.PI * 1.5;
const midAngleNormal = normalizeRadian(rotateType === 'tangential' ? Math.PI / 2 - midAngle : midAngle);

// For text that is up-side down, rotate 180 degrees to make sure
// it's readable
const needsFlip = midAngleNormal > flipStartAngle
&& !isRadianAroundZero(midAngleNormal - flipStartAngle)
&& midAngleNormal < flipEndAngle;

if (labelPosition === 'outside') {
r = layout.r + labelPadding;
if (layout.clockwise) {
textAlign = midAngle > Math.PI / 2 ? 'right' : 'left';
}
else {
textAlign = midAngle > -Math.PI * 3 / 2 ? 'right' : 'left';
}
textAlign = needsFlip ? 'right' : 'left';
}
else {
if (!textAlign || textAlign === 'center') {
Expand All @@ -237,29 +243,11 @@ class SunburstPiece extends graphic.Sector {
}
else if (textAlign === 'left') {
r = layout.r0 + labelPadding;
if (layout.clockwise) {
if (midAngle > Math.PI / 2 && !isRadianAroundZero(midAngle - Math.PI / 2)) {
textAlign = 'right';
}
}
else {
if (midAngle > -Math.PI * 3 / 2 && !isRadianAroundZero(midAngle - Math.PI / 2)) {
textAlign = 'right';
}
}
textAlign = needsFlip ? 'right' : 'left';
}
else if (textAlign === 'right') {
r = layout.r - labelPadding;
if (layout.clockwise) {
if (midAngle > Math.PI / 2 && !isRadianAroundZero(midAngle - Math.PI / 2)) {
textAlign = 'left';
}
}
else {
if (midAngle > -Math.PI * 3 / 2 && !isRadianAroundZero(midAngle - Math.PI / 2)) {
textAlign = 'left';
}
}
textAlign = needsFlip ? 'left' : 'right';
}
}

Expand All @@ -269,22 +257,14 @@ class SunburstPiece extends graphic.Sector {
state.x = r * dx + layout.cx;
state.y = r * dy + layout.cy;

const rotateType = getLabelAttr(labelStateModel, 'rotate');
let rotate = 0;
if (rotateType === 'radial') {
rotate = normalizeRadian(-midAngle);
if (((rotate > Math.PI / 2 && rotate < Math.PI * 1.5))) {
rotate += Math.PI;
}
rotate = normalizeRadian(-midAngle)
+ (needsFlip ? Math.PI : 0);
}
else if (rotateType === 'tangential') {
rotate = Math.PI / 2 - midAngle;
if (rotate > Math.PI / 2) {
rotate -= Math.PI;
}
else if (rotate < -Math.PI / 2) {
rotate += Math.PI;
}
rotate = normalizeRadian(Math.PI / 2 - midAngle)
+ (needsFlip ? Math.PI : 0);
}
else if (zrUtil.isNumber(rotateType)) {
rotate = rotateType * Math.PI / 180;
Expand Down
40 changes: 37 additions & 3 deletions test/sunburst-label-align.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion test/sunburst-label.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 818853e

Please sign in to comment.