@@ -182,6 +182,9 @@ function drawColorBar(g, opts, gd) {
182182 var optsX = opts . x ;
183183 var optsY = isVertical ? opts . y : 1 - opts . y ;
184184
185+ var isPaperY = opts . yref === 'paper' ;
186+ var isPaperX = opts . xref === 'paper' ;
187+
185188 var fullLayout = gd . _fullLayout ;
186189 var gs = fullLayout . _size ;
187190
@@ -216,11 +219,14 @@ function drawColorBar(g, opts, gd) {
216219 var lenPx = Math . round ( len * ( lenmode === 'fraction' ? ( isVertical ? gs . h : gs . w ) : 1 ) ) ;
217220 var lenFrac = lenPx / ( isVertical ? gs . h : gs . w ) ;
218221
222+ var posW = isPaperX ? gs . w : gd . _fullLayout . width ;
223+ var posH = isPaperY ? gs . h : gd . _fullLayout . height ;
224+
219225 // x positioning: do it initially just for left anchor,
220226 // then fix at the end (since we don't know the width yet)
221227 var uPx = Math . round ( isVertical ?
222- optsX * gs . w + xpad :
223- optsY * gs . h + ypad
228+ optsX * posW + xpad :
229+ optsY * posH + ypad
224230 ) ;
225231
226232 var xRatio = { center : 0.5 , right : 1 } [ xanchor ] || 0 ;
@@ -237,8 +243,8 @@ function drawColorBar(g, opts, gd) {
237243 optsX - xRatio * lenFrac ;
238244
239245 var vPx = Math . round ( isVertical ?
240- gs . h * ( 1 - vFrac ) :
241- gs . w * vFrac
246+ posH * ( 1 - vFrac ) :
247+ posW * vFrac
242248 ) ;
243249
244250 // stash a few things for makeEditable
@@ -351,18 +357,18 @@ function drawColorBar(g, opts, gd) {
351357 var x , y ;
352358
353359 if ( titleSide === 'top' ) {
354- x = xpad + gs . l + gs . w * optsX ;
355- y = ypad + gs . t + gs . h * ( 1 - vFrac - lenFrac ) + 3 + titleFontSize * 0.75 ;
360+ x = xpad + gs . l + posW * optsX ;
361+ y = ypad + gs . t + posH * ( 1 - vFrac - lenFrac ) + 3 + titleFontSize * 0.75 ;
356362 }
357363
358364 if ( titleSide === 'bottom' ) {
359- x = xpad + gs . l + gs . w * optsX ;
360- y = ypad + gs . t + gs . h * ( 1 - vFrac ) - 3 - titleFontSize * 0.25 ;
365+ x = xpad + gs . l + posW * optsX ;
366+ y = ypad + gs . t + posH * ( 1 - vFrac ) - 3 - titleFontSize * 0.25 ;
361367 }
362368
363369 if ( titleSide === 'right' ) {
364- y = ypad + gs . t + gs . h * optsY + 3 + titleFontSize * 0.75 ;
365- x = xpad + gs . l + gs . w * vFrac ;
370+ y = ypad + gs . t + posH * optsY + 3 + titleFontSize * 0.75 ;
371+ x = xpad + gs . l + posW * vFrac ;
366372 }
367373
368374 drawTitle ( ax . _id + 'title' , {
@@ -382,14 +388,14 @@ function drawColorBar(g, opts, gd) {
382388
383389 if ( titleSide === 'right' ) {
384390 y = mid ;
385- x = gs . l + gs . w * pos + 10 + titleFontSize * (
391+ x = gs . l + posW * pos + 10 + titleFontSize * (
386392 ax . showticklabels ? 1 : 0.5
387393 ) ;
388394 } else {
389395 x = mid ;
390396
391397 if ( titleSide === 'bottom' ) {
392- y = gs . t + gs . h * pos + 10 + (
398+ y = gs . t + posH * pos + 10 + (
393399 ticklabelposition . indexOf ( 'inside' ) === - 1 ?
394400 ax . tickfont . size :
395401 0
@@ -402,7 +408,7 @@ function drawColorBar(g, opts, gd) {
402408
403409 if ( titleSide === 'top' ) {
404410 var nlines = title . text . split ( '<br>' ) . length ;
405- y = gs . t + gs . h * pos + 10 - thickPx - LINE_SPACING * titleFontSize * nlines ;
411+ y = gs . t + posH * pos + 10 - thickPx - LINE_SPACING * titleFontSize * nlines ;
406412 }
407413 }
408414
@@ -668,9 +674,13 @@ function drawColorBar(g, opts, gd) {
668674
669675 var extraW = borderwidth + outlinewidth ;
670676
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+
671681 g . select ( '.' + cn . cbbg )
672- . attr ( 'x' , ( isVertical ? uPx : vPx ) - extraW / 2 - ( isVertical ? xpad : 0 ) )
673- . attr ( 'y' , ( isVertical ? vPx : uPx ) - ( isVertical ? lenPx : ypad + moveY - hColorbarMoveTitle ) )
682+ . attr ( 'x' , lx )
683+ . attr ( 'y' , ly )
674684 . attr ( isVertical ? 'width' : 'height' , Math . max ( outerThickness - hColorbarMoveTitle , 2 ) )
675685 . attr ( isVertical ? 'height' : 'width' , Math . max ( lenPx + extraW , 2 ) )
676686 . call ( Color . fill , bgcolor )
@@ -693,9 +703,14 @@ function drawColorBar(g, opts, gd) {
693703 'stroke-width' : outlinewidth
694704 } ) ;
695705
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+
696711 g . attr ( 'transform' , strTranslate (
697- gs . l - ( isVertical ? xRatio * outerThickness : 0 ) ,
698- gs . t - ( isVertical ? 0 : ( 1 - yRatio ) * outerThickness - moveY )
712+ xShift ,
713+ yShift
699714 ) ) ;
700715
701716 if ( ! isVertical && (
@@ -802,8 +817,30 @@ function drawColorBar(g, opts, gd) {
802817 marginOpts . yb = optsY + thickness * bFrac ;
803818 }
804819 }
820+ var sideY = opts . y < 0.5 ? 'b' : 't' ;
821+ var sideX = opts . x < 0.5 ? 'l' : 'r' ;
822+
823+ 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+ } ;
805830
806- Plots . autoMargin ( gd , opts . _id , marginOpts ) ;
831+ if ( isPaperX && isPaperY ) {
832+ Plots . autoMargin ( gd , opts . _id , marginOpts ) ;
833+ } else if ( isPaperX ) {
834+ gd . _fullLayout . _reservedMargin [ opts . _id ] [ sideY ] = possibleReservedMargins [ sideY ] ;
835+ } else if ( isPaperY ) {
836+ gd . _fullLayout . _reservedMargin [ opts . _id ] [ sideX ] = possibleReservedMargins [ sideX ] ;
837+ } else {
838+ if ( isVertical ) {
839+ gd . _fullLayout . _reservedMargin [ opts . _id ] [ sideX ] = possibleReservedMargins [ sideX ] ;
840+ } else {
841+ gd . _fullLayout . _reservedMargin [ opts . _id ] [ sideY ] = possibleReservedMargins [ sideY ] ;
842+ }
843+ }
807844 }
808845
809846 return Lib . syncOrAsync ( [
0 commit comments