@@ -554,6 +554,7 @@ function appendBarText(gd, plotinfo, bar, cd, i, x0, x1, y0, y1, r, overhead, op
554554 // get trace attributes
555555 var trace = cd [ 0 ] . trace ;
556556 var isHorizontal = trace . orientation === 'h' ;
557+ var zeroBarDir = getZeroBarDir ( cd , isHorizontal , xa , ya ) ;
557558
558559 var text = getText ( fullLayout , cd , i , xa , ya ) ;
559560
@@ -707,7 +708,8 @@ function appendBarText(gd, plotinfo, bar, cd, i, x0, x1, y0, y1, r, overhead, op
707708 transform = toMoveOutsideBar ( x0 , x1 , y0 , y1 , textBB , {
708709 isHorizontal : isHorizontal ,
709710 constrained : constrained ,
710- angle : angle
711+ angle : angle ,
712+ zeroBarDir : zeroBarDir
711713 } ) ;
712714 } else {
713715 constrained = trace . constraintext === 'both' || trace . constraintext === 'inside' ;
@@ -957,7 +959,12 @@ function toMoveOutsideBar(x0, x1, y0, y1, textBB, opts) {
957959 var anchorX = 0 ;
958960 var anchorY = 0 ;
959961
960- var dir = isHorizontal ? dirSign ( x1 , x0 ) : dirSign ( y0 , y1 ) ;
962+ var dir ;
963+ if ( ( isHorizontal ? x0 === x1 : y0 === y1 ) && opts . zeroBarDir ) {
964+ dir = opts . zeroBarDir ;
965+ } else {
966+ dir = isHorizontal ? dirSign ( x1 , x0 ) : dirSign ( y0 , y1 ) ;
967+ }
961968 if ( isHorizontal ) {
962969 targetX = x1 - dir * textpad ;
963970 anchorX = dir * extrapad ;
@@ -999,6 +1006,38 @@ function getTextPosition(trace, index) {
9991006 return helpers . coerceEnumerated ( attributeTextPosition , value ) ;
10001007}
10011008
1009+ function getZeroBarDir ( cd , isHorizontal , xa , ya ) {
1010+ var hasPositive = false ;
1011+ var hasNegative = false ;
1012+
1013+ for ( var i = 0 ; i < cd . length ; i ++ ) {
1014+ var s = cd [ i ] . s ;
1015+
1016+ if ( s > 0 ) {
1017+ hasPositive = true ;
1018+ } else if ( s < 0 ) {
1019+ hasNegative = true ;
1020+ }
1021+
1022+ if ( hasPositive && hasNegative ) {
1023+ return 0 ;
1024+ }
1025+ }
1026+
1027+ var axis = isHorizontal ? xa : ya ;
1028+ var positiveDir = - dirSign ( axis . range [ 0 ] , axis . range [ 1 ] ) ;
1029+
1030+ if ( ! hasNegative ) {
1031+ return positiveDir ;
1032+ }
1033+
1034+ if ( ! hasPositive ) {
1035+ return - positiveDir ;
1036+ }
1037+
1038+ return 0 ;
1039+ }
1040+
10021041function calcTexttemplate ( fullLayout , cd , index , xa , ya ) {
10031042 var trace = cd [ 0 ] . trace ;
10041043 var texttemplate = Lib . castOption ( trace , index , 'texttemplate' ) ;
0 commit comments