Skip to content

Commit b773f31

Browse files
sersishen荊芥
and
荊芥
authored
Feat: add more datamarker annotation direction (#298)
* feat: 标注dataMarker类型增加更多的朝向 * test: 增加datamarker测试用例 --------- Co-authored-by: 荊芥 <[email protected]>
1 parent 799abf0 commit b773f31

File tree

3 files changed

+134
-34
lines changed

3 files changed

+134
-34
lines changed

src/annotation/data-marker.ts

Lines changed: 83 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -144,40 +144,69 @@ class DataMarkerAnnotation extends GroupComponent<DataMarkerAnnotationCfg> imple
144144
}
145145

146146
if (textGroup) {
147+
let translateX = textGroup.attr('x'), translateY = textGroup.attr('y');
148+
let textBaseline, textAlign;
149+
let xFactor = 0, yFactor = 0;
147150
if (x + minX <= coordinateBBox.minX) {
148151
// 左侧超出
149-
const overflow = coordinateBBox.minX - (x + minX);
150-
applyTranslate(textGroup, textGroup.attr('x') + overflow, textGroup.attr('y'));
151-
}
152-
if (x + maxX >= coordinateBBox.maxX) {
152+
if (direction === 'leftward') {
153+
textAlign = 'start';
154+
xFactor = 1;
155+
} else {
156+
const overflow = coordinateBBox.minX - (x + minX);
157+
translateX = textGroup.attr('x') + overflow;
158+
}
159+
} else if (x + maxX >= coordinateBBox.maxX) {
153160
// 右侧超出
154-
const overflow = x + maxX - coordinateBBox.maxX;
155-
applyTranslate(textGroup, textGroup.attr('x') - overflow, textGroup.attr('y'));
161+
if (direction === 'rightward') {
162+
textAlign = 'end';
163+
xFactor = -1;
164+
} else {
165+
const overflow = x + maxX - coordinateBBox.maxX;
166+
translateX = textGroup.attr('x') - overflow;
167+
}
156168
}
157-
}
158-
159-
if (
160-
(direction === 'upward' && y + minY <= coordinateBBox.minY) ||
161-
(direction !== 'upward' && y + maxY >= coordinateBBox.maxY)
162-
) {
163-
// 上方或者下方超出
164-
let textBaseline;
165-
let factor;
166-
if (direction === 'upward' && y + minY <= coordinateBBox.minY) {
167-
textBaseline = 'top';
168-
factor = 1;
169-
} else {
170-
textBaseline = 'bottom';
171-
factor = -1;
169+
if (!!xFactor && textAlign) {
170+
textShape.attr('textAlign', textAlign);
171+
if (lineShape) {
172+
lineShape.attr('path', [
173+
['M', 0, 0],
174+
['L', lineLength * xFactor, 0],
175+
]);
176+
}
177+
translateX = (lineLength + 2) * xFactor;
172178
}
173-
textShape.attr('textBaseline', textBaseline);
174-
if (lineShape) {
175-
lineShape.attr('path', [
176-
['M', 0, 0],
177-
['L', 0, lineLength * factor],
178-
]);
179+
if (y + minY <= coordinateBBox.minY) {
180+
// 上方超出
181+
if (direction === 'upward') {
182+
textBaseline = 'top';
183+
yFactor = 1;
184+
} else {
185+
const overflow = coordinateBBox.minY - (y + minY);
186+
translateY = textGroup.attr('y') + overflow;
187+
}
188+
} else if (y + maxY >= coordinateBBox.maxY) {
189+
// 下方超出
190+
if (direction === 'downward') {
191+
textBaseline = 'bottom';
192+
yFactor = -1;
193+
} else {
194+
const overflow = y + maxY - coordinateBBox.maxY;
195+
translateY = textGroup.attr('y') - overflow;
196+
}
179197
}
180-
applyTranslate(textGroup, textGroup.attr('x'), (lineLength + 2) * factor)
198+
if (!!yFactor && textBaseline) {
199+
textShape.attr('textBaseline', textBaseline);
200+
if (lineShape) {
201+
lineShape.attr('path', [
202+
['M', 0, 0],
203+
['L', 0, lineLength * yFactor],
204+
]);
205+
}
206+
translateY = (lineLength + 2) * yFactor;
207+
}
208+
if (translateX !== textGroup.attr('x') || translateY !== textGroup.attr('y'))
209+
applyTranslate(textGroup, translateX, translateY);
181210
}
182211
}
183212

@@ -188,7 +217,27 @@ class DataMarkerAnnotation extends GroupComponent<DataMarkerAnnotationCfg> imple
188217
const textStyle = get(this.get('text'), 'style', {});
189218
const direction = this.get('direction');
190219
const lineLength = lineDisplay ? get(this.get('line'), 'length', 0) : 0;
191-
const factor = direction === 'upward' ? -1 : 1;
220+
let xFactor = 0, yFactor = 0;
221+
let textBaseline = 'top',
222+
textAlign = 'start';
223+
switch (direction) {
224+
case 'upward':
225+
yFactor = -1;
226+
textBaseline = 'bottom';
227+
break;
228+
case 'downward':
229+
yFactor = 1;
230+
textBaseline = 'top';
231+
break;
232+
case 'leftward':
233+
xFactor = -1;
234+
textAlign = 'end';
235+
break;
236+
case 'rightward':
237+
xFactor = 1;
238+
textAlign = 'start';
239+
break;
240+
}
192241
return {
193242
point: {
194243
x: 0,
@@ -198,15 +247,16 @@ class DataMarkerAnnotation extends GroupComponent<DataMarkerAnnotationCfg> imple
198247
line: {
199248
path: [
200249
['M', 0, 0],
201-
['L', 0, lineLength * factor],
250+
['L', 0, lineLength * xFactor, lineLength * yFactor],
202251
],
203252
...lineStyle,
204253
},
205254
text: {
206-
x: 0,
207-
y: (lineLength + 2) * factor,
255+
x: (lineLength + 2) * xFactor,
256+
y: (lineLength + 2) * yFactor,
208257
text: get(this.get('text'), 'content', ''),
209-
textBaseline: direction === 'upward' ? 'bottom' : 'top',
258+
textBaseline,
259+
textAlign,
210260
...textStyle,
211261
},
212262
};

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1219,7 +1219,7 @@ export interface DataMarkerAnnotationCfg extends GroupComponentCfg {
12191219
/**
12201220
* 方向
12211221
*/
1222-
direction?: 'upward' | 'downward';
1222+
direction?: 'upward' | 'downward' | 'leftward' | 'rightward';
12231223
/**
12241224
* 是否自动调整
12251225
*/

tests/unit/annotation/data-marker-spec.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,56 @@ describe('annotation data-marker', () => {
8787
expect(textGroup.attr('y')).toBeGreaterThan(pointShape.attr('y'));
8888
});
8989

90+
it('render leftward', () => {
91+
dataMarker.update({
92+
direction: 'leftward',
93+
});
94+
95+
// group matrix
96+
expect(dataMarker.get('group').attr('matrix')).toEqual([1, 0, 0, 0, 1, 0, 100, 150, 1]);
97+
98+
// point rendered
99+
const pointShape: IShape = dataMarker.getElementByLocalId('point');
100+
expect(pointShape).toBeDefined();
101+
102+
// line rendered
103+
const lineShape: IShape = dataMarker.getElementByLocalId('line');
104+
expect(lineShape).toBeDefined();
105+
106+
// text rendered
107+
const textShape: IShape = dataMarker.getElementByLocalId('text');
108+
expect(textShape.attr('text')).toBe('test text');
109+
110+
const textGroup = dataMarker.getElementByLocalId('text-group');
111+
expect(textGroup.attr('x')).toBeLessThan(pointShape.attr('x'))
112+
expect(textGroup.attr('y')).toBe(pointShape.attr('y'));
113+
});
114+
115+
it('render rightward', () => {
116+
dataMarker.update({
117+
direction: 'rightward',
118+
});
119+
120+
// group matrix
121+
expect(dataMarker.get('group').attr('matrix')).toEqual([1, 0, 0, 0, 1, 0, 100, 150, 1]);
122+
123+
// point rendered
124+
const pointShape: IShape = dataMarker.getElementByLocalId('point');
125+
expect(pointShape).toBeDefined();
126+
127+
// line rendered
128+
const lineShape: IShape = dataMarker.getElementByLocalId('line');
129+
expect(lineShape).toBeDefined();
130+
131+
// text rendered
132+
const textShape: IShape = dataMarker.getElementByLocalId('text');
133+
expect(textShape.attr('text')).toBe('test text');
134+
135+
const textGroup = dataMarker.getElementByLocalId('text-group');
136+
expect(textGroup.attr('x')).toBeGreaterThan(pointShape.attr('x'))
137+
expect(textGroup.attr('y')).toBe(pointShape.attr('y'));
138+
});
139+
90140
it('render styled', () => {
91141
dataMarker.update({
92142
point: {

0 commit comments

Comments
 (0)