diff --git a/packages/vrender-components/__tests__/unit/label-update-animation.test.ts b/packages/vrender-components/__tests__/unit/label-update-animation.test.ts index eb4c3d8ca..4b58edfc7 100644 --- a/packages/vrender-components/__tests__/unit/label-update-animation.test.ts +++ b/packages/vrender-components/__tests__/unit/label-update-animation.test.ts @@ -1,15 +1,16 @@ -import { application, createRect, createText } from '@visactor/vrender-core'; +import { application, createLine, createRect, createText } from '@visactor/vrender-core'; import { DefaultTimeline, ManualTicker, registerAnimate } from '@visactor/vrender-animate'; import { LabelBase } from '../../src/label/base'; import { RectLabel } from '../../src/label/rect'; +import type { LabelContent } from '../../src/label/type'; class TestLabel extends LabelBase { protected render(): void { return; } - runUpdate(prevLabel: any, currentLabel: any): void { - (this as any)._updateLabel(prevLabel, currentLabel); + runUpdate(prevLabel: LabelContent, currentLabel: LabelContent): void { + this._updateLabel(prevLabel, currentLabel); } } @@ -165,6 +166,73 @@ describe('Label update animation static truth', () => { expect((prevText as any).getFinalAttribute().opacity).toBeCloseTo(newAttrs.opacity, 5); }); + test.each([true, false])('reconciles label lines during updates with animation=%s', animation => { + const { stage, ticker } = createStageHarness('label-line-update'); + const label = new TestLabel({ + type: 'rect', + baseMarkGroupName: 'bar', + data: [], + animation, + animationEnter: false, + animationExit: false, + animationUpdate: { duration: 300, easing: 'linear' } + }); + label._prepareAnimate({}); + label.setStage(stage, null as any); + const firstLine = createLine({ + points: [ + { x: 0, y: 0 }, + { x: 20, y: 0 } + ] + }); + const text = createText({ x: 20, y: 0, text: 'Label' }); + const renderedLabel: LabelContent = { text, labelLine: firstLine }; + label.add(text); + label.add(firstLine); + + label.runUpdate(renderedLabel, { text: createText({ x: 40, y: 0, text: 'Label' }) }); + expect(renderedLabel.labelLine).toBeUndefined(); + expect(firstLine.parent).toBeNull(); + expect(text.parent).toBe(label); + if (animation) { + tick(ticker, 150); + expect(text.attribute.x).toBeGreaterThan(20); + expect(text.attribute.x).toBeLessThan(40); + tick(ticker, 150); + } + expect(text.attribute.x).toBe(40); + + const restoredLine = createLine({ + points: [ + { x: 0, y: 0 }, + { x: 60, y: 0 } + ] + }); + label.runUpdate(renderedLabel, { text: createText({ x: 60, y: 0, text: 'Label' }), labelLine: restoredLine }); + expect(renderedLabel.labelLine).toBe(restoredLine); + expect(restoredLine.parent).toBe(label); + if (animation) { + tick(ticker, 300); + } + + const nextLine = createLine({ + points: [ + { x: 0, y: 0 }, + { x: 80, y: 0 } + ] + }); + label.runUpdate(renderedLabel, { text: createText({ x: 80, y: 0, text: 'Label' }), labelLine: nextLine }); + expect(renderedLabel.labelLine).toBe(restoredLine); + expect(label.getChildren()).toEqual([text, restoredLine]); + if (animation) { + tick(ticker, 150); + expect(restoredLine.attribute.points[1].x).toBeGreaterThan(60); + expect(restoredLine.attribute.points[1].x).toBeLessThan(80); + tick(ticker, 150); + } + expect(restoredLine.attribute.points[1].x).toBe(80); + }); + test('lays out labels from related rect context final attrs before update handoff', () => { createStageHarness('label-related-rect-static-truth'); diff --git a/packages/vrender-components/src/animation/label-animate.ts b/packages/vrender-components/src/animation/label-animate.ts index 5a60fd27b..7359d3240 100644 --- a/packages/vrender-components/src/animation/label-animate.ts +++ b/packages/vrender-components/src/animation/label-animate.ts @@ -43,7 +43,7 @@ export class LabelUpdate extends AComponentAnimate { }); } - if (prevLabelLine) { + if (prevLabelLine && curLabelLine) { animator.animate(prevLabelLine, { type: 'to', to: curLabelLine.attribute, diff --git a/packages/vrender-components/src/label/base.ts b/packages/vrender-components/src/label/base.ts index 3e9defa9f..5a2c06bc9 100644 --- a/packages/vrender-components/src/label/base.ts +++ b/packages/vrender-components/src/label/base.ts @@ -1114,11 +1114,19 @@ export class LabelBase extends AnimateComponent { if (this._enableAnimation === false || this._animationConfig.update === false) { prevLabel.text.setAttributes(curText.attribute as any); if (prevLabelLine && curLabelLine) { - prevLabel.labelLine.setAttributes(curLabelLine.attribute); + prevLabelLine.setAttributes(curLabelLine.attribute); } } else { this._runUpdateAnimation(prevLabel, currentLabel); } + + if (prevLabelLine && !curLabelLine) { + this.removeChild(prevLabelLine); + prevLabel.labelLine = undefined; + } else if (!prevLabelLine && curLabelLine) { + this.add(curLabelLine); + prevLabel.labelLine = curLabelLine; + } } protected _removeLabel(textMap: Map) { diff --git a/packages/vrender/__tests__/visual/cases/index.mjs b/packages/vrender/__tests__/visual/cases/index.mjs index 1e9417f02..ef0e23791 100644 --- a/packages/vrender/__tests__/visual/cases/index.mjs +++ b/packages/vrender/__tests__/visual/cases/index.mjs @@ -1,4 +1,4 @@ -/** 冻结的首批用例清单;新增用例须显式登记。 */ +/** 视觉用例清单,新增用例须显式登记。 */ export const cases = [ { "id": "primitives-text", @@ -20,6 +20,11 @@ export const cases = [ "purpose": "标签组件文字、背景与更新", "file": "./tag-component.mjs" }, + { + "id": "label-line-update", + "purpose": "数据标签在柱条内外切换时移除并恢复引线", + "file": "./label-line-update.mjs" + }, { "id": "attribute-update", "purpose": "属性更新同步改变几何与拾取位置", diff --git a/packages/vrender/__tests__/visual/cases/label-line-update.mjs b/packages/vrender/__tests__/visual/cases/label-line-update.mjs new file mode 100644 index 000000000..b0dd1d847 --- /dev/null +++ b/packages/vrender/__tests__/visual/cases/label-line-update.mjs @@ -0,0 +1,61 @@ +import { assertionError } from '../assertions.mjs'; + +/** 创建带引线的柱条标签。标签位置变化时,引线随实际布局生成。 */ +export function createSpec(v, stage) { + const bar = v.createRect({ x: 60, y: 60, width: 160, height: 56, fill: '#a6c8f2' }); + const label = new v.RectLabel({ + type: 'rect', + data: [{ id: 'total', text: 'Total' }], + getBaseMarks: () => [bar], + position: 'right', + offset: 24, + line: { visible: true, style: { stroke: '#2d548c', lineWidth: 2 } }, + textStyle: { fontSize: 18, fontFamily: 'Arial', fill: '#222222' }, + smartInvert: false, + overlap: false, + animationEnter: false, + animationExit: false, + animationUpdate: { duration: 100, easing: 'linear', increaseEffect: false } + }); + stage.defaultLayer.add(bar); + stage.defaultLayer.add(label); + return { bar, label }; +} + +/** 通过公开更新接口将同一标签移入柱条,再移回外侧。 */ +export async function exercise(page) { + const removed = await page.evaluate(() => { + const objects = window.__visualObjects; + const { label } = objects; + const oldLine = label.getChildren().find(child => child.type === 'line'); + if (!oldLine) return false; + + objects.oldLine = oldLine; + objects.outsideX = label.getChildren().find(child => child.type === 'text').attribute.x; + label.setAttribute('position', 'inside-middle'); + return !oldLine.parent && label.getChildren().every(child => child.type !== 'line'); + }); + if (!removed) throw assertionError('标签移入柱条后引线未移除'); + await page.waitForFunction(() => { + const { bar, label } = window.__visualObjects; + return label.getChildren().find(child => child.type === 'text').attribute.x < bar.AABBBounds.x2; + }); + await page.evaluate(() => window.__visualObjects.label.setAttribute('position', 'right')); +} + +/** 更新动画结束后,确认同一标签外侧只绘制一条引线。 */ +export async function verify(page) { + await page.waitForFunction(() => { + const { label, outsideX } = window.__visualObjects; + const text = label.getChildren().find(child => child.type === 'text'); + return Math.abs(text.attribute.x - outsideX) < 0.01; + }); + const ok = await page.evaluate(() => { + const { label, oldLine } = window.__visualObjects; + const children = label.getChildren(); + const lines = children.filter(child => child.type === 'line'); + window.__visualStage.render(); + return children.length === 2 && lines.length === 1 && lines[0] !== oldLine && lines[0].attribute.points.length === 2; + }); + if (!ok) throw assertionError('外侧标签和引线的最终绘制不正确'); +}