@@ -66,13 +66,8 @@ module.exports = function draw(gd) {
66
66
var scrollBox = Lib . ensureSingle ( legend , 'g' , 'scrollbox' ) ;
67
67
68
68
var scrollBar = Lib . ensureSingle ( legend , 'rect' , 'scrollbar' , function ( s ) {
69
- s . attr ( {
70
- rx : 20 ,
71
- ry : 3 ,
72
- width : 0 ,
73
- height : 0
74
- } )
75
- . call ( Color . fill , '#808BA4' ) ;
69
+ s . attr ( constants . scrollBarEnterAttrs )
70
+ . call ( Color . fill , constants . scrollBarColor ) ;
76
71
} ) ;
77
72
78
73
var groups = scrollBox . selectAll ( 'g.groups' ) . data ( legendData ) ;
@@ -378,7 +373,7 @@ function drawTexts(g, gd) {
378
373
. call ( Drawing . font , fullLayout . legend . font )
379
374
. text ( isEditable ? ensureLength ( name , maxNameLength ) : name ) ;
380
375
381
- svgTextUtils . positionText ( textEl , constants . textOffsetX , 0 ) ;
376
+ svgTextUtils . positionText ( textEl , constants . textGap , 0 ) ;
382
377
383
378
function textLayout ( s ) {
384
379
svgTextUtils . convertToTspans ( s , gd , function ( ) {
@@ -497,7 +492,7 @@ function computeTextDimensions(g, gd) {
497
492
// approximation to height offset to center the font
498
493
// to avoid getBoundingClientRect
499
494
var textY = lineHeight * ( 0.3 + ( 1 - textLines ) / 2 ) ;
500
- svgTextUtils . positionText ( text , constants . textOffsetX , textY ) ;
495
+ svgTextUtils . positionText ( text , constants . textGap , textY ) ;
501
496
}
502
497
503
498
legendItem . lineHeight = lineHeight ;
@@ -508,13 +503,15 @@ function computeTextDimensions(g, gd) {
508
503
function computeLegendDimensions ( gd , groups , traces ) {
509
504
var fullLayout = gd . _fullLayout ;
510
505
var opts = fullLayout . legend ;
511
- var borderwidth = opts . borderwidth ;
512
506
var isGrouped = helpers . isGrouped ( opts ) ;
513
507
514
- var extraWidth = 0 ;
515
-
516
- var traceGap = 5 ;
508
+ var bw = opts . borderwidth ;
509
+ var bw2 = 2 * bw ;
510
+ var textGap = constants . textGap ;
511
+ var itemGap = constants . itemGap ;
512
+ var endPad = 2 * ( bw + itemGap ) ;
517
513
514
+ var extraWidth = 0 ;
518
515
opts . _width = 0 ;
519
516
opts . _height = 0 ;
520
517
@@ -526,63 +523,48 @@ function computeLegendDimensions(gd, groups, traces) {
526
523
}
527
524
528
525
traces . each ( function ( d ) {
529
- var legendItem = d [ 0 ] ;
530
- var textHeight = legendItem . height ;
531
- var textWidth = legendItem . width ;
532
-
533
- Drawing . setTranslate ( this ,
534
- borderwidth ,
535
- ( 5 + borderwidth + opts . _height + textHeight / 2 ) ) ;
536
-
537
- opts . _height += textHeight ;
538
- opts . _width = Math . max ( opts . _width , textWidth ) ;
526
+ var h = d [ 0 ] . height ;
527
+ Drawing . setTranslate ( this , bw , itemGap + bw + opts . _height + h / 2 ) ;
528
+ opts . _height += h ;
529
+ opts . _width = Math . max ( opts . _width , d [ 0 ] . width ) ;
539
530
} ) ;
540
531
541
- opts . _width += 45 + borderwidth * 2 ;
542
- opts . _height += 10 + borderwidth * 2 ;
532
+ opts . _width += itemGap + textGap + bw2 ;
533
+ opts . _height += endPad ;
543
534
544
535
if ( isGrouped ) {
545
536
opts . _height += ( opts . _lgroupsLength - 1 ) * opts . tracegroupgap ;
546
537
}
547
538
548
- extraWidth = 40 ;
539
+ extraWidth = textGap ;
549
540
} else if ( isGrouped ) {
550
541
var maxHeight = 0 ;
551
542
var maxWidth = 0 ;
552
- var groupData = groups . data ( ) ;
553
-
554
543
var maxItems = 0 ;
555
-
544
+ var groupData = groups . data ( ) ;
556
545
var i ;
546
+
557
547
for ( i = 0 ; i < groupData . length ; i ++ ) {
558
548
var group = groupData [ i ] ;
559
- var groupWidths = group . map ( function ( legendItemArray ) {
560
- return legendItemArray [ 0 ] . width ;
561
- } ) ;
562
-
563
- var groupWidth = Lib . aggNums ( Math . max , null , groupWidths ) ;
564
- var groupHeight = group . reduce ( function ( a , b ) {
565
- return a + b [ 0 ] . height ;
566
- } , 0 ) ;
567
-
568
- maxWidth = Math . max ( maxWidth , groupWidth ) ;
549
+ var groupWidths = group . map ( function ( d ) { return d [ 0 ] . width ; } ) ;
550
+ var groupHeight = group . reduce ( function ( a , b ) { return a + b [ 0 ] . height ; } , 0 ) ;
551
+ maxWidth = Math . max ( maxWidth , Lib . aggNums ( Math . max , null , groupWidths ) ) ;
569
552
maxHeight = Math . max ( maxHeight , groupHeight ) ;
570
553
maxItems = Math . max ( maxItems , group . length ) ;
571
554
}
572
555
573
- maxWidth += traceGap ;
574
- maxWidth += 40 ;
556
+ maxWidth += itemGap + textGap ;
575
557
576
558
var groupXOffsets = [ opts . _width ] ;
577
559
var groupYOffsets = [ ] ;
578
560
var rowNum = 0 ;
579
561
for ( i = 0 ; i < groupData . length ; i ++ ) {
580
- if ( fullLayout . _size . w < ( borderwidth + opts . _width + traceGap + maxWidth ) ) {
562
+ if ( fullLayout . _size . w < ( bw + opts . _width + itemGap + maxWidth ) ) {
581
563
groupXOffsets [ groupXOffsets . length - 1 ] = groupXOffsets [ 0 ] ;
582
564
opts . _width = maxWidth ;
583
565
rowNum ++ ;
584
566
} else {
585
- opts . _width += maxWidth + borderwidth ;
567
+ opts . _width += maxWidth + bw ;
586
568
}
587
569
588
570
var rowYOffset = ( rowNum * maxHeight ) ;
@@ -602,23 +584,17 @@ function computeLegendDimensions(gd, groups, traces) {
602
584
var groupHeight = 0 ;
603
585
604
586
groupTraces . each ( function ( d ) {
605
- var legendItem = d [ 0 ] ;
606
- var textHeight = legendItem . height ;
607
-
608
- Drawing . setTranslate ( this ,
609
- 0 ,
610
- ( 5 + borderwidth + groupHeight + textHeight / 2 ) ) ;
611
-
612
- groupHeight += textHeight ;
587
+ var h = d [ 0 ] . height ;
588
+ Drawing . setTranslate ( this , 0 , itemGap + bw + groupHeight + h / 2 ) ;
589
+ groupHeight += h ;
613
590
} ) ;
614
591
} ) ;
615
592
616
593
var maxYLegend = groupYOffsets [ groupYOffsets . length - 1 ] + maxHeight ;
617
- opts . _height = 10 + ( borderwidth * 2 ) + maxYLegend ;
594
+ opts . _height = maxYLegend + endPad ;
618
595
619
596
var maxOffset = Math . max . apply ( null , groupXOffsets ) ;
620
- opts . _width = maxOffset + maxWidth + 40 ;
621
- opts . _width += borderwidth * 2 ;
597
+ opts . _width = maxOffset + maxWidth + textGap + bw2 ;
622
598
} else {
623
599
var rowHeight = 0 ;
624
600
var maxTraceHeight = 0 ;
@@ -628,34 +604,31 @@ function computeLegendDimensions(gd, groups, traces) {
628
604
629
605
// calculate largest width for traces and use for width of all legend items
630
606
traces . each ( function ( d ) {
631
- maxTraceWidth = Math . max ( 40 + d [ 0 ] . width , maxTraceWidth ) ;
632
- fullTracesWidth += 40 + d [ 0 ] . width + traceGap ;
607
+ maxTraceWidth = Math . max ( maxTraceWidth , textGap + d [ 0 ] . width ) ;
608
+ fullTracesWidth += textGap + d [ 0 ] . width + itemGap ;
633
609
} ) ;
634
610
635
611
// check if legend fits in one row
636
- var oneRowLegend = fullLayout . _size . w > borderwidth + fullTracesWidth - traceGap ;
612
+ var oneRowLegend = fullLayout . _size . w > bw + fullTracesWidth - itemGap ;
637
613
638
614
traces . each ( function ( d ) {
639
- var legendItem = d [ 0 ] ;
640
- var traceWidth = oneRowLegend ? 40 + d [ 0 ] . width : maxTraceWidth ;
615
+ var h = d [ 0 ] . height ;
616
+ var traceWidth = oneRowLegend ? textGap + d [ 0 ] . width : maxTraceWidth ;
641
617
642
- if ( ( borderwidth + offsetX + traceGap + traceWidth ) > fullLayout . _size . w ) {
618
+ if ( ( bw + offsetX + itemGap + traceWidth ) > fullLayout . _size . w ) {
643
619
offsetX = 0 ;
644
620
rowHeight += maxTraceHeight ;
645
621
opts . _height += maxTraceHeight ;
646
622
// reset for next row
647
623
maxTraceHeight = 0 ;
648
624
}
649
625
650
- Drawing . setTranslate ( this ,
651
- ( borderwidth + offsetX ) ,
652
- ( 5 + borderwidth + legendItem . height / 2 ) + rowHeight ) ;
653
-
654
- opts . _width += traceGap + traceWidth ;
626
+ Drawing . setTranslate ( this , bw + offsetX , itemGap + bw + h / 2 + rowHeight ) ;
627
+ opts . _width += itemGap + traceWidth ;
655
628
656
629
// keep track of tallest trace in group
657
- offsetX += traceGap + traceWidth ;
658
- maxTraceHeight = Math . max ( legendItem . height , maxTraceHeight ) ;
630
+ offsetX += itemGap + traceWidth ;
631
+ maxTraceHeight = Math . max ( maxTraceHeight , h ) ;
659
632
} ) ;
660
633
661
634
if ( oneRowLegend ) {
@@ -664,8 +637,8 @@ function computeLegendDimensions(gd, groups, traces) {
664
637
opts . _height += maxTraceHeight ;
665
638
}
666
639
667
- opts . _width += borderwidth * 2 ;
668
- opts . _height += 10 + borderwidth * 2 ;
640
+ opts . _width += bw2 ;
641
+ opts . _height += endPad ;
669
642
}
670
643
671
644
// make sure we're only getting full pixels
@@ -678,14 +651,12 @@ function computeLegendDimensions(gd, groups, traces) {
678
651
) ;
679
652
680
653
traces . each ( function ( d ) {
681
- var legendItem = d [ 0 ] ;
682
- var bg = d3 . select ( this ) . select ( '.legendtoggle' ) ;
683
-
684
- Drawing . setRect ( bg ,
654
+ var h = d [ 0 ] . height ;
655
+ Drawing . setRect ( d3 . select ( this ) . select ( '.legendtoggle' ) ,
685
656
0 ,
686
- - legendItem . height / 2 ,
657
+ - h / 2 ,
687
658
( isEditable ? 0 : opts . _width ) + extraWidth ,
688
- legendItem . height
659
+ h
689
660
) ;
690
661
} ) ;
691
662
}
0 commit comments