Skip to content

Commit 7829f2f

Browse files
authored
Merge pull request #6616 from plotly/bugfix/container-ref-margin
Bugfix: Only add minimum margin-spacing for container referenced components
2 parents 384e492 + 3e8ddd9 commit 7829f2f

File tree

4 files changed

+72
-9
lines changed

4 files changed

+72
-9
lines changed

Diff for: draftlogs/6616_fix.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Ensure that only minimum margin spacing is added for container-referenced legends and colorbars [[#6616](https://github.com/plotly/plotly.js/pull/6616)]

Diff for: src/plots/plots.js

+23-9
Original file line numberDiff line numberDiff line change
@@ -2010,13 +2010,6 @@ plots.doAutoMargin = function(gd) {
20102010
var reservedMargins = {t: 0, b: 0, l: 0, r: 0};
20112011
var oldMargins = Lib.extendFlat({}, gs);
20122012

2013-
var margins = gd._fullLayout._reservedMargin;
2014-
for(var key in margins) {
2015-
for(var side in margins[key]) {
2016-
var val = margins[key][side];
2017-
reservedMargins[side] = Math.max(reservedMargins[side], val);
2018-
}
2019-
}
20202013
// adjust margins for outside components
20212014
// fullLayout.margin is the requested margin,
20222015
// fullLayout._size has margins and plotsize after adjustment
@@ -2029,11 +2022,18 @@ plots.doAutoMargin = function(gd) {
20292022
var minreducedwidth = fullLayout.minreducedwidth;
20302023
var minreducedheight = fullLayout.minreducedheight;
20312024

2032-
if(fullLayout.margin.autoexpand !== false) {
2025+
if(margin.autoexpand !== false) {
20332026
for(var k in pushMargin) {
20342027
if(!pushMarginIds[k]) delete pushMargin[k];
20352028
}
20362029

2030+
var margins = gd._fullLayout._reservedMargin;
2031+
for(var key in margins) {
2032+
for(var side in margins[key]) {
2033+
var val = margins[key][side];
2034+
reservedMargins[side] = Math.max(reservedMargins[side], val);
2035+
}
2036+
}
20372037
// fill in the requested margins
20382038
pushMargin.base = {
20392039
l: {val: 0, size: ml},
@@ -2042,9 +2042,23 @@ plots.doAutoMargin = function(gd) {
20422042
b: {val: 0, size: mb}
20432043
};
20442044

2045+
2046+
// make sure that the reservedMargin is the minimum needed
2047+
for(var s in reservedMargins) {
2048+
var autoMarginPush = 0;
2049+
for(var m in pushMargin) {
2050+
if(m !== 'base') {
2051+
if(isNumeric(pushMargin[m][s].size)) {
2052+
autoMarginPush = pushMargin[m][s].size > autoMarginPush ? pushMargin[m][s].size : autoMarginPush;
2053+
}
2054+
}
2055+
}
2056+
var extraMargin = Math.max(0, (margin[s] - autoMarginPush));
2057+
reservedMargins[s] = Math.max(0, reservedMargins[s] - extraMargin);
2058+
}
2059+
20452060
// now cycle through all the combinations of l and r
20462061
// (and t and b) to find the required margins
2047-
20482062
for(var k1 in pushMargin) {
20492063
var pushleft = pushMargin[k1].l || {};
20502064
var pushbottom = pushMargin[k1].b || {};
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"data": [
3+
{
4+
"colorbar": {
5+
"orientation": "v",
6+
"xref": "container",
7+
"x": 1,
8+
"thickness": 10,
9+
"ticks": "outside",
10+
"bgcolor": "rgba(255,255,0,0.5)",
11+
"borderwidth": 4,
12+
"bordercolor": "gray",
13+
"title": {
14+
"side": "top",
15+
"text": "Colorbar<br>title",
16+
"font": {
17+
"size": 16
18+
}
19+
}
20+
},
21+
"z": [
22+
[
23+
1,
24+
3,
25+
5
26+
],
27+
[
28+
4,
29+
7,
30+
10
31+
],
32+
[
33+
7,
34+
11,
35+
14
36+
]
37+
],
38+
"type": "heatmap"
39+
}
40+
],
41+
"layout": {
42+
"margin": {"l": 0, "r": 148, "t": 0, "b": 0},
43+
"height": 300,
44+
"width": 400,
45+
"yaxis": {"automargin": true, "side": "right", "title": {"text": "Y-axis title"}},
46+
"title": {"text": "Colorbar with `xref='container'` | vertical | margin", "automargin": true}
47+
}
48+
}

0 commit comments

Comments
 (0)