Skip to content

Commit 7cfec79

Browse files
committed
Add scrollbox (not yet set to be active though)
1 parent b55b501 commit 7cfec79

File tree

1 file changed

+46
-13
lines changed

1 file changed

+46
-13
lines changed

src/components/legend/index.js

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -444,22 +444,28 @@ legend.draw = function(td) {
444444

445445
var legendsvg = fullLayout._infolayer.selectAll('svg.legend')
446446
.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');
449450

450-
var bgRect = legendsvg.selectAll('rect.bg')
451+
var bg = legendsvg.selectAll('rect.bg')
451452
.data([0]);
452-
bgRect.enter(0).append('rect')
453-
.attr('class','bg');
454-
bgRect
453+
bg.enter().append('rect')
454+
.attr('class','bg')
455455
.call(Color.stroke, opts.bordercolor)
456456
.call(Color.fill, opts.bgcolor)
457-
.style('stroke-width', opts.borderwidth+'px');
457+
.style('stroke-width', opts.borderwidth + 'px');
458458

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();
461464

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');
463469
groups.exit().remove();
464470

465471
if(isGrouped(opts)) {
@@ -530,6 +536,7 @@ legend.draw = function(td) {
530536
});
531537
});
532538

539+
// Position and size the legend
533540
legend.repositionLegend(td, traces);
534541

535542
if(td._context.editable) {
@@ -672,11 +679,37 @@ legend.repositionLegend = function(td, traces){
672679
lx = Math.round(lx);
673680
ly = Math.round(ly);
674681

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+
675709
fullLayout._infolayer.selectAll('svg.legend')
676-
.call(Drawing.setRect, lx, ly, legendwidth, legendheight);
710+
.call(Drawing.setRect, lx, ly, legendwidth, scrollheight);
677711
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 });
680713

681714
// lastly check if the margin auto-expand has changed
682715
Plots.autoMargin(td,'legend',{

0 commit comments

Comments
 (0)