@@ -182,6 +182,9 @@ function drawColorBar(g, opts, gd) {
182
182
var optsX = opts . x ;
183
183
var optsY = isVertical ? opts . y : 1 - opts . y ;
184
184
185
+ var isPaperY = opts . yref === 'paper' ;
186
+ var isPaperX = opts . xref === 'paper' ;
187
+
185
188
var fullLayout = gd . _fullLayout ;
186
189
var gs = fullLayout . _size ;
187
190
@@ -216,11 +219,14 @@ function drawColorBar(g, opts, gd) {
216
219
var lenPx = Math . round ( len * ( lenmode === 'fraction' ? ( isVertical ? gs . h : gs . w ) : 1 ) ) ;
217
220
var lenFrac = lenPx / ( isVertical ? gs . h : gs . w ) ;
218
221
222
+ var posW = isPaperX ? gs . w : gd . _fullLayout . width ;
223
+ var posH = isPaperY ? gs . h : gd . _fullLayout . height ;
224
+
219
225
// x positioning: do it initially just for left anchor,
220
226
// then fix at the end (since we don't know the width yet)
221
227
var uPx = Math . round ( isVertical ?
222
- optsX * gs . w + xpad :
223
- optsY * gs . h + ypad
228
+ optsX * posW + xpad :
229
+ optsY * posH + ypad
224
230
) ;
225
231
226
232
var xRatio = { center : 0.5 , right : 1 } [ xanchor ] || 0 ;
@@ -237,8 +243,8 @@ function drawColorBar(g, opts, gd) {
237
243
optsX - xRatio * lenFrac ;
238
244
239
245
var vPx = Math . round ( isVertical ?
240
- gs . h * ( 1 - vFrac ) :
241
- gs . w * vFrac
246
+ posH * ( 1 - vFrac ) :
247
+ posW * vFrac
242
248
) ;
243
249
244
250
// stash a few things for makeEditable
@@ -351,18 +357,18 @@ function drawColorBar(g, opts, gd) {
351
357
var x , y ;
352
358
353
359
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 ;
356
362
}
357
363
358
364
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 ;
361
367
}
362
368
363
369
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 ;
366
372
}
367
373
368
374
drawTitle ( ax . _id + 'title' , {
@@ -382,14 +388,14 @@ function drawColorBar(g, opts, gd) {
382
388
383
389
if ( titleSide === 'right' ) {
384
390
y = mid ;
385
- x = gs . l + gs . w * pos + 10 + titleFontSize * (
391
+ x = gs . l + posW * pos + 10 + titleFontSize * (
386
392
ax . showticklabels ? 1 : 0.5
387
393
) ;
388
394
} else {
389
395
x = mid ;
390
396
391
397
if ( titleSide === 'bottom' ) {
392
- y = gs . t + gs . h * pos + 10 + (
398
+ y = gs . t + posH * pos + 10 + (
393
399
ticklabelposition . indexOf ( 'inside' ) === - 1 ?
394
400
ax . tickfont . size :
395
401
0
@@ -402,7 +408,7 @@ function drawColorBar(g, opts, gd) {
402
408
403
409
if ( titleSide === 'top' ) {
404
410
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 ;
406
412
}
407
413
}
408
414
@@ -668,9 +674,13 @@ function drawColorBar(g, opts, gd) {
668
674
669
675
var extraW = borderwidth + outlinewidth ;
670
676
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
+
671
681
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 )
674
684
. attr ( isVertical ? 'width' : 'height' , Math . max ( outerThickness - hColorbarMoveTitle , 2 ) )
675
685
. attr ( isVertical ? 'height' : 'width' , Math . max ( lenPx + extraW , 2 ) )
676
686
. call ( Color . fill , bgcolor )
@@ -693,9 +703,14 @@ function drawColorBar(g, opts, gd) {
693
703
'stroke-width' : outlinewidth
694
704
} ) ;
695
705
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
+
696
711
g . attr ( 'transform' , strTranslate (
697
- gs . l - ( isVertical ? xRatio * outerThickness : 0 ) ,
698
- gs . t - ( isVertical ? 0 : ( 1 - yRatio ) * outerThickness - moveY )
712
+ xShift ,
713
+ yShift
699
714
) ) ;
700
715
701
716
if ( ! isVertical && (
@@ -802,8 +817,30 @@ function drawColorBar(g, opts, gd) {
802
817
marginOpts . yb = optsY + thickness * bFrac ;
803
818
}
804
819
}
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
+ } ;
805
830
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
+ }
807
844
}
808
845
809
846
return Lib . syncOrAsync ( [
0 commit comments