@@ -462,9 +462,9 @@ legend.draw = function(td) {
462462 . call ( Color . fill , opts . bgcolor )
463463 . style ( 'stroke-width' , opts . borderwidth + 'px' ) ;
464464
465- var scrollBox = legendsvg . selectAll ( 'svg .scrollbox' )
465+ var scrollBox = legendsvg . selectAll ( 'g .scrollbox' )
466466 . data ( [ 0 ] ) ;
467- scrollBox . enter ( ) . append ( 'svg ' )
467+ scrollBox . enter ( ) . append ( 'g ' )
468468 . attr ( 'class' , 'scrollbox' ) ;
469469 scrollBox . exit ( ) . remove ( ) ;
470470
@@ -559,24 +559,38 @@ legend.draw = function(td) {
559559 // It requires the legend width, height, x and y to position the scrollbox
560560 // and these values are mutated in repositionLegend.
561561 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+ }
564578
565579 // Deal with scrolling
566580 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 ;
569583
570584 bg . style ( { width : opts . width , height : scrollheight } ) ;
571- scrollBox . attr ( 'viewBox ' , '0 ' + scrollPosition + ' ' + opts . width + ' ' + scrollheight ) ;
585+ scrollBox . attr ( 'transform ' , 'translate(0, ' + scrollPosition + ')' ) ;
572586
573587 legendsvg . call ( Drawing . setRect , lx , ly , opts . width , scrollheight ) ;
574588
575589 if ( td . firstRender && opts . height - scrollheight > 0 && ! td . _context . staticPlot ) {
576590
577591 legendsvg . node ( ) . addEventListener ( 'wheel' , function ( e ) {
578592 e . preventDefault ( ) ;
579- scrollHandler ( Math . round ( e . deltaY / 15 ) ) ;
593+ scrollHandler ( e . deltaY / 20 ) ;
580594 } ) ;
581595
582596 scrollBar . node ( ) . addEventListener ( 'mousedown' , function ( e ) {
@@ -609,15 +623,13 @@ legend.draw = function(td) {
609623
610624 function scrollHandler ( delta ) {
611625
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 ;
619630
620- scrollBox . attr ( 'viewBox' , viewBox . join ( ' ' ) ) ;
631+ scrollBox . attr ( 'data-scroll' , scrollBoxY ) ;
632+ scrollBox . attr ( 'transform' , 'translate(0, ' + scrollBoxY + ')' ) ;
621633 scrollBar . call (
622634 Drawing . setRect ,
623635 opts . width - ( constants . scrollBarWidth + constants . scrollBarMargin ) ,
@@ -737,8 +749,8 @@ legend.repositionLegend = function(td, traces){
737749 // values <1/3 align the low side at that fraction, 1/3-2/3 align the
738750 // center at that fraction, >2/3 align the right at that fraction
739751
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 ) ;
742754
743755 var xanchor = 'left' ;
744756 if ( opts . xanchor === 'right' || ( opts . xanchor === 'auto' && opts . x >= 2 / 3 ) ) {
0 commit comments