Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -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<any> {
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);
}
}

Expand Down Expand Up @@ -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');

Expand Down
2 changes: 1 addition & 1 deletion packages/vrender-components/src/animation/label-animate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class LabelUpdate extends AComponentAnimate<any> {
});
}

if (prevLabelLine) {
if (prevLabelLine && curLabelLine) {
animator.animate(prevLabelLine, {
type: 'to',
to: curLabelLine.attribute,
Expand Down
10 changes: 9 additions & 1 deletion packages/vrender-components/src/label/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1114,11 +1114,19 @@ export class LabelBase<T extends BaseLabelAttrs> extends AnimateComponent<T> {
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);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Keep a restored label line aligned with the animated text

When a label moves from inside-middle back to right with animationUpdate enabled (the new visual case does exactly this), _createLabelLine builds curLabelLine from the destination text bounds. _runUpdateAnimation animates the existing text, but LabelUpdate.onBind only animates a line when both prevLabelLine and curLabelLine exist. This branch therefore adds the new line immediately at its final points while the displayed text is still moving from inside the bar. The connector is visibly detached for the animation duration. Start or update the restored line in sync with the text, and assert an intermediate animation frame in the visual case.

prevLabel.labelLine = curLabelLine;
}
}

protected _removeLabel(textMap: Map<any, LabelContent>) {
Expand Down
7 changes: 6 additions & 1 deletion packages/vrender/__tests__/visual/cases/index.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** 冻结的首批用例清单;新增用例须显式登记。 */
/** 视觉用例清单,新增用例须显式登记。 */
export const cases = [
{
"id": "primitives-text",
Expand All @@ -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": "属性更新同步改变几何与拾取位置",
Expand Down
61 changes: 61 additions & 0 deletions packages/vrender/__tests__/visual/cases/label-line-update.mjs
Original file line number Diff line number Diff line change
@@ -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('外侧标签和引线的最终绘制不正确');
}
Loading