Skip to content

Automargin edge cases #3605

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 5, 2019
Merged
Show file tree
Hide file tree
Changes from 4 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
24 changes: 24 additions & 0 deletions src/plots/cartesian/axes.js
Original file line number Diff line number Diff line change
Expand Up @@ -1984,6 +1984,30 @@ axes.drawOne = function(gd, ax, opts) {
if(ax.title.text !== fullLayout._dfltTitle[axLetter]) {
push[s] += ax.title.font.size;
}

if(axLetter === 'x' && bbox.width > 0) {
var rExtra = bbox.right - (ax._offset + ax._length);
if(rExtra > 0) {
push.x = 1;
push.r = rExtra;
}
var lExtra = ax._offset - bbox.left;
if(lExtra > 0) {
push.x = 0;
push.l = lExtra;
}
} else if(axLetter === 'y' && bbox.height > 0) {
var bExtra = bbox.bottom - (ax._offset + ax._length);
if(bExtra > 0) {
push.y = 0;
push.b = bExtra;
}
var tExtra = ax._offset - bbox.top;
if(tExtra > 0) {
push.y = 1;
push.t = tExtra;
}
}
}

Plots.autoMargin(gd, axAutoMarginID(ax), push);
Expand Down
5 changes: 2 additions & 3 deletions src/plots/cartesian/set_convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -484,15 +484,14 @@ module.exports = function setConvert(ax, fullLayout) {
ax._length = gs.h * (ax.domain[1] - ax.domain[0]);
ax._m = ax._length / (rl0 - rl1);
ax._b = -ax._m * rl1;
}
else {
} else {
ax._offset = gs.l + ax.domain[0] * gs.w;
ax._length = gs.w * (ax.domain[1] - ax.domain[0]);
ax._m = ax._length / (rl1 - rl0);
ax._b = -ax._m * rl0;
}

if(!isFinite(ax._m) || !isFinite(ax._b)) {
if(!isFinite(ax._m) || !isFinite(ax._b) || ax._length < 0) {
fullLayout._replotting = false;
throw new Error('Something went wrong with axis scaling');
}
Expand Down
22 changes: 10 additions & 12 deletions src/plots/plots.js
Original file line number Diff line number Diff line change
Expand Up @@ -1841,6 +1841,8 @@ plots.doAutoMargin = function(gd) {
var mr = margin.r;
var mt = margin.t;
var mb = margin.b;
var width = fullLayout.width;
var height = fullLayout.height;
var pushMargin = fullLayout._pushmargin;
var pushMarginIds = fullLayout._pushmarginIds;

Expand Down Expand Up @@ -1876,11 +1878,9 @@ plots.doAutoMargin = function(gd) {
var pr = pushMargin[k2].r.size;

if(fr > fl) {
var newl = (pl * fr +
(pr - fullLayout.width) * fl) / (fr - fl);
var newr = (pr * (1 - fl) +
(pl - fullLayout.width) * (1 - fr)) / (fr - fl);
if(newl >= 0 && newr >= 0 && newl + newr > ml + mr) {
var newl = (pl * fr + (pr - width) * fl) / (fr - fl);
var newr = (pr * (1 - fl) + (pl - width) * (1 - fr)) / (fr - fl);
if(newl >= 0 && newr >= 0 && width - (newl + newr) > 0 && newl + newr > ml + mr) {
ml = newl;
mr = newr;
}
Expand All @@ -1892,11 +1892,9 @@ plots.doAutoMargin = function(gd) {
var pt = pushMargin[k2].t.size;

if(ft > fb) {
var newb = (pb * ft +
(pt - fullLayout.height) * fb) / (ft - fb);
var newt = (pt * (1 - fb) +
(pb - fullLayout.height) * (1 - ft)) / (ft - fb);
if(newb >= 0 && newt >= 0 && newb + newt > mb + mt) {
var newb = (pb * ft + (pt - height) * fb) / (ft - fb);
var newt = (pt * (1 - fb) + (pb - height) * (1 - ft)) / (ft - fb);
if(newb >= 0 && newt >= 0 && height - (newt + newb) > 0 && newb + newt > mb + mt) {
mb = newb;
mt = newt;
}
Expand All @@ -1911,8 +1909,8 @@ plots.doAutoMargin = function(gd) {
gs.t = Math.round(mt);
gs.b = Math.round(mb);
gs.p = Math.round(margin.pad);
gs.w = Math.round(fullLayout.width) - gs.l - gs.r;
gs.h = Math.round(fullLayout.height) - gs.t - gs.b;
gs.w = Math.round(width) - gs.l - gs.r;
gs.h = Math.round(height) - gs.t - gs.b;

// if things changed and we're not already redrawing, trigger a redraw
if(!fullLayout._replotting &&
Expand Down
Binary file added test/image/baselines/automargin-push-x-extra.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.
109 changes: 109 additions & 0 deletions test/image/mocks/automargin-push-x-extra.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
{
"data": [
{
"x": [
"12 AM diagonal",
"1 AM diagonal",
"2 AM diagonal",
"3 AM diagonal",
"4 AM diagonal",
"5 AM diagonal",
"6 AM diagonal",
"7 AM diagonal",
"8 AM diagonal",
"9 AM diagonal",
"10 AM diagonal",
"11 AM diagonal"
],
"y": [
59.44,
68.75,
87.5,
100.5,
95.56,
92.8,
85.25,
77.4,
76.4,
73.94,
74.56,
81.06
]
},
{
"x": [
"12 AM diagonal",
"1 AM diagonal",
"2 AM diagonal",
"3 AM diagonal",
"4 AM diagonal",
"5 AM diagonal",
"6 AM diagonal",
"7 AM diagonal",
"8 AM diagonal",
"9 AM diagonal",
"10 AM diagonal",
"11 AM diagonal"
],
"y": [
59.44,
68.75,
87.5,
100.5,
95.56,
92.8,
85.25,
77.4,
76.4,
73.94,
74.56,
81.06
],
"xaxis": "x2",
"yaxis": "y2"
}
],
"layout": {
"grid": {
"columns": 2,
"rows": 1,
"pattern": "independent"
},
"margin": {
"l": 0,
"r": 0,
"t": 0,
"b": 0
},
"xaxis": {
"showgrid": false,
"showline": true,
"zeroline": true,
"automargin": true,
"side": "top",
"tickangle": 45
},
"yaxis": {
"side": "right",
"showgrid": false,
"showline": true,
"zeroline": true,
"automargin": true
},
"xaxis2": {
"showgrid": false,
"showline": true,
"zeroline": true,
"automargin": true,
"tickangle": 45
},
"yaxis2": {
"side": "left",
"showgrid": false,
"showline": true,
"zeroline": true,
"automargin": true
},
"showlegend": false
}
}
109 changes: 109 additions & 0 deletions test/image/mocks/automargin-push-y-extra.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
{
"data": [
{
"y": [
"12 AM diagonal",
"1 AM diagonal",
"2 AM diagonal",
"3 AM diagonal",
"4 AM diagonal",
"5 AM diagonal",
"6 AM diagonal",
"7 AM diagonal",
"8 AM diagonal",
"9 AM diagonal",
"10 AM diagonal",
"11 AM diagonal"
],
"x": [
59.44,
68.75,
87.5,
100.5,
95.56,
92.8,
85.25,
77.4,
76.4,
73.94,
74.56,
81.06
]
},
{
"y": [
"12 AM diagonal",
"1 AM diagonal",
"2 AM diagonal",
"3 AM diagonal",
"4 AM diagonal",
"5 AM diagonal",
"6 AM diagonal",
"7 AM diagonal",
"8 AM diagonal",
"9 AM diagonal",
"10 AM diagonal",
"11 AM diagonal"
],
"x": [
81.06,
74.56,
73.94,
76.4,
77.4,
85.25,
92.8,
95.56,
100.5,
87.5,
68.75,
59.44
],
"xaxis": "x2",
"yaxis": "y2"
}
],
"layout": {
"grid": {
"columns": 1,
"rows": 2,
"pattern": "independent"
},
"margin": {
"l": 0,
"r": 0,
"t": 0,
"b": 0
},
"xaxis": {
"showgrid": false,
"showline": true,
"zeroline": true,
"automargin": true,
"side": "bottom"
},
"yaxis": {
"side": "right",
"showgrid": false,
"showline": true,
"zeroline": true,
"automargin": true,
"tickangle": -45
},
"xaxis2": {
"showgrid": false,
"showline": true,
"zeroline": true,
"automargin": true,
"side": "top"
},
"yaxis2": {
"showgrid": false,
"showline": true,
"zeroline": true,
"automargin": true,
"tickangle": -45
},
"showlegend": false
}
}
Loading