@@ -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 } ;
0 commit comments