Skip to content

Commit 745a189

Browse files
authored
Merge pull request #2012 from VisActor/feat/polar-axis-autoLabelMaxWidth
feat: support autoLabelMaxWidth in circleAxis
2 parents ff41289 + b416efb commit 745a189

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

packages/vrender-components/src/axis/circle.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,13 +250,51 @@ export class CircleAxis extends AxisBase<CircleAxisAttributes> {
250250
}
251251
}
252252
}
253+
253254
protected afterLabelsOverlap(
254255
labelShapes: IText[],
255256
labelData: AxisItem[],
256257
labelContainer: IGroup,
257258
layer: number,
258259
layerCount: number
259260
): void {
261+
/**
262+
* 基于布局矩形与文本对齐方式设置标签最大行宽:
263+
* - left:可用宽度为到矩形右边界的水平距离(rectRight - x)
264+
* - right:可用宽度为到矩形左边界的水平距离(x - rectLeft)
265+
* - center:取左右两侧最小距离的两倍(2 * min(x - rectLeft, rectRight - x))
266+
*/
267+
const { layoutRect, autoLabelMaxWidth } = this.attribute as CircleAxisAttributes;
268+
if (!autoLabelMaxWidth || !layoutRect || !labelShapes || labelShapes.length === 0) {
269+
return;
270+
}
271+
272+
const rectLeft = 0;
273+
const rectRight = 0 + layoutRect.width;
274+
275+
for (let i = 0; i < labelShapes.length; i++) {
276+
const label = labelShapes[i];
277+
const x = label.attribute.x;
278+
const align = (label.attribute.textAlign as TextAlignType) ?? 'center';
279+
280+
let maxWidth = 0;
281+
if (align === 'left') {
282+
maxWidth = rectRight - x;
283+
} else if (align === 'right') {
284+
maxWidth = x - rectLeft;
285+
} else {
286+
const leftDist = x - rectLeft;
287+
const rightDist = rectRight - x;
288+
maxWidth = 2 * Math.max(0, Math.min(leftDist, rightDist));
289+
}
290+
291+
if (maxWidth > 0) {
292+
label.setAttributes({ maxLineWidth: maxWidth });
293+
} else {
294+
label.setAttributes({ maxLineWidth: 0 });
295+
}
296+
}
297+
260298
return;
261299
}
262300

packages/vrender-components/src/axis/type.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,15 @@ export interface CircleAxisAttributes extends AxisBaseAttributes {
259259
* @since 0.19.24
260260
*/
261261
sides?: number;
262+
263+
/**
264+
* 坐标轴可用布局区域的大小
265+
*/
266+
layoutRect?: { x: number; y: number; width: number; height: number };
267+
/**
268+
* 是否自动调整标签宽度以适应布局区域
269+
*/
270+
autoLabelMaxWidth?: boolean;
262271
}
263272

264273
// 坐标轴标题配置

0 commit comments

Comments
 (0)