@@ -705,17 +705,19 @@ function drawColorBar(g, opts, gd) {
705705 )
706706 ) ) {
707707 // for horizontal colorbars when there is a border line or having different background color
708- // adjust x positioning for the first/last tick labels if they go outside the border
708+ // hide/adjust x positioning for the first/last tick labels if they go outside the border
709+ var tickLabels = axLayer . selectAll ( 'text' ) ;
710+ var numTicks = tickLabels [ 0 ] . length ;
711+
709712 var border = g . select ( '.' + cn . cbbg ) . node ( ) ;
710713 var oBb = Drawing . bBox ( border ) ;
711714 var oTr = Drawing . getTranslate ( g ) ;
712715
713716 var TEXTPAD = 2 ;
714717
715- var tickLabels = axLayer . selectAll ( 'text' ) ;
716718 tickLabels . each ( function ( d , i ) {
717719 var first = 0 ;
718- var last = tickLabels [ 0 ] . length - 1 ;
720+ var last = numTicks - 1 ;
719721 if ( i === first || i === last ) {
720722 var iBb = Drawing . bBox ( this ) ;
721723 var iTr = Drawing . getTranslate ( this ) ;
@@ -727,9 +729,7 @@ function drawColorBar(g, opts, gd) {
727729
728730 deltaX = oRight - iRight ;
729731 if ( deltaX > 0 ) deltaX = 0 ;
730- }
731-
732- if ( i === first ) {
732+ } else if ( i === first ) {
733733 var iLeft = iBb . left + iTr . x ;
734734 var oLeft = oBb . left + oTr . x + vPx + borderwidth + TEXTPAD ;
735735
@@ -738,10 +738,14 @@ function drawColorBar(g, opts, gd) {
738738 }
739739
740740 if ( deltaX ) {
741- this . setAttribute ( 'transform' ,
742- 'translate(' + deltaX + ',0) ' +
743- this . getAttribute ( 'transform' )
744- ) ;
741+ if ( numTicks < 3 ) { // adjust position
742+ this . setAttribute ( 'transform' ,
743+ 'translate(' + deltaX + ',0) ' +
744+ this . getAttribute ( 'transform' )
745+ ) ;
746+ } else { // hide
747+ this . setAttribute ( 'visibility' , 'hidden' ) ;
748+ }
745749 }
746750 }
747751 } ) ;
0 commit comments