') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); })(); improve legend positioning in edge cases by antoinerg · Pull Request #3024 · plotly/plotly.js · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/components/legend/draw.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -169,10 +169,9 @@ module.exports = function draw(gd) {

// Make sure the legend left and right sides are visible
var legendWidth = opts._width;
var legendWidthMax = gs.w;
var legendWidthMax = gs.w * Math.max(1 - opts.x, 1);

if(legendWidth > legendWidthMax) {
lx = gs.l;
legendWidth = legendWidthMax;
}
else {
Expand All@@ -185,11 +184,14 @@ module.exports = function draw(gd) {
// (legends with a scroll bar are not allowed to stretch beyond the extended
// margins)
var legendHeight = opts._height;
var legendHeightMax = gs.h;
var legendHeightMax = gs.h * Math.max(opts.y, 1);

var padY = 12;
if(legendHeight > legendHeightMax) {
ly = gs.t;
legendHeight = legendHeightMax;
if(!helpers.isVertical(opts)) {
ly += padY;
}
}
else {
if(ly + legendHeight > lyMax) ly = lyMax - legendHeight;
Expand DownExpand Up@@ -638,13 +640,14 @@ function computeLegendDimensions(gd, groups, traces) {
});

// check if legend fits in one row
var oneRowLegend = fullLayout._size.w > borderwidth + fullTracesWidth - traceGap;
var legendWidthMax = fullLayout._size.w * Math.max(1 - opts.x, 1);
var oneRowLegend = legendWidthMax > borderwidth + fullTracesWidth - traceGap;

traces.each(function(d) {
var legendItem = d[0];
var traceWidth = oneRowLegend ? 40 + d[0].width : maxTraceWidth;

if((borderwidth + offsetX + traceGap + traceWidth) > fullLayout._size.w) {
if((borderwidth + offsetX + traceGap + traceWidth) > legendWidthMax) {
offsetX = 0;
rowHeight += maxTraceHeight;
opts._height += maxTraceHeight;
Expand Down
13 changes: 11 additions & 2 deletions src/plots/plots.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -1802,8 +1802,17 @@ plots.autoMargin = function(gd, id, o) {

// if the item is too big, just give it enough automargin to
// make sure you can still grab it and bring it back
if(o.l + o.r > fullLayout.width * 0.5) o.l = o.r = 0;
if(o.b + o.t > fullLayout.height * 0.5) o.b = o.t = 0;
var rescaleFactor;
if(o.l + o.r > (fullLayout.width * 0.5)) {
rescaleFactor = (fullLayout.width * 0.5) / (o.l + o.r);
o.l *= rescaleFactor;
o.r *= rescaleFactor;
}
if(o.b + o.t > (fullLayout.height * 0.5)) {
rescaleFactor = (fullLayout.height * 0.5) / (o.b + o.t);
o.b *= rescaleFactor;
o.t *= rescaleFactor;
}

var xl = o.xl !== undefined ? o.xl : o.x;
var xr = o.xr !== undefined ? o.xr : o.x;
Expand Down
Binary file addedtest/image/baselines/legend_negative_x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file addedtest/image/baselines/legend_small_vertical.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
51 changes: 51 additions & 0 deletions test/image/mocks/legend_negative_x.json
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
{
"data": [{
"x": [0, 1, 2, 3, 4, 5, 6, 7, 8],
"y": [0, 3, 6, 4, 5, 2, 3, 5, 4],
"type": "scatter"
},
{
"x": [0, 1, 2, 3, 4, 5, 6, 7, 8],
"y": [0, 4, 7, 8, 3, 6, 3, 3, 4],
"type": "scatter"
},
{
"x": [0, 1, 2, 3, 4, 5, 6, 7, 8],
"y": [0, 4, 7, 8, 3, 6, 3, 3, 4],
"type": "scatter"
},
{
"x": [0, 1, 2, 3, 4, 5, 6, 7, 8],
"y": [0, 4, 7, 8, 3, 6, 3, 3, 4],
"type": "scatter"
},
{
"x": [0, 1, 2, 3, 4, 5, 6, 7, 8],
"y": [0, 4, 7, 8, 3, 6, 3, 3, 4],
"type": "scatter"
},
{
"x": [0, 1, 2, 3, 4, 5, 6, 7, 8],
"y": [0, 4, 7, 8, 3, 6, 3, 3, 4],
"type": "scatter"
}
],
"layout": {
"showlegend": true,
"legend": {
"orientation": "h",
"traceorder": "reversed",
"x": -0.5,
"y": 1.2
},
"margin": {
"l": 125,
"r": 5,
"b": 30,
"t": 20,
"pad": 0
},
"height": 350,
"width": 700
}
}
104 changes: 104 additions & 0 deletions test/image/mocks/legend_small_horizontal.json
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
{
"layout": {
"legend": {
"orientation": "h",
"bordercolor": "#000000",
"borderwidth": 1,
"bgcolor": "#ffffff00"
},
"margin": {
"t": 25,
"b": 25,
"r": 25,
"l": 25
},
"width":500,
"height":200
},
"data": [
{
"x": [1, 2, 3, 4],
"y": [63.69, 62.55, 61.64, 61.39]
}, {
"x": [1, 2, 3, 4],
"y": [58.24, 54.93, 42.11, 50.75]
}, {
"x": [1, 2, 3, 4],
"y": [51.49, 49.59, 37.12, 31.45]
}, {
"x": [1, 2, 3, 4],
"y": [49.09, 58.54, 53.91, 43.12]
}, {
"x": [1, 2, 3, 4],
"y": [70.53, 72.51, 72.28, 78.65]
}, {
"x": [1, 2, 3, 4],
"y": [62.69, 59.09, 63.82, 62]
}, {
"x": [1, 2, 3, 4],
"y": [76.27, 71.43, 59.83, 64.34]
}, {
"x": [1, 2, 3, 4],
"y": [71.15, 81.82, 88.46, 74.29]
}, {
"x": [1, 2, 3, 4],
"y": [57.89, 57.38, 52.08, 63.83]
}, {
"x": [1, 2, 3, 4],
"y": [65.4, 63.27, 65.78, 64.03]
}, {
"x": [1, 2, 3, 4],
"y": [58.24, 54.93, 42.11, 50.75]
}, {
"x": [1, 2, 3, 4],
"y": [51.49, 49.59, 37.12, 31.45]
}, {
"x": [1, 2, 3, 4],
"y": [49.09, 58.54, 53.91, 43.12]
}, {
"x": [1, 2, 3, 4],
"y": [70.53, 72.51, 72.28, 78.65]
}, {
"x": [1, 2, 3, 4],
"y": [62.69, 59.09, 63.82, 62]
}, {
"x": [1, 2, 3, 4],
"y": [76.27, 71.43, 59.83, 64.34]
}, {
"x": [1, 2, 3, 4],
"y": [71.15, 81.82, 88.46, 74.29]
}, {
"x": [1, 2, 3, 4],
"y": [57.89, 57.38, 52.08, 63.83]
}, {
"x": [1, 2, 3, 4],
"y": [65.4, 63.27, 65.78, 64.03]
}, {
"x": [1, 2, 3, 4],
"y": [58.24, 54.93, 42.11, 50.75]
}, {
"x": [1, 2, 3, 4],
"y": [51.49, 49.59, 37.12, 31.45]
}, {
"x": [1, 2, 3, 4],
"y": [49.09, 58.54, 53.91, 43.12]
}, {
"x": [1, 2, 3, 4],
"y": [70.53, 72.51, 72.28, 78.65]
}, {
"x": [1, 2, 3, 4],
"y": [62.69, 59.09, 63.82, 62]
}, {
"x": [1, 2, 3, 4],
"y": [76.27, 71.43, 59.83, 64.34]
}, {
"x": [1, 2, 3, 4],
"y": [71.15, 81.82, 88.46, 74.29]
}, {
"x": [1, 2, 3, 4],
"y": [57.89, 57.38, 52.08, 63.83]
}, {
"x": [1, 2, 3, 4],
"y": [65.4, 63.27, 65.78, 64.03]
}]
}
104 changes: 104 additions & 0 deletions test/image/mocks/legend_small_vertical.json
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
{
"layout": {
"legend": {
"orientation": "v",
"bordercolor": "#000000",
"borderwidth": 1,
"bgcolor": "#ffffff00"
},
"margin": {
"t": 25,
"b": 25,
"r": 25,
"l": 25
},
"width":200,
"height":500
},
"data": [
{
"x": [1, 2, 3, 4],
"y": [63.69, 62.55, 61.64, 61.39]
}, {
"x": [1, 2, 3, 4],
"y": [58.24, 54.93, 42.11, 50.75]
}, {
"x": [1, 2, 3, 4],
"y": [51.49, 49.59, 37.12, 31.45]
}, {
"x": [1, 2, 3, 4],
"y": [49.09, 58.54, 53.91, 43.12]
}, {
"x": [1, 2, 3, 4],
"y": [70.53, 72.51, 72.28, 78.65]
}, {
"x": [1, 2, 3, 4],
"y": [62.69, 59.09, 63.82, 62]
}, {
"x": [1, 2, 3, 4],
"y": [76.27, 71.43, 59.83, 64.34]
}, {
"x": [1, 2, 3, 4],
"y": [71.15, 81.82, 88.46, 74.29]
}, {
"x": [1, 2, 3, 4],
"y": [57.89, 57.38, 52.08, 63.83]
}, {
"x": [1, 2, 3, 4],
"y": [65.4, 63.27, 65.78, 64.03]
}, {
"x": [1, 2, 3, 4],
"y": [58.24, 54.93, 42.11, 50.75]
}, {
"x": [1, 2, 3, 4],
"y": [51.49, 49.59, 37.12, 31.45]
}, {
"x": [1, 2, 3, 4],
"y": [49.09, 58.54, 53.91, 43.12]
}, {
"x": [1, 2, 3, 4],
"y": [70.53, 72.51, 72.28, 78.65]
}, {
"x": [1, 2, 3, 4],
"y": [62.69, 59.09, 63.82, 62]
}, {
"x": [1, 2, 3, 4],
"y": [76.27, 71.43, 59.83, 64.34]
}, {
"x": [1, 2, 3, 4],
"y": [71.15, 81.82, 88.46, 74.29]
}, {
"x": [1, 2, 3, 4],
"y": [57.89, 57.38, 52.08, 63.83]
}, {
"x": [1, 2, 3, 4],
"y": [65.4, 63.27, 65.78, 64.03]
}, {
"x": [1, 2, 3, 4],
"y": [58.24, 54.93, 42.11, 50.75]
}, {
"x": [1, 2, 3, 4],
"y": [51.49, 49.59, 37.12, 31.45]
}, {
"x": [1, 2, 3, 4],
"y": [49.09, 58.54, 53.91, 43.12]
}, {
"x": [1, 2, 3, 4],
"y": [70.53, 72.51, 72.28, 78.65]
}, {
"x": [1, 2, 3, 4],
"y": [62.69, 59.09, 63.82, 62]
}, {
"x": [1, 2, 3, 4],
"y": [76.27, 71.43, 59.83, 64.34]
}, {
"x": [1, 2, 3, 4],
"y": [71.15, 81.82, 88.46, 74.29]
}, {
"x": [1, 2, 3, 4],
"y": [57.89, 57.38, 52.08, 63.83]
}, {
"x": [1, 2, 3, 4],
"y": [65.4, 63.27, 65.78, 64.03]
}]
}