Skip to content

Fix/label layout margin #21103

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 8 commits into from
Jul 21, 2025
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
4 changes: 2 additions & 2 deletions src/chart/pie/PieView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { setStatesStylesFromModel, toggleHoverEmphasis } from '../../util/states
import ChartView from '../../view/Chart';
import GlobalModel from '../../model/Global';
import ExtensionAPI from '../../core/ExtensionAPI';
import { Payload, ColorString, CircleLayoutOptionMixin, SeriesOption } from '../../util/types';
import { Payload, ColorString } from '../../util/types';
import SeriesData from '../../data/SeriesData';
import PieSeriesModel, {PieDataItemOption} from './PieSeries';
import labelLayout from './labelLayout';
Expand Down Expand Up @@ -197,7 +197,7 @@ class PiePiece extends graphic.Sector {
sector.setTextConfig({
// reset position, rotation
position: null,
rotation: null
rotation: null,
});

// Make sure update style on labelText after setLabelStyle.
Expand Down
37 changes: 21 additions & 16 deletions src/chart/pie/labelLayout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ import ZRText from 'zrender/src/graphic/Text';
import BoundingRect, {RectLike} from 'zrender/src/core/BoundingRect';
import { each, isNumber } from 'zrender/src/core/util';
import { limitTurnAngle, limitSurfaceAngle } from '../../label/labelGuideHelper';
import { shiftLayoutOnY } from '../../label/labelLayoutHelper';
import {
computeLabelGeometry, LabelGeometry, shiftLayoutOnXY
} from '../../label/labelLayoutHelper';

const RADIAN = Math.PI / 180;

Expand Down Expand Up @@ -139,7 +141,7 @@ function adjustSingleSide(
}
}

if (shiftLayoutOnY(list, viewTop, viewTop + viewHeight)) {
if (shiftLayoutOnXY(list, 1, viewTop, viewTop + viewHeight)) {
recalculateX(list);
}
}
Expand Down Expand Up @@ -212,7 +214,7 @@ function avoidOverlap(
}
layout.targetTextWidth = targetTextWidth;

constrainTextWidth(layout, targetTextWidth);
constrainTextWidth(layout, targetTextWidth, false);
}
}

Expand Down Expand Up @@ -267,7 +269,7 @@ function avoidOverlap(
function constrainTextWidth(
layout: LabelLayout,
availableWidth: number,
forceRecalculate: boolean = false
forceRecalculate: boolean
) {
if (layout.labelStyleWidth != null) {
// User-defined style.width has the highest priority.
Expand All @@ -285,7 +287,7 @@ function constrainTextWidth(
// textRect.width already contains paddingH if bgColor is set
const oldOuterWidth = textRect.width + (bgColor ? 0 : paddingH);
if (availableWidth < oldOuterWidth || forceRecalculate) {
const oldHeight = textRect.height;

if (overflow && overflow.match('break')) {
// Temporarily set background to be null to calculate
// the bounding box without background.
Expand Down Expand Up @@ -325,14 +327,20 @@ function constrainTextWidth(
label.setStyle('width', newWidth);
}

const newRect = label.getBoundingRect();
textRect.width = newRect.width;
const margin = ((label.style.margin as number) || 0) + 2.1;
textRect.height = newRect.height + margin;
textRect.y -= (textRect.height - oldHeight) / 2;
computeLabelGlobalRect(textRect, label);
}
}

function computeLabelGlobalRect(out: BoundingRect, label: ZRText): void {
_tmpLabelGeometry.rect = out;
computeLabelGeometry(_tmpLabelGeometry, label, _computeLabelGeometryOpt);
}
const _computeLabelGeometryOpt = {
minMarginForce: [null, 0, null, 0],
marginDefault: [1, 0, 1, 0], // Arbitrary value
};
const _tmpLabelGeometry: Partial<LabelGeometry> = {};

function isPositionCenter(sectorShape: LabelLayout) {
// Not change x for center label
return sectorShape.position === 'center';
Expand Down Expand Up @@ -502,12 +510,9 @@ export default function pieLabelLayout(

// Not sectorShape the inside label
if (!isLabelInside) {
const textRect = label.getBoundingRect().clone();
textRect.applyTransform(label.getComputedTransform());
// Text has a default 1px stroke. Exclude this.
const margin = ((label.style.margin as number) || 0) + 2.1;
textRect.y -= margin / 2;
textRect.height += margin;

const textRect = new BoundingRect(0, 0, 0, 0);
computeLabelGlobalRect(textRect, label);

labelLayoutList.push({
label,
Expand Down
Loading