@@ -731,6 +731,7 @@ function calcTextAngle(shapex0, shapey0, shapex1, shapey1) {
731
731
732
732
function calcTextPosition ( shapex0 , shapey0 , shapex1 , shapey1 , shapeOptions , actualTextAngle , textBB ) {
733
733
var textPosition = shapeOptions . label . textposition ;
734
+ var textAngle = shapeOptions . label . textangle ;
734
735
var textPadding = shapeOptions . label . padding ;
735
736
var shapeType = shapeOptions . type ;
736
737
var textAngleRad = Math . PI / 180 * actualTextAngle ;
@@ -745,24 +746,27 @@ function calcTextPosition(shapex0, shapey0, shapex1, shapey1, shapeOptions, actu
745
746
if ( textPosition . indexOf ( 'start' ) !== - 1 ) {
746
747
textx = shapex0 ;
747
748
texty = shapey0 ;
748
- if ( xanchor === 'auto' ) xanchor = ( shapex1 >= shapex0 ) ? 'right' : 'left' ;
749
+ if ( xanchor === 'auto' ) {
750
+ if ( textAngle === 'auto' ) xanchor = ( shapex1 >= shapex0 ) ? 'left' : 'right' ;
751
+ else xanchor = ( shapex1 >= shapex0 ) ? 'right' : 'left' ;
752
+ }
749
753
} else if ( textPosition . indexOf ( 'end' ) !== - 1 ) {
750
754
textx = shapex1 ;
751
755
texty = shapey1 ;
752
- if ( xanchor === 'auto' ) xanchor = ( shapex1 >= shapex0 ) ? 'left' : 'right' ;
756
+ if ( xanchor === 'auto' ) {
757
+ if ( textAngle === 'auto' ) xanchor = ( shapex1 >= shapex0 ) ? 'right' : 'left' ;
758
+ else xanchor = ( shapex1 >= shapex0 ) ? 'left' : 'right' ;
759
+ }
753
760
} else { // Default: center
754
761
textx = ( shapex0 + shapex1 ) / 2 ;
755
762
texty = ( shapey0 + shapey1 ) / 2 ;
756
763
if ( xanchor === 'auto' ) xanchor = 'center' ;
757
764
}
758
765
759
- // Default yanchor is 'bottom' for lines (should handle in defaults.js)
760
- if ( yanchor === 'auto' ) yanchor = 'bottom' ;
761
-
762
766
// Special case for padding when angle is 'auto' for lines
763
767
// Padding should be treated as an orthogonal offset in this case
764
768
// Otherwise, padding is just a simple x and y offset
765
- if ( shapeOptions . label . textangle === 'auto' ) {
769
+ if ( textAngle === 'auto' ) {
766
770
// Set direction to apply padding (based on `yanchor` only)
767
771
var paddingDirection = { bottom : 1 , middle : 0 , top : - 1 } [ yanchor ] ;
768
772
paddingX = textPadding * Math . sin ( textAngleRad ) * paddingDirection ;
@@ -782,27 +786,30 @@ function calcTextPosition(shapex0, shapey0, shapex1, shapey1, shapeOptions, actu
782
786
// Horizontal needs a little extra padding to look balanced
783
787
paddingX = textPadding + 3 ;
784
788
if ( textPosition . indexOf ( 'right' ) !== - 1 ) {
785
- textx = Math . max ( shapex0 , shapex1 ) + paddingX ;
786
- if ( xanchor === 'auto' ) xanchor = 'left' ;
787
- } else if ( textPosition . indexOf ( 'left' ) !== - 1 ) {
788
- textx = Math . min ( shapex0 , shapex1 ) - paddingX ;
789
+ textx = Math . max ( shapex0 , shapex1 ) - paddingX ;
789
790
if ( xanchor === 'auto' ) xanchor = 'right' ;
791
+ } else if ( textPosition . indexOf ( 'left' ) !== - 1 ) {
792
+ textx = Math . min ( shapex0 , shapex1 ) + paddingX ;
793
+ if ( xanchor === 'auto' ) xanchor = 'left' ;
790
794
} else { // Default: center
791
795
textx = ( shapex0 + shapex1 ) / 2 ;
792
796
if ( xanchor === 'auto' ) xanchor = 'center' ;
793
797
}
794
798
795
799
// calc vertical position
796
- paddingY = textPadding ;
797
800
if ( textPosition . indexOf ( 'top' ) !== - 1 ) {
798
- texty = Math . min ( shapey0 , shapey1 ) - paddingY ;
799
- if ( yanchor === 'auto' ) yanchor = 'bottom' ;
801
+ texty = Math . min ( shapey0 , shapey1 ) ;
800
802
} else if ( textPosition . indexOf ( 'bottom' ) !== - 1 ) {
801
- texty = Math . max ( shapey0 , shapey1 ) + paddingY ;
802
- if ( yanchor === 'auto' ) yanchor = 'top' ;
803
- } else { // Default: middle
803
+ texty = Math . max ( shapey0 , shapey1 ) ;
804
+ } else {
804
805
texty = ( shapey0 + shapey1 ) / 2 ;
805
- if ( yanchor === 'auto' ) yanchor = 'middle' ;
806
+ }
807
+ // Apply padding
808
+ paddingY = textPadding ;
809
+ if ( yanchor === 'bottom' ) {
810
+ texty = texty - paddingY ;
811
+ } else if ( yanchor === 'top' ) {
812
+ texty = texty + paddingY ;
806
813
}
807
814
}
808
815
0 commit comments