@@ -219,11 +219,14 @@ function drawColorBar(g, opts, gd) {
219219 var lenPx = Math . round ( len * ( lenmode === 'fraction' ? ( isVertical ? gs . h : gs . w ) : 1 ) ) ;
220220 var lenFrac = lenPx / ( isVertical ? gs . h : gs . w ) ;
221221
222+ var posW = isPaperX ? gs . w : gd . _fullLayout . width ;
223+ var posH = isPaperY ? gs . h : gd . _fullLayout . height ;
224+
222225 // x positioning: do it initially just for left anchor,
223226 // then fix at the end (since we don't know the width yet)
224227 var uPx = Math . round ( isVertical ?
225- optsX * gs . w + xpad :
226- optsY * gs . h + ypad
228+ optsX * posW + xpad :
229+ optsY * posH + ypad
227230 ) ;
228231
229232 var xRatio = { center : 0.5 , right : 1 } [ xanchor ] || 0 ;
@@ -240,8 +243,8 @@ function drawColorBar(g, opts, gd) {
240243 optsX - xRatio * lenFrac ;
241244
242245 var vPx = Math . round ( isVertical ?
243- gs . h * ( 1 - vFrac ) :
244- gs . w * vFrac
246+ posH * ( 1 - vFrac ) :
247+ posW * vFrac
245248 ) ;
246249
247250 // stash a few things for makeEditable
@@ -385,14 +388,14 @@ function drawColorBar(g, opts, gd) {
385388
386389 if ( titleSide === 'right' ) {
387390 y = mid ;
388- x = gs . l + gs . w * pos + 10 + titleFontSize * (
391+ x = gs . l + posW * pos + 10 + titleFontSize * (
389392 ax . showticklabels ? 1 : 0.5
390393 ) ;
391394 } else {
392395 x = mid ;
393396
394397 if ( titleSide === 'bottom' ) {
395- y = gs . t + gs . h * pos + 10 + (
398+ y = gs . t + posH * pos + 10 + (
396399 ticklabelposition . indexOf ( 'inside' ) === - 1 ?
397400 ax . tickfont . size :
398401 0
@@ -405,7 +408,7 @@ function drawColorBar(g, opts, gd) {
405408
406409 if ( titleSide === 'top' ) {
407410 var nlines = title . text . split ( '<br>' ) . length ;
408- y = gs . t + gs . h * pos + 10 - thickPx - LINE_SPACING * titleFontSize * nlines ;
411+ y = gs . t + posH * pos + 10 - thickPx - LINE_SPACING * titleFontSize * nlines ;
409412 }
410413 }
411414
@@ -671,9 +674,13 @@ function drawColorBar(g, opts, gd) {
671674
672675 var extraW = borderwidth + outlinewidth ;
673676
677+ // TODO - are these the correct positions?
678+ var lx = ( isVertical ? uPx : vPx ) - extraW / 2 - ( isVertical ? xpad : 0 ) ;
679+ var ly = ( isVertical ? vPx : uPx ) - ( isVertical ? lenPx : ypad + moveY - hColorbarMoveTitle ) ;
680+
674681 g . select ( '.' + cn . cbbg )
675- . attr ( 'x' , ( isVertical ? uPx : vPx ) - extraW / 2 - ( isVertical ? xpad : 0 ) )
676- . attr ( 'y' , ( isVertical ? vPx : uPx ) - ( isVertical ? lenPx : ypad + moveY - hColorbarMoveTitle ) )
682+ . attr ( 'x' , lx )
683+ . attr ( 'y' , ly )
677684 . attr ( isVertical ? 'width' : 'height' , Math . max ( outerThickness - hColorbarMoveTitle , 2 ) )
678685 . attr ( isVertical ? 'height' : 'width' , Math . max ( lenPx + extraW , 2 ) )
679686 . call ( Color . fill , bgcolor )
@@ -696,9 +703,14 @@ function drawColorBar(g, opts, gd) {
696703 'stroke-width' : outlinewidth
697704 } ) ;
698705
706+ var xShift = ( ( isVertical ? xRatio * outerThickness : 0 ) ) ;
707+ var yShift = ( ( isVertical ? 0 : ( 1 - yRatio ) * outerThickness - moveY ) ) ;
708+ xShift = isPaperX ? gs . l - xShift : - xShift ;
709+ yShift = isPaperY ? gs . t - yShift : - yShift ;
710+
699711 g . attr ( 'transform' , strTranslate (
700- gs . l - ( isVertical ? xRatio * outerThickness : 0 ) ,
701- gs . t - ( isVertical ? 0 : ( 1 - yRatio ) * outerThickness - moveY )
712+ xShift ,
713+ yShift
702714 ) ) ;
703715
704716 if ( ! isVertical && (
@@ -809,18 +821,24 @@ function drawColorBar(g, opts, gd) {
809821 var sideX = opts . x < 0.5 ? 'l' : 'r' ;
810822
811823 gd . _fullLayout . _reservedMargin [ opts . _id ] = { } ;
824+ var possibleReservedMargins = {
825+ r : ( fullLayout . width - lx - xShift ) ,
826+ l : lx + marginOpts . r ,
827+ b : ( fullLayout . height - ly - yShift ) ,
828+ t : ly + marginOpts . b
829+ } ;
812830
813831 if ( isPaperX && isPaperY ) {
814832 Plots . autoMargin ( gd , opts . _id , marginOpts ) ;
815833 } else if ( isPaperX ) {
816- gd . _fullLayout . _reservedMargin [ opts . _id ] [ sideY ] = marginOpts [ sideY ] ;
834+ gd . _fullLayout . _reservedMargin [ opts . _id ] [ sideY ] = possibleReservedMargins [ sideY ] ;
817835 } else if ( isPaperY ) {
818- gd . _fullLayout . _reservedMargin [ opts . _id ] [ sideX ] = marginOpts [ sideX ] ;
836+ gd . _fullLayout . _reservedMargin [ opts . _id ] [ sideX ] = possibleReservedMargins [ sideX ] ;
819837 } else {
820838 if ( isVertical ) {
821- gd . _fullLayout . _reservedMargin [ opts . _id ] [ sideX ] = marginOpts [ sideX ] ;
839+ gd . _fullLayout . _reservedMargin [ opts . _id ] [ sideX ] = possibleReservedMargins [ sideX ] ;
822840 } else {
823- gd . _fullLayout . _reservedMargin [ opts . _id ] [ sideY ] = marginOpts [ sideY ] ;
841+ gd . _fullLayout . _reservedMargin [ opts . _id ] [ sideY ] = possibleReservedMargins [ sideY ] ;
824842 }
825843 }
826844 }
0 commit comments