@@ -144,40 +144,69 @@ class DataMarkerAnnotation extends GroupComponent<DataMarkerAnnotationCfg> imple
144
144
}
145
145
146
146
if ( textGroup ) {
147
+ let translateX = textGroup . attr ( 'x' ) , translateY = textGroup . attr ( 'y' ) ;
148
+ let textBaseline , textAlign ;
149
+ let xFactor = 0 , yFactor = 0 ;
147
150
if ( x + minX <= coordinateBBox . minX ) {
148
151
// 左侧超出
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 ) {
153
160
// 右侧超出
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
+ }
156
168
}
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 ;
172
178
}
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
+ }
179
197
}
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 ) ;
181
210
}
182
211
}
183
212
@@ -188,7 +217,27 @@ class DataMarkerAnnotation extends GroupComponent<DataMarkerAnnotationCfg> imple
188
217
const textStyle = get ( this . get ( 'text' ) , 'style' , { } ) ;
189
218
const direction = this . get ( 'direction' ) ;
190
219
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
+ }
192
241
return {
193
242
point : {
194
243
x : 0 ,
@@ -198,15 +247,16 @@ class DataMarkerAnnotation extends GroupComponent<DataMarkerAnnotationCfg> imple
198
247
line : {
199
248
path : [
200
249
[ 'M' , 0 , 0 ] ,
201
- [ 'L' , 0 , lineLength * factor ] ,
250
+ [ 'L' , 0 , lineLength * xFactor , lineLength * yFactor ] ,
202
251
] ,
203
252
...lineStyle ,
204
253
} ,
205
254
text : {
206
- x : 0 ,
207
- y : ( lineLength + 2 ) * factor ,
255
+ x : ( lineLength + 2 ) * xFactor ,
256
+ y : ( lineLength + 2 ) * yFactor ,
208
257
text : get ( this . get ( 'text' ) , 'content' , '' ) ,
209
- textBaseline : direction === 'upward' ? 'bottom' : 'top' ,
258
+ textBaseline,
259
+ textAlign,
210
260
...textStyle ,
211
261
} ,
212
262
} ;
0 commit comments