@@ -444,22 +444,28 @@ legend.draw = function(td) {
444
444
445
445
var legendsvg = fullLayout . _infolayer . selectAll ( 'svg.legend' )
446
446
. data ( [ 0 ] ) ;
447
- legendsvg . enter ( 0 ) . append ( 'svg' )
448
- . attr ( 'class' , 'legend' ) ;
447
+ legendsvg . enter ( ) . append ( 'svg' )
448
+ . attr ( 'class' , 'legend' )
449
+ . attr ( 'pointer-events' , 'all' ) ;
449
450
450
- var bgRect = legendsvg . selectAll ( 'rect.bg' )
451
+ var bg = legendsvg . selectAll ( 'rect.bg' )
451
452
. data ( [ 0 ] ) ;
452
- bgRect . enter ( 0 ) . append ( 'rect' )
453
- . attr ( 'class' , 'bg' ) ;
454
- bgRect
453
+ bg . enter ( ) . append ( 'rect' )
454
+ . attr ( 'class' , 'bg' )
455
455
. call ( Color . stroke , opts . bordercolor )
456
456
. call ( Color . fill , opts . bgcolor )
457
- . style ( 'stroke-width' , opts . borderwidth + 'px' ) ;
457
+ . style ( 'stroke-width' , opts . borderwidth + 'px' ) ;
458
458
459
- var groups = legendsvg . selectAll ( 'g.groups' )
460
- . data ( legendData ) ;
459
+ var scrollBox = legendsvg . selectAll ( 'svg.scrollbox' )
460
+ . data ( [ 0 ] ) ;
461
+ scrollBox . enter ( ) . append ( 'svg' )
462
+ . attr ( 'class' , 'scrollbox' ) ;
463
+ scrollBox . exit ( ) . remove ( ) ;
461
464
462
- groups . enter ( ) . append ( 'g' ) . attr ( 'class' , 'groups' ) ;
465
+ var groups = scrollBox . selectAll ( 'g.groups' )
466
+ . data ( legendData ) ;
467
+ groups . enter ( ) . append ( 'g' )
468
+ . attr ( 'class' , 'groups' ) ;
463
469
groups . exit ( ) . remove ( ) ;
464
470
465
471
if ( isGrouped ( opts ) ) {
@@ -530,6 +536,7 @@ legend.draw = function(td) {
530
536
} ) ;
531
537
} ) ;
532
538
539
+ // Position and size the legend
533
540
legend . repositionLegend ( td , traces ) ;
534
541
535
542
if ( td . _context . editable ) {
@@ -672,11 +679,37 @@ legend.repositionLegend = function(td, traces){
672
679
lx = Math . round ( lx ) ;
673
680
ly = Math . round ( ly ) ;
674
681
682
+ // Add scroll functionality
683
+ var legendsvg = fullLayout . _infolayer . selectAll ( 'svg.legend' ) ,
684
+ scrollBox = fullLayout . _infolayer . selectAll ( 'svg.legend .scrollbox' ) ,
685
+ scrollheight = Math . min ( 80 , legendheight ) ;
686
+
687
+ scrollBox . attr ( 'viewBox' , '0 0 ' + legendwidth + ' ' + scrollheight ) ;
688
+ legendsvg . node ( ) . addEventListener ( 'wheel' , scrollHandler ) ;
689
+
690
+ function scrollHandler ( e ) {
691
+ e . preventDefault ( ) ;
692
+
693
+ var scroll = scrollBox . attr ( 'viewBox' ) . split ( ' ' ) ;
694
+ scroll [ 1 ] = constrain ( 0 , Math . max ( legendheight - scrollheight , 0 ) , + scroll [ 1 ] + e . deltaY ) ;
695
+
696
+ scrollBox . attr ( 'viewBox' , scroll . join ( ' ' ) ) ;
697
+ }
698
+
699
+ function constrain ( min , max , c ) {
700
+ if ( c <= max && c >= min ) {
701
+ return c ;
702
+ } else if ( c > max ) {
703
+ return max ;
704
+ } else {
705
+ return min ;
706
+ }
707
+ }
708
+
675
709
fullLayout . _infolayer . selectAll ( 'svg.legend' )
676
- . call ( Drawing . setRect , lx , ly , legendwidth , legendheight ) ;
710
+ . call ( Drawing . setRect , lx , ly , legendwidth , scrollheight ) ;
677
711
fullLayout . _infolayer . selectAll ( 'svg.legend .bg' )
678
- . call ( Drawing . setRect , borderwidth / 2 , borderwidth / 2 ,
679
- legendwidth - borderwidth , legendheight - borderwidth ) ;
712
+ . style ( { width : legendwidth , height : scrollheight } ) ;
680
713
681
714
// lastly check if the margin auto-expand has changed
682
715
Plots . autoMargin ( td , 'legend' , {
0 commit comments