Skip to content

Commit e1948aa

Browse files
authored
Merge pull request #21103 from apache/fix/label-layout-margin
Fix/label layout margin
2 parents e630036 + 8dbf829 commit e1948aa

36 files changed

+730
-389
lines changed

src/chart/pie/PieView.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import { setStatesStylesFromModel, toggleHoverEmphasis } from '../../util/states
2525
import ChartView from '../../view/Chart';
2626
import GlobalModel from '../../model/Global';
2727
import ExtensionAPI from '../../core/ExtensionAPI';
28-
import { Payload, ColorString, CircleLayoutOptionMixin, SeriesOption } from '../../util/types';
28+
import { Payload, ColorString } from '../../util/types';
2929
import SeriesData from '../../data/SeriesData';
3030
import PieSeriesModel, {PieDataItemOption} from './PieSeries';
3131
import labelLayout from './labelLayout';
@@ -197,7 +197,7 @@ class PiePiece extends graphic.Sector {
197197
sector.setTextConfig({
198198
// reset position, rotation
199199
position: null,
200-
rotation: null
200+
rotation: null,
201201
});
202202

203203
// Make sure update style on labelText after setLabelStyle.

src/chart/pie/labelLayout.ts

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ import ZRText from 'zrender/src/graphic/Text';
2727
import BoundingRect, {RectLike} from 'zrender/src/core/BoundingRect';
2828
import { each, isNumber } from 'zrender/src/core/util';
2929
import { limitTurnAngle, limitSurfaceAngle } from '../../label/labelGuideHelper';
30-
import { shiftLayoutOnY } from '../../label/labelLayoutHelper';
30+
import {
31+
computeLabelGeometry, LabelGeometry, shiftLayoutOnXY
32+
} from '../../label/labelLayoutHelper';
3133

3234
const RADIAN = Math.PI / 180;
3335

@@ -139,7 +141,7 @@ function adjustSingleSide(
139141
}
140142
}
141143

142-
if (shiftLayoutOnY(list, viewTop, viewTop + viewHeight)) {
144+
if (shiftLayoutOnXY(list, 1, viewTop, viewTop + viewHeight)) {
143145
recalculateX(list);
144146
}
145147
}
@@ -212,7 +214,7 @@ function avoidOverlap(
212214
}
213215
layout.targetTextWidth = targetTextWidth;
214216

215-
constrainTextWidth(layout, targetTextWidth);
217+
constrainTextWidth(layout, targetTextWidth, false);
216218
}
217219
}
218220

@@ -267,7 +269,7 @@ function avoidOverlap(
267269
function constrainTextWidth(
268270
layout: LabelLayout,
269271
availableWidth: number,
270-
forceRecalculate: boolean = false
272+
forceRecalculate: boolean
271273
) {
272274
if (layout.labelStyleWidth != null) {
273275
// User-defined style.width has the highest priority.
@@ -285,7 +287,7 @@ function constrainTextWidth(
285287
// textRect.width already contains paddingH if bgColor is set
286288
const oldOuterWidth = textRect.width + (bgColor ? 0 : paddingH);
287289
if (availableWidth < oldOuterWidth || forceRecalculate) {
288-
const oldHeight = textRect.height;
290+
289291
if (overflow && overflow.match('break')) {
290292
// Temporarily set background to be null to calculate
291293
// the bounding box without background.
@@ -325,14 +327,20 @@ function constrainTextWidth(
325327
label.setStyle('width', newWidth);
326328
}
327329

328-
const newRect = label.getBoundingRect();
329-
textRect.width = newRect.width;
330-
const margin = ((label.style.margin as number) || 0) + 2.1;
331-
textRect.height = newRect.height + margin;
332-
textRect.y -= (textRect.height - oldHeight) / 2;
330+
computeLabelGlobalRect(textRect, label);
333331
}
334332
}
335333

334+
function computeLabelGlobalRect(out: BoundingRect, label: ZRText): void {
335+
_tmpLabelGeometry.rect = out;
336+
computeLabelGeometry(_tmpLabelGeometry, label, _computeLabelGeometryOpt);
337+
}
338+
const _computeLabelGeometryOpt = {
339+
minMarginForce: [null, 0, null, 0],
340+
marginDefault: [1, 0, 1, 0], // Arbitrary value
341+
};
342+
const _tmpLabelGeometry: Partial<LabelGeometry> = {};
343+
336344
function isPositionCenter(sectorShape: LabelLayout) {
337345
// Not change x for center label
338346
return sectorShape.position === 'center';
@@ -502,12 +510,9 @@ export default function pieLabelLayout(
502510

503511
// Not sectorShape the inside label
504512
if (!isLabelInside) {
505-
const textRect = label.getBoundingRect().clone();
506-
textRect.applyTransform(label.getComputedTransform());
507-
// Text has a default 1px stroke. Exclude this.
508-
const margin = ((label.style.margin as number) || 0) + 2.1;
509-
textRect.y -= margin / 2;
510-
textRect.height += margin;
513+
514+
const textRect = new BoundingRect(0, 0, 0, 0);
515+
computeLabelGlobalRect(textRect, label);
511516

512517
labelLayoutList.push({
513518
label,

0 commit comments

Comments
 (0)