@@ -48,13 +48,7 @@ function draw(gd) {
48
48
49
49
colorBars . each ( function ( opts ) {
50
50
var g = d3 . select ( this ) ;
51
-
52
- Lib . ensureSingle ( g , 'rect' , cn . cbbg ) ;
53
- Lib . ensureSingle ( g , 'g' , cn . cbfills ) ;
54
- Lib . ensureSingle ( g , 'g' , cn . cblines ) ;
55
- Lib . ensureSingle ( g , 'g' , cn . cbaxis , function ( s ) { s . classed ( cn . crisp , true ) ; } ) ;
56
- Lib . ensureSingle ( g , 'g' , cn . cbtitleunshift , function ( s ) { s . append ( 'g' ) . classed ( cn . cbtitle , true ) ; } ) ;
57
- Lib . ensureSingle ( g , 'rect' , cn . cboutline ) ;
51
+ makeFramework ( g ) ;
58
52
59
53
var done = drawColorBar ( g , opts , gd ) ;
60
54
if ( done && done . then ) ( gd . _promises || [ ] ) . push ( done ) ;
@@ -64,9 +58,7 @@ function draw(gd) {
64
58
}
65
59
} ) ;
66
60
67
- colorBars . exit ( )
68
- . each ( function ( opts ) { Plots . autoMargin ( gd , opts . _id ) ; } )
69
- . remove ( ) ;
61
+ colorBars . exit ( ) . remove ( ) ;
70
62
71
63
colorBars . order ( ) ;
72
64
}
@@ -173,6 +165,15 @@ function makeColorBarData(gd) {
173
165
return out ;
174
166
}
175
167
168
+ function makeFramework ( g ) {
169
+ Lib . ensureSingle ( g , 'rect' , cn . cbbg ) ;
170
+ Lib . ensureSingle ( g , 'g' , cn . cbfills ) ;
171
+ Lib . ensureSingle ( g , 'g' , cn . cblines ) ;
172
+ Lib . ensureSingle ( g , 'g' , cn . cbaxis , function ( s ) { s . classed ( cn . crisp , true ) ; } ) ;
173
+ Lib . ensureSingle ( g , 'g' , cn . cbtitleunshift , function ( s ) { s . append ( 'g' ) . classed ( cn . cbtitle , true ) ; } ) ;
174
+ Lib . ensureSingle ( g , 'rect' , cn . cboutline ) ;
175
+ }
176
+
176
177
function drawColorBar ( g , opts , gd ) {
177
178
var fullLayout = gd . _fullLayout ;
178
179
var gs = fullLayout . _size ;
@@ -420,7 +421,6 @@ function drawColorBar(g, opts, gd) {
420
421
// overlap, to prevent antialiasing gaps
421
422
z [ 1 ] = Lib . constrain ( z [ 1 ] + ( z [ 1 ] > z [ 0 ] ) ? 1 : - 1 , zBounds [ 0 ] , zBounds [ 1 ] ) ;
422
423
423
-
424
424
// Colorbar cannot currently support opacities so we
425
425
// use an opaque fill even when alpha channels present
426
426
var fillEl = d3 . select ( this ) . attr ( {
@@ -478,10 +478,6 @@ function drawColorBar(g, opts, gd) {
478
478
} ) ;
479
479
}
480
480
481
- // wait for the axis & title to finish rendering before
482
- // continuing positioning
483
- // TODO: why are we redrawing multiple times now with this?
484
- // I guess autoMargin doesn't like being post-promise?
485
481
function positionCB ( ) {
486
482
var innerWidth = thickPx + opts . outlinewidth / 2 + Drawing . bBox ( axLayer . node ( ) ) . width ;
487
483
titleEl = titleCont . select ( 'text' ) ;
@@ -501,8 +497,8 @@ function drawColorBar(g, opts, gd) {
501
497
innerWidth = Math . max ( innerWidth , titleWidth ) ;
502
498
}
503
499
504
- var outerwidth = 2 * opts . xpad + innerWidth + opts . borderwidth + opts . outlinewidth / 2 ;
505
- var outerheight = yBottomPx - yTopPx ;
500
+ var outerwidth = opts . _outerwidth = 2 * opts . xpad + innerWidth + opts . borderwidth + opts . outlinewidth / 2 ;
501
+ var outerheight = opts . _outerheight = yBottomPx - yTopPx ;
506
502
507
503
g . select ( '.' + cn . cbbg ) . attr ( {
508
504
x : xLeft - opts . xpad - ( opts . borderwidth + opts . outlinewidth ) / 2 ,
@@ -529,36 +525,6 @@ function drawColorBar(g, opts, gd) {
529
525
// fix positioning for xanchor!='left'
530
526
var xoffset = ( { center : 0.5 , right : 1 } [ opts . xanchor ] || 0 ) * outerwidth ;
531
527
g . attr ( 'transform' , 'translate(' + ( gs . l - xoffset ) + ',' + gs . t + ')' ) ;
532
-
533
- // auto margin adjustment
534
- var marginOpts = { } ;
535
- var tFrac = FROM_TL [ opts . yanchor ] ;
536
- var bFrac = FROM_BR [ opts . yanchor ] ;
537
- if ( opts . lenmode === 'pixels' ) {
538
- marginOpts . y = opts . y ;
539
- marginOpts . t = outerheight * tFrac ;
540
- marginOpts . b = outerheight * bFrac ;
541
- } else {
542
- marginOpts . t = marginOpts . b = 0 ;
543
- marginOpts . yt = opts . y + opts . len * tFrac ;
544
- marginOpts . yb = opts . y - opts . len * bFrac ;
545
- }
546
-
547
- var lFrac = FROM_TL [ opts . xanchor ] ;
548
- var rFrac = FROM_BR [ opts . xanchor ] ;
549
- if ( opts . thicknessmode === 'pixels' ) {
550
- marginOpts . x = opts . x ;
551
- marginOpts . l = outerwidth * lFrac ;
552
- marginOpts . r = outerwidth * rFrac ;
553
- } else {
554
- var extraThickness = outerwidth - thickPx ;
555
- marginOpts . l = extraThickness * lFrac ;
556
- marginOpts . r = extraThickness * rFrac ;
557
- marginOpts . xl = opts . x - opts . thickness * lFrac ;
558
- marginOpts . xr = opts . x + opts . thickness * rFrac ;
559
- }
560
-
561
- Plots . autoMargin ( gd , opts . _id , marginOpts ) ;
562
528
}
563
529
564
530
return Lib . syncOrAsync ( [
@@ -719,6 +685,71 @@ function mockColorBarAxis(gd, opts, zrange) {
719
685
return cbAxisOut ;
720
686
}
721
687
688
+ function pushMargin ( gd ) {
689
+ var fullLayout = gd . _fullLayout ;
690
+ var colorBarData = makeColorBarData ( gd ) ;
691
+ var gs = fullLayout . _size ;
692
+
693
+ var fakeColorBars = Drawing . tester . selectAll ( 'g.' + cn . colorbar )
694
+ . data ( colorBarData ) ;
695
+
696
+ fakeColorBars . enter ( ) . append ( 'g' )
697
+ . classed ( cn . colorbar , true ) ;
698
+
699
+ fakeColorBars . each ( function ( opts ) {
700
+ var g = d3 . select ( this ) ;
701
+ makeFramework ( g ) ;
702
+
703
+ var done = drawColorBar ( g , opts , gd ) ;
704
+ if ( done && done . then ) ( gd . _promises || [ ] ) . push ( done ) ;
705
+ } ) ;
706
+
707
+ return Lib . syncOrAsync ( [
708
+ Plots . previousPromises ,
709
+ function ( ) {
710
+ fakeColorBars . remove ( ) ;
711
+
712
+ for ( var i = 0 ; i < colorBarData . length ; i ++ ) {
713
+ var opts = colorBarData [ i ] ;
714
+ var outerwidth = opts . _outerwidth ;
715
+ var outerheight = opts . _outerheight ;
716
+ var tFrac = FROM_TL [ opts . yanchor ] ;
717
+ var bFrac = FROM_BR [ opts . yanchor ] ;
718
+
719
+ var marginOpts = { } ;
720
+
721
+ if ( opts . lenmode === 'pixels' ) {
722
+ marginOpts . y = opts . y ;
723
+ marginOpts . t = outerheight * tFrac ;
724
+ marginOpts . b = outerheight * bFrac ;
725
+ } else {
726
+ marginOpts . t = marginOpts . b = 0 ;
727
+ marginOpts . yt = opts . y + opts . len * tFrac ;
728
+ marginOpts . yb = opts . y - opts . len * bFrac ;
729
+ }
730
+
731
+ var lFrac = FROM_TL [ opts . xanchor ] ;
732
+ var rFrac = FROM_BR [ opts . xanchor ] ;
733
+ if ( opts . thicknessmode === 'pixels' ) {
734
+ marginOpts . x = opts . x ;
735
+ marginOpts . l = outerwidth * lFrac ;
736
+ marginOpts . r = outerwidth * rFrac ;
737
+ } else {
738
+ var thickPx = Math . round ( opts . thickness * gs . w ) ;
739
+ var extraThickness = outerwidth - thickPx ;
740
+ marginOpts . l = extraThickness * lFrac ;
741
+ marginOpts . r = extraThickness * rFrac ;
742
+ marginOpts . xl = opts . x - opts . thickness * lFrac ;
743
+ marginOpts . xr = opts . x + opts . thickness * rFrac ;
744
+ }
745
+
746
+ Plots . autoMargin ( gd , opts . _id , marginOpts ) ;
747
+ }
748
+ }
749
+ ] , gd ) ;
750
+ }
751
+
722
752
module . exports = {
723
- draw : draw
753
+ draw : draw ,
754
+ pushMargin : pushMargin
724
755
} ;
0 commit comments