Skip to content

Commit 7b393d4

Browse files
committed
cropped legend element using clip-path on legend <g>
1 parent cdd7240 commit 7b393d4

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

src/components/legend/constants.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,7 @@ module.exports = {
1212
scrollBarWidth: 4,
1313
scrollBarHeight: 20,
1414
scrollBarColor: '#808BA4',
15-
scrollBarMargin: 4
15+
scrollBarMargin: 4,
16+
17+
clipId: 'legend'
1618
};

src/components/legend/draw.js

+35-1
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,26 @@ module.exports = function draw(gd) {
197197

198198
legend.attr('transform', 'translate(' + lx + ',' + ly + ')');
199199

200+
var clipPath = selectClipPath(gd);
201+
202+
clipPath.attr({
203+
width: opts.width,
204+
height: scrollheight,
205+
x: 0,
206+
y: 0
207+
});
208+
209+
legend.call(Drawing.setClipUrl, constants.clipId);
210+
200211
// If scrollbar should be shown.
201212
if(gd.firstRender && opts.height - scrollheight > 0 && !gd._context.staticPlot) {
213+
bg.attr({
214+
width: opts.width - 2 * opts.borderwidth + constants.scrollBarWidth
215+
});
202216

203-
bg.attr({ width: opts.width - 2 * opts.borderwidth + constants.scrollBarWidth });
217+
clipPath.attr({
218+
width: opts.width + constants.scrollBarWidth
219+
});
204220

205221
legend.node().addEventListener('wheel', function(e) {
206222
e.preventDefault();
@@ -450,3 +466,21 @@ function repositionLegend(gd, traces) {
450466
t: opts.height * ({bottom: 1, middle: 0.5}[yanchor] || 0)
451467
});
452468
}
469+
470+
function selectClipPath(gd) {
471+
var container = gd._fullLayout._infolayer.node().parentNode;
472+
473+
var defs = d3.select(container).selectAll('defs')
474+
.data([0]);
475+
476+
defs.enter().append('defs');
477+
478+
var clipPath = defs.selectAll('#' + constants.clipId)
479+
.data([0]);
480+
481+
var path = clipPath.enter().append('clipPath')
482+
.attr('id', constants.clipId)
483+
.append('rect');
484+
485+
return path;
486+
}

0 commit comments

Comments
 (0)