Skip to content

Commit aa66bd4

Browse files
authored
Merge pull request #2106 from VisActor/agent/fix-line-axis-obb-offset
fix(axis): align rotated text OBB offsets
2 parents 18e6b59 + d696aa4 commit aa66bd4

2 files changed

Lines changed: 48 additions & 2 deletions

File tree

packages/vrender-components/__tests__/electron/axis/line.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,50 @@ describe('Line Axis', () => {
198198
expect(labelWidth).toBeLessThan(40);
199199
});
200200

201+
it('hides a flushed rotated endpoint label that overlaps its predecessor', () => {
202+
const axis = new LineAxis({
203+
orient: 'left',
204+
start: { x: 0, y: 0 },
205+
end: { x: 0, y: 400 },
206+
verticalFactor: 1,
207+
items: [
208+
[
209+
{ id: -200000000000, label: '-200000000000', rawValue: -200000000000, value: 1 },
210+
{ id: -100000000000, label: '-100000000000', rawValue: -100000000000, value: 0.8571428571428571 },
211+
{ id: 0, label: '0', rawValue: 0, value: 0.7142857142857143 },
212+
{ id: 100000000000, label: '100000000000', rawValue: 100000000000, value: 0.5714285714285714 },
213+
{ id: 200000000000, label: '200000000000', rawValue: 200000000000, value: 0.4285714285714286 },
214+
{ id: 300000000000, label: '300000000000', rawValue: 300000000000, value: 0.2857142857142857 },
215+
{ id: 400000000000, label: '400000000000', rawValue: 400000000000, value: 0.14285714285714285 },
216+
{ id: 500000000000, label: '500000000000', rawValue: 500000000000, value: 0 }
217+
]
218+
],
219+
label: {
220+
visible: true,
221+
autoLimit: false,
222+
autoHide: true,
223+
autoHideMethod: 'greedy',
224+
flush: true,
225+
style: {
226+
angle: Math.PI / 2,
227+
textAlign: 'center',
228+
textBaseline: 'top'
229+
}
230+
}
231+
});
232+
233+
(axis as any).render();
234+
235+
const labels = axis.getElementsByName(AXIS_ELEMENT_NAME.label) as unknown as IText[];
236+
const first = labels.find(label => label.attribute.text === '-200000000000');
237+
const second = labels.find(label => label.attribute.text === '-100000000000');
238+
239+
expect(first).toBeDefined();
240+
expect(second).toBeDefined();
241+
expect(first.attribute.visible).toBe(true);
242+
expect(second.attribute.visible).toBe(false);
243+
});
244+
201245
it('Line Axis with Title', () => {
202246
const scale = new LinearScale().domain([0, 100]).range([0, 1]).nice();
203247
const items = scale.ticks(10).map(tick => {

packages/vrender-core/src/graphic/text.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,15 @@ export class Text extends Graphic<ITextGraphicAttribute> implements IText {
172172
if (!this.obbText) {
173173
this.obbText = new Text({});
174174
}
175-
this.obbText.setAttributes({ ...attribute, angle: 0 });
175+
const { dx = 0, dy = 0 } = attribute;
176+
this.obbText.setAttributes({ ...attribute, angle: 0, dx: 0, dy: 0 });
176177
const bounds1 = this.obbText.AABBBounds;
177178
const { x, y } = attribute;
178179
const boundsCenter = { x: (bounds1.x1 + bounds1.x2) / 2, y: (bounds1.y1 + bounds1.y2) / 2 };
179180
const center = rotatePoint(boundsCenter, angle, { x, y });
180181
this._OBBBounds.copy(bounds1);
181-
this._OBBBounds.translate(center.x - boundsCenter.x, center.y - boundsCenter.y);
182+
// dx/dy are applied as a world-space translation after the text rotation.
183+
this._OBBBounds.translate(center.x - boundsCenter.x + dx, center.y - boundsCenter.y + dy);
182184
this._OBBBounds.angle = angle;
183185
return this._OBBBounds;
184186
}

0 commit comments

Comments
 (0)