Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 18 additions & 38 deletions src/chart/sunburst/SunburstPiece.ts
Original file line number Diff line number Diff line change
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.