Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: bitmap range overflow result in incorrect label layout #38

Merged
merged 1 commit into from
Jun 14, 2023
Merged
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
22 changes: 12 additions & 10 deletions packages/vrender-components/src/label/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ export abstract class LabelBase<T extends BaseLabelAttrs> extends AbstractCompon

if (avoidBaseMark) {
this._baseMarks?.forEach(mark => {
mark.AABBBounds && bitmap.setRange(boundToRange(bmpTool, mark.AABBBounds));
mark.AABBBounds && bitmap.setRange(boundToRange(bmpTool, mark.AABBBounds, true));
});
}

Expand All @@ -268,25 +268,25 @@ export abstract class LabelBase<T extends BaseLabelAttrs> extends AbstractCompon
if (canPlace(bmpTool, bitmap, text.AABBBounds, clampForce)) {
// 如果配置了限制在图形内部,需要提前判断;
if (!checkBounds) {
bitmap.setRange(boundToRange(bmpTool, text.AABBBounds));
bitmap.setRange(boundToRange(bmpTool, text.AABBBounds, true));
result.push({
...text.attribute,
_insideGraphic: canPlaceInside(text.AABBBounds, baseMark?.AABBBounds),
_computedBound: text.AABBBounds
_insideGraphic: canPlaceInside(text.AABBBounds, baseMark?.AABBBounds)
});
continue;
}

if (checkBounds && baseMark?.AABBBounds && canPlaceInside(text.AABBBounds, baseMark?.AABBBounds)) {
bitmap.setRange(boundToRange(bmpTool, text.AABBBounds));
bitmap.setRange(boundToRange(bmpTool, text.AABBBounds, true));
result.push({ ...text.attribute, _insideGraphic: true, _computedBound: text.AABBBounds });
continue;
}
}

let hasPlace: ReturnType<typeof place> = false;
// 发生碰撞,根据策略寻找可放置的位置
for (let j = 0; j < strategy.length; j++) {
const hasPlace = place(
hasPlace = place(
bmpTool,
bitmap,
strategy[j],
Expand All @@ -295,17 +295,19 @@ export abstract class LabelBase<T extends BaseLabelAttrs> extends AbstractCompon
this.getGraphicBounds(baseMark, labels[i]),
this.labeling
);
if (hasPlace) {
if (hasPlace !== false) {
result.push({
...text.attribute,
_insideGraphic: canPlaceInside(text.AABBBounds, baseMark?.AABBBounds),
_computedBound: text.AABBBounds
x: hasPlace.x,
y: hasPlace.y,
_insideGraphic: canPlaceInside(text.AABBBounds, baseMark?.AABBBounds)
});
break;
}
}

!hideOnHit &&
!hasPlace &&
!hideOnHit &&
result.push({
...text.attribute,
_insideGraphic: canPlaceInside(text.AABBBounds, baseMark?.AABBBounds),
Expand Down
20 changes: 10 additions & 10 deletions packages/vrender-components/src/label/overlap/place.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Text, createText } from '@visactor/vrender';
import { Text } from '@visactor/vrender';
import { IAABBBounds, IBoundsLike, isFunction } from '@visactor/vutils';
import { PointLocationCfg } from '../../core/type';
import type { LabelBase } from '../base';
Expand Down Expand Up @@ -67,18 +67,18 @@ export function placeToCandidates(
text: Text,
candidates: PointLocationCfg[] = [],
clampForce = true
) {
): PointLocationCfg | false {
for (let i = 0; i < candidates.length; i++) {
const tempText = createText(text.attribute) as Text;
const tempText = text.clone();
tempText.setAttributes(candidates[i]);
tempText.update();

if (canPlace($, bitmap, boundToRange($, tempText.AABBBounds), clampForce)) {
bitmap.setRange(boundToRange($, tempText.AABBBounds));
text.setAttributes(candidates[i]);
return true;
bitmap.setRange(boundToRange($, tempText.AABBBounds, true));
return candidates[i];
}
}
return false;
}

export function place<T extends BaseLabelAttrs>(
Expand All @@ -89,14 +89,14 @@ export function place<T extends BaseLabelAttrs>(
text: Text,
bounds: IBoundsLike,
labeling?: LabelBase<T>['labeling']
): boolean {
): PointLocationCfg | false {
if (s.type === 'bound' || s.type === 'position') {
if (isFunction(labeling)) {
// TODO:这里可以 filter 掉初始位置,提升一部分性能
const userPosition = isFunction(s.position) ? s.position(text.attribute) : s.position;
const positions = (userPosition || defaultLabelPosition(attrs.type)) as string[];
const candidates = positions.map(p => labeling(text.AABBBounds, bounds, p, attrs.offset) as PointLocationCfg);
return !!placeToCandidates($, bitmap, text, candidates, (attrs.overlap as OverlapAttrs)?.clampForce);
return placeToCandidates($, bitmap, text, candidates, (attrs.overlap as OverlapAttrs)?.clampForce);
}
return false;
}
Expand All @@ -106,15 +106,15 @@ export function place<T extends BaseLabelAttrs>(
const candidates = offset.map(dy => {
return { x: text.attribute.x as number, y: (text.attribute.y as number) + dy };
});
return !!placeToCandidates($, bitmap, text, candidates, (attrs.overlap as OverlapAttrs)?.clampForce);
return placeToCandidates($, bitmap, text, candidates, (attrs.overlap as OverlapAttrs)?.clampForce);
}

if (s.type === 'moveX') {
const offset = s.offset ? (isFunction(s.offset) ? s.offset(text.attribute) : s.offset) : [];
const candidates = offset.map(dx => {
return { x: (text.attribute.x as number) + dx, y: text.attribute.y as number };
});
return !!placeToCandidates($, bitmap, text, candidates, (attrs.overlap as OverlapAttrs)?.clampForce);
return placeToCandidates($, bitmap, text, candidates, (attrs.overlap as OverlapAttrs)?.clampForce);
}
return false;
}
Expand Down
18 changes: 16 additions & 2 deletions packages/vrender-components/src/label/overlap/scaler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IBoundsLike } from '@visactor/vutils';
import { IBoundsLike, clamp as clampRange } from '@visactor/vutils';
import { bitmap } from './bitmap';

/**
Expand Down Expand Up @@ -48,7 +48,21 @@ export function bitmapTool(width: number, height: number, padding = 0) {
return scale;
}

export function boundToRange($: BitmapTool, bound: IBoundsLike) {
export function boundToRange($: BitmapTool, bound: IBoundsLike, clamp: boolean = false) {
if (clamp) {
const { x1, x2, y1, y2 } = bound;
const _x1 = clampRange(x1, 0, $.width);
const _x2 = clampRange(x2, 0, $.width);
const _y1 = clampRange(y1, 0, $.height);
const _y2 = clampRange(y2, 0, $.height);
return {
x1: $(_x1),
x2: $(_x2),
y1: $(_y1),
y2: $(_y2)
};
}

return {
x1: $(bound.x1),
x2: $(bound.x2),
Expand Down