@@ -30,7 +30,7 @@ var getLegendData = require('./get_legend_data');
30
30
var style = require ( './style' ) ;
31
31
var helpers = require ( './helpers' ) ;
32
32
33
- module . exports = function draw ( gd ) {
33
+ function draw ( gd ) {
34
34
var fullLayout = gd . _fullLayout ;
35
35
var clipId = 'legend' + fullLayout . _uid ;
36
36
@@ -39,13 +39,13 @@ module.exports = function draw(gd) {
39
39
if ( ! gd . _legendMouseDownTime ) gd . _legendMouseDownTime = 0 ;
40
40
41
41
var opts = fullLayout . legend ;
42
- var legendData = fullLayout . showlegend && getLegendData ( gd . calcdata , opts ) ;
42
+ var legendData = getLegendData ( gd ) ;
43
43
var hiddenSlices = fullLayout . hiddenlabels || [ ] ;
44
44
45
- if ( ! fullLayout . showlegend || ! legendData . length ) {
45
+ if ( ! legendData . length ) {
46
46
fullLayout . _infolayer . selectAll ( '.legend' ) . remove ( ) ;
47
47
fullLayout . _topdefs . select ( '#' + clipId ) . remove ( ) ;
48
- return Plots . autoMargin ( gd , 'legend' ) ;
48
+ return ;
49
49
}
50
50
51
51
var legend = Lib . ensureSingle ( fullLayout . _infolayer , 'g' , 'legend' , function ( s ) {
@@ -94,11 +94,7 @@ module.exports = function draw(gd) {
94
94
Plots . previousPromises ,
95
95
function ( ) { return computeLegendDimensions ( gd , groups , traces ) ; } ,
96
96
function ( ) {
97
- // IF expandMargin return a Promise (which is truthy),
98
- // we're under a doAutoMargin redraw, so we don't have to
99
- // draw the remaining pieces below
100
- if ( expandMargin ( gd ) ) return ;
101
-
97
+ var edits = gd . _context . edits ;
102
98
var gs = fullLayout . _size ;
103
99
var bw = opts . borderwidth ;
104
100
@@ -124,6 +120,15 @@ module.exports = function draw(gd) {
124
120
// legend, background and border, scroll box and scroll bar
125
121
Drawing . setTranslate ( legend , lx , ly ) ;
126
122
123
+ var isEditable = edits . legendText || edits . legendPosition ;
124
+ traces . each ( function ( d ) {
125
+ var h = d [ 0 ] . height ;
126
+ var w = isEditable ? constants . textGap :
127
+ ( opts . toggleRectWidth || ( constants . textGap + d [ 0 ] . width ) ) ;
128
+ if ( ! helpers . isVertical ( opts ) ) w += constants . itemGap / 2 ;
129
+ Drawing . setRect ( d3 . select ( this ) . select ( '.legendtoggle' ) , 0 , - h / 2 , w , h ) ;
130
+ } ) ;
131
+
127
132
// to be safe, remove previous listeners
128
133
scrollBar . on ( '.drag' , null ) ;
129
134
legend . on ( 'wheel' , null ) ;
@@ -232,7 +237,7 @@ module.exports = function draw(gd) {
232
237
clipPath . select ( 'rect' ) . attr ( 'y' , bw + scrollBoxY ) ;
233
238
}
234
239
235
- if ( gd . _context . edits . legendPosition ) {
240
+ if ( edits . legendPosition ) {
236
241
var xf , yf , x0 , y0 ;
237
242
238
243
legend . classed ( 'cursor-move' , true ) ;
@@ -274,7 +279,7 @@ module.exports = function draw(gd) {
274
279
} ) ;
275
280
}
276
281
} ] , gd ) ;
277
- } ;
282
+ }
278
283
279
284
function clickOrDoubleClick ( gd , legend , legendItem , numClicks , evt ) {
280
285
var trace = legendItem . data ( ) [ 0 ] [ 0 ] . trace ;
@@ -472,6 +477,8 @@ function computeTextDimensions(g, gd) {
472
477
*
473
478
* - _width: legend width
474
479
* - _maxWidth (for orientation:h only): maximum width before starting new row
480
+ *
481
+ * - toggleRectWidth: ...
475
482
*/
476
483
function computeLegendDimensions ( gd , groups , traces ) {
477
484
var fullLayout = gd . _fullLayout ;
@@ -497,9 +504,9 @@ function computeLegendDimensions(gd, groups, traces) {
497
504
30
498
505
) ;
499
506
500
- var toggleRectWidth = 0 ;
501
507
opts . _width = 0 ;
502
508
opts . _height = 0 ;
509
+ opts . _toggleRectWidth = 0 ;
503
510
504
511
if ( isVertical ) {
505
512
traces . each ( function ( d ) {
@@ -509,7 +516,7 @@ function computeLegendDimensions(gd, groups, traces) {
509
516
opts . _width = Math . max ( opts . _width , d [ 0 ] . width ) ;
510
517
} ) ;
511
518
512
- toggleRectWidth = textGap + opts . _width ;
519
+ opts . _toggleRectWidth = textGap + opts . _width ;
513
520
opts . _width += itemGap + textGap + bw2 ;
514
521
opts . _height += endPad ;
515
522
@@ -542,7 +549,7 @@ function computeLegendDimensions(gd, groups, traces) {
542
549
combinedItemWidth += w ;
543
550
} ) ;
544
551
545
- toggleRectWidth = null ;
552
+ opts . _toggleRectWidth = null ;
546
553
var maxRowWidth = 0 ;
547
554
548
555
if ( isGrouped ) {
@@ -618,30 +625,46 @@ function computeLegendDimensions(gd, groups, traces) {
618
625
opts . _height = Math . ceil ( opts . _height ) ;
619
626
620
627
opts . _effHeight = Math . min ( opts . _height , opts . _maxHeight ) ;
621
-
622
- var edits = gd . _context . edits ;
623
- var isEditable = edits . legendText || edits . legendPosition ;
624
- traces . each ( function ( d ) {
625
- var traceToggle = d3 . select ( this ) . select ( '.legendtoggle' ) ;
626
- var h = d [ 0 ] . height ;
627
- var w = isEditable ? textGap : ( toggleRectWidth || ( textGap + d [ 0 ] . width ) ) ;
628
- if ( ! isVertical ) w += itemGap / 2 ;
629
- Drawing . setRect ( traceToggle , 0 , - h / 2 , w , h ) ;
630
- } ) ;
631
628
}
632
629
633
- function expandMargin ( gd ) {
630
+ function pushMargin ( gd ) {
634
631
var fullLayout = gd . _fullLayout ;
635
632
var opts = fullLayout . legend ;
636
- var xanchor = Lib . getXanchor ( opts ) ;
637
- var yanchor = Lib . getYanchor ( opts ) ;
638
633
639
- return Plots . autoMargin ( gd , 'legend' , {
640
- x : opts . x ,
641
- y : opts . y ,
642
- l : opts . _width * ( FROM_TL [ xanchor ] ) ,
643
- r : opts . _width * ( FROM_BR [ xanchor ] ) ,
644
- b : opts . _effHeight * ( FROM_BR [ yanchor ] ) ,
645
- t : opts . _effHeight * ( FROM_TL [ yanchor ] )
646
- } ) ;
634
+ var legendData = getLegendData ( gd ) ;
635
+ if ( ! legendData . length ) return ;
636
+
637
+ var fakeLegend = Lib . ensureSingle ( Drawing . tester , 'g' , 'legend' ) ;
638
+
639
+ var groups = fakeLegend . selectAll ( 'g.groups' ) . data ( legendData ) ;
640
+ groups . enter ( ) . append ( 'g' ) . attr ( 'class' , 'groups' ) ;
641
+
642
+ var traces = groups . selectAll ( 'g.traces' ) . data ( Lib . identity ) ;
643
+ traces . enter ( ) . append ( 'g' ) . attr ( 'class' , 'traces' ) ;
644
+ traces . each ( function ( ) { d3 . select ( this ) . call ( drawTexts , gd ) ; } ) ;
645
+
646
+ return Lib . syncOrAsync ( [
647
+ Plots . previousPromises ,
648
+ function ( ) {
649
+ computeLegendDimensions ( gd , groups , traces ) ;
650
+ fakeLegend . remove ( ) ;
651
+
652
+ var xanchor = Lib . getXanchor ( opts ) ;
653
+ var yanchor = Lib . getYanchor ( opts ) ;
654
+
655
+ Plots . autoMargin ( gd , 'legend' , {
656
+ x : opts . x ,
657
+ y : opts . y ,
658
+ l : opts . _width * ( FROM_TL [ xanchor ] ) ,
659
+ r : opts . _width * ( FROM_BR [ xanchor ] ) ,
660
+ b : opts . _effHeight * ( FROM_BR [ yanchor ] ) ,
661
+ t : opts . _effHeight * ( FROM_TL [ yanchor ] )
662
+ } ) ;
663
+ }
664
+ ] , gd ) ;
647
665
}
666
+
667
+ module . exports = {
668
+ pushMargin : pushMargin ,
669
+ draw : draw
670
+ } ;
0 commit comments