perf: optimize getCartesianLabelBounds()

This commit is contained in:
xile611 2023-12-07 19:31:57 +08:00 committed by Weixing Zhang
parent 73e48a3ca7
commit 8612c33278

View File

@ -71,11 +71,16 @@ export const getCartesianLabelBounds = (scale: IBaseScale, domain: any[], op: IC
}
const isHorizontal = ['bottom', 'top'].includes(axisOrientType);
const isVertical = ['left', 'right'].includes(axisOrientType);
let orientAngle = startAngle;
let scaleX = 1;
let scaleY = 0;
if (isHorizontal) {
orientAngle = 0;
// nothing to update
} else if (isVertical) {
orientAngle = degreeToRadian(-90);
scaleX = 0;
scaleY = 1;
} else if (startAngle) {
scaleX = Math.cos(startAngle);
scaleY = -Math.sin(startAngle);
}
const textMeasure = initTextMeasure(labelStyle);
@ -89,10 +94,8 @@ export const getCartesianLabelBounds = (scale: IBaseScale, domain: any[], op: IC
// 估算文本位置
const pos = scale.scale(v);
const scaleX = Math.cos(orientAngle) * pos;
const scaleY = -Math.sin(orientAngle) * pos;
let textX = scaleX;
let textY = scaleY;
let textX = scaleX * pos;
let textY = scaleY * pos;
let align: TextAlignType;
if (labelFlush && isHorizontal && i === 0) {
@ -123,9 +126,12 @@ export const getCartesianLabelBounds = (scale: IBaseScale, domain: any[], op: IC
}
// 计算 label 包围盒
const bounds = new AABBBounds()
.set(textX, textY, textX + textWidth, textY + textHeight)
.rotate(labelAngle, scaleX, scaleY);
const bounds = new AABBBounds().set(textX, textY, textX + textWidth, textY + textHeight);
if (labelAngle) {
bounds.rotate(labelAngle, scaleX, scaleY);
}
return bounds;
});