Skip to content

Commit 91c7757

Browse files
committed
Fix invisible legends with negative y
Fixes #384
1 parent c68ed58 commit 91c7757

File tree

1 file changed

+39
-15
lines changed

1 file changed

+39
-15
lines changed

src/components/legend/draw.js

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -184,27 +184,51 @@ module.exports = function draw(gd) {
184184
if(anchorUtils.isRightAnchor(opts)) {
185185
lx -= opts.width;
186186
}
187-
if(anchorUtils.isCenterAnchor(opts)) {
187+
else if(anchorUtils.isCenterAnchor(opts)) {
188188
lx -= opts.width / 2;
189189
}
190190

191191
if(anchorUtils.isBottomAnchor(opts)) {
192192
ly -= opts.height;
193193
}
194-
if(anchorUtils.isMiddleAnchor(opts)) {
194+
else if(anchorUtils.isMiddleAnchor(opts)) {
195195
ly -= opts.height / 2;
196196
}
197197

198+
lx = Math.round(lx);
199+
ly = Math.round(ly);
200+
201+
// Make sure the legend top and bottom are visible
202+
// (legends with a scroll bar are not allowed to stretch beyond the extended
203+
// margins)
204+
var lyMin = 0;
205+
var lyMax = fullLayout.margin.t + fullLayout.height + fullLayout.margin.b;
206+
var legendHeight = opts.height;
207+
var legendHeightMax = gs.h;
208+
209+
210+
if(legendHeight > legendHeightMax) {
211+
ly = gs.t;
212+
legendHeight = legendHeightMax;
213+
}
214+
else {
215+
if(ly > lyMax) ly = lyMax - legendHeight;
216+
217+
if(ly < lyMin) ly = lyMin;
218+
219+
legendHeight = Math.min(lyMax - ly, opts.height);
220+
}
221+
198222
// Deal with scrolling
199-
var plotHeight = fullLayout.height - fullLayout.margin.b,
200-
scrollheight = Math.min(plotHeight - ly, opts.height),
201-
scrollPosition = scrollBox.attr('data-scroll') ? scrollBox.attr('data-scroll') : 0;
223+
var scrollPosition = scrollBox.attr('data-scroll') ?
224+
scrollBox.attr('data-scroll') :
225+
0;
202226

203227
scrollBox.attr('transform', 'translate(0, ' + scrollPosition + ')');
204228

205229
bg.attr({
206230
width: opts.width - 2 * opts.borderwidth,
207-
height: scrollheight - 2 * opts.borderwidth,
231+
height: legendHeight - 2 * opts.borderwidth,
208232
x: opts.borderwidth,
209233
y: opts.borderwidth
210234
});
@@ -213,15 +237,15 @@ module.exports = function draw(gd) {
213237

214238
clipPath.select('rect').attr({
215239
width: opts.width,
216-
height: scrollheight,
240+
height: legendHeight,
217241
x: 0,
218242
y: 0
219243
});
220244

221245
legend.call(Drawing.setClipUrl, clipId);
222246

223247
// If scrollbar should be shown.
224-
if(opts.height - scrollheight > 0 && !gd._context.staticPlot) {
248+
if(opts.height - legendHeight > 0 && !gd._context.staticPlot) {
225249

226250
bg.attr({
227251
width: opts.width - 2 * opts.borderwidth + constants.scrollBarWidth
@@ -243,21 +267,21 @@ module.exports = function draw(gd) {
243267
scrollBox.attr('data-scroll',0);
244268
}
245269

246-
scrollHandler(0,scrollheight);
270+
scrollHandler(0,legendHeight);
247271

248272
legend.on('wheel',null);
249273

250274
legend.on('wheel', function() {
251275
var e = d3.event;
252276
e.preventDefault();
253-
scrollHandler(e.deltaY / 20, scrollheight);
277+
scrollHandler(e.deltaY / 20, legendHeight);
254278
});
255279

256280
scrollBar.on('.drag',null);
257281
scrollBox.on('.drag',null);
258282
var drag = d3.behavior.drag()
259283
.on('drag', function() {
260-
scrollHandler(d3.event.dy, scrollheight);
284+
scrollHandler(d3.event.dy, legendHeight);
261285
});
262286

263287
scrollBar.call(drag);
@@ -266,12 +290,12 @@ module.exports = function draw(gd) {
266290
}
267291

268292

269-
function scrollHandler(delta, scrollheight) {
293+
function scrollHandler(delta, legendHeight) {
270294

271-
var scrollBarTrack = scrollheight - constants.scrollBarHeight - 2 * constants.scrollBarMargin,
295+
var scrollBarTrack = legendHeight - constants.scrollBarHeight - 2 * constants.scrollBarMargin,
272296
translateY = scrollBox.attr('data-scroll'),
273-
scrollBoxY = Lib.constrain(translateY - delta, scrollheight-opts.height, 0),
274-
scrollBarY = -scrollBoxY / (opts.height - scrollheight) * scrollBarTrack + constants.scrollBarMargin;
297+
scrollBoxY = Lib.constrain(translateY - delta, legendHeight-opts.height, 0),
298+
scrollBarY = -scrollBoxY / (opts.height - legendHeight) * scrollBarTrack + constants.scrollBarMargin;
275299

276300
scrollBox.attr('data-scroll', scrollBoxY);
277301
scrollBox.attr('transform', 'translate(0, ' + scrollBoxY + ')');

0 commit comments

Comments
 (0)