@@ -462,9 +462,9 @@ legend.draw = function(td) {
462
462
. call ( Color . fill , opts . bgcolor )
463
463
. style ( 'stroke-width' , opts . borderwidth + 'px' ) ;
464
464
465
- var scrollBox = legendsvg . selectAll ( 'svg .scrollbox' )
465
+ var scrollBox = legendsvg . selectAll ( 'g .scrollbox' )
466
466
. data ( [ 0 ] ) ;
467
- scrollBox . enter ( ) . append ( 'svg ' )
467
+ scrollBox . enter ( ) . append ( 'g ' )
468
468
. attr ( 'class' , 'scrollbox' ) ;
469
469
scrollBox . exit ( ) . remove ( ) ;
470
470
@@ -559,24 +559,38 @@ legend.draw = function(td) {
559
559
// It requires the legend width, height, x and y to position the scrollbox
560
560
// and these values are mutated in repositionLegend.
561
561
var gs = fullLayout . _size ,
562
- lx = gs . l + gs . w * opts . x ,
563
- ly = gs . t + gs . h * ( 1 - opts . y ) ;
562
+ lx = gs . l + gs . w * opts . x ,
563
+ ly = gs . t + gs . h * ( 1 - opts . y ) ;
564
+
565
+ if ( opts . xanchor === 'right' || ( opts . xanchor === 'auto' && opts . x >= 2 / 3 ) ) {
566
+ lx -= opts . width ;
567
+ }
568
+ else if ( opts . xanchor === 'center' || ( opts . xanchor === 'auto' && opts . x > 1 / 3 ) ) {
569
+ lx -= opts . width / 2 ;
570
+ }
571
+
572
+ if ( opts . yanchor === 'bottom' || ( opts . yanchor === 'auto' && opts . y <= 1 / 3 ) ) {
573
+ ly -= opts . width ;
574
+ }
575
+ else if ( opts . yanchor === 'middle' || ( opts . yanchor === 'auto' && opts . y < 2 / 3 ) ) {
576
+ ly -= opts . height / 2 ;
577
+ }
564
578
565
579
// Deal with scrolling
566
580
var plotHeight = fullLayout . height - fullLayout . margin . b ,
567
- scrollPosition = scrollBox . attr ( 'viewBox' ) ? scrollBox . attr ( 'viewBox' ) . split ( ' ' ) [ 1 ] : 0 ,
568
- scrollheight = Math . min ( plotHeight - ly , opts . height ) ;
581
+ scrollheight = Math . min ( plotHeight - ly , opts . height ) ,
582
+ scrollPosition = scrollBox . attr ( 'data-scroll' ) ? scrollBox . attr ( 'data-scroll' ) : 0 ;
569
583
570
584
bg . style ( { width : opts . width , height : scrollheight } ) ;
571
- scrollBox . attr ( 'viewBox ' , '0 ' + scrollPosition + ' ' + opts . width + ' ' + scrollheight ) ;
585
+ scrollBox . attr ( 'transform ' , 'translate(0, ' + scrollPosition + ')' ) ;
572
586
573
587
legendsvg . call ( Drawing . setRect , lx , ly , opts . width , scrollheight ) ;
574
588
575
589
if ( td . firstRender && opts . height - scrollheight > 0 && ! td . _context . staticPlot ) {
576
590
577
591
legendsvg . node ( ) . addEventListener ( 'wheel' , function ( e ) {
578
592
e . preventDefault ( ) ;
579
- scrollHandler ( Math . round ( e . deltaY / 15 ) ) ;
593
+ scrollHandler ( e . deltaY / 20 ) ;
580
594
} ) ;
581
595
582
596
scrollBar . node ( ) . addEventListener ( 'mousedown' , function ( e ) {
@@ -609,15 +623,13 @@ legend.draw = function(td) {
609
623
610
624
function scrollHandler ( delta ) {
611
625
612
- // Scale movement to simulate native scroll performance
613
- var viewBox = scrollBox . attr ( 'viewBox' ) . split ( ' ' ) ,
614
- scrollBarTrack = scrollheight - constants . scrollBarHeight - 2 * constants . scrollBarMargin ,
615
- scrollBoxY = Lib . constrain ( + viewBox [ 1 ] + delta , 0 , Math . max ( opts . height - scrollheight , 0 ) ) ,
616
- scrollBarY = scrollBoxY / ( opts . height - scrollheight ) * scrollBarTrack + constants . scrollBarMargin ;
617
-
618
- viewBox [ 1 ] = scrollBoxY ;
626
+ var scrollBarTrack = scrollheight - constants . scrollBarHeight - 2 * constants . scrollBarMargin ,
627
+ translateY = scrollBox . attr ( 'data-scroll' ) ,
628
+ scrollBoxY = Lib . constrain ( translateY - delta , Math . min ( scrollheight - opts . height , 0 ) , 0 ) ,
629
+ scrollBarY = - scrollBoxY / ( opts . height - scrollheight ) * scrollBarTrack + constants . scrollBarMargin ;
619
630
620
- scrollBox . attr ( 'viewBox' , viewBox . join ( ' ' ) ) ;
631
+ scrollBox . attr ( 'data-scroll' , scrollBoxY ) ;
632
+ scrollBox . attr ( 'transform' , 'translate(0, ' + scrollBoxY + ')' ) ;
621
633
scrollBar . call (
622
634
Drawing . setRect ,
623
635
opts . width - ( constants . scrollBarWidth + constants . scrollBarMargin ) ,
@@ -737,8 +749,8 @@ legend.repositionLegend = function(td, traces){
737
749
// values <1/3 align the low side at that fraction, 1/3-2/3 align the
738
750
// center at that fraction, >2/3 align the right at that fraction
739
751
740
- var lx = gs . l + gs . w * opts . x ,
741
- ly = gs . t + gs . h * ( 1 - opts . y ) ;
752
+ var lx = gs . l + gs . w * opts . x ,
753
+ ly = gs . t + gs . h * ( 1 - opts . y ) ;
742
754
743
755
var xanchor = 'left' ;
744
756
if ( opts . xanchor === 'right' || ( opts . xanchor === 'auto' && opts . x >= 2 / 3 ) ) {
0 commit comments