Skip to content

Provide minimal padding for inside tick labels case of bar, heatmap, line plots #5325

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 7 commits into from
Dec 7, 2020
Merged
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
46 changes: 21 additions & 25 deletions src/plots/cartesian/autorange.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,11 @@ function makePadFn(ax, max) {
if(axReverse) max = !max;
}

extrappad = adjustPadForInsideLabelsOnAnchorAxis(extrappad, ax, max);
extrappad = adjustPadForInsideLabelsOnThisAxis(extrappad, ax, max);
var A = padInsideLabelsOnAnchorAxis(ax, max);
var B = padInsideLabelsOnThisAxis(ax, max);

var zero = Math.max(A, B);
extrappad = Math.max(zero, extrappad);

// domain-constrained axes: base extrappad on the unconstrained
// domain so it's consistent as the domain changes
Expand All @@ -228,18 +231,18 @@ function makePadFn(ax, max) {
(ax.domain[1] - ax.domain[0]);
}

return function getPad(pt) { return pt.pad + (pt.extrapad ? extrappad : 0); };
return function getPad(pt) { return pt.pad + (pt.extrapad ? extrappad : zero); };
}

var TEXTPAD = 3;

function adjustPadForInsideLabelsOnThisAxis(extrappad, ax, max) {
function padInsideLabelsOnThisAxis(ax, max) {
var ticklabelposition = ax.ticklabelposition || '';
var has = function(str) {
return ticklabelposition.indexOf(str) !== -1;
};

if(!has('inside')) return extrappad;
if(!has('inside')) return 0;
var isTop = has('top');
var isLeft = has('left');
var isRight = has('right');
Expand All @@ -250,27 +253,26 @@ function adjustPadForInsideLabelsOnThisAxis(extrappad, ax, max) {
(max && (isLeft || isBottom)) ||
(!max && (isRight || isTop))
) {
return extrappad;
return 0;
}

// increase padding to make more room for inside tick labels of the axis
var fontSize = ax.tickfont ? ax.tickfont.size : 12;
var isX = ax._id.charAt(0) === 'x';
var morePad = (isX ? 1.2 : 0.6) * fontSize;
var pad = (isX ? 1.2 : 0.6) * fontSize;

if(isAligned) {
morePad *= 2;
morePad += (ax.tickwidth || 0) / 2;
pad *= 2;
pad += (ax.tickwidth || 0) / 2;
}

morePad += TEXTPAD;

extrappad = Math.max(extrappad, morePad);
pad += TEXTPAD;

return extrappad;
return pad;
}

function adjustPadForInsideLabelsOnAnchorAxis(extrappad, ax, max) {
function padInsideLabelsOnAnchorAxis(ax, max) {
var pad = 0;
var anchorAxis = (ax._anchorAxis || {});
if((anchorAxis.ticklabelposition || '').indexOf('inside') !== -1) {
// increase padding to make more room for inside tick labels of the counter axis
Expand All @@ -287,7 +289,6 @@ function adjustPadForInsideLabelsOnAnchorAxis(extrappad, ax, max) {
)) {
var isX = ax._id.charAt(0) === 'x';

var morePad = 0;
if(anchorAxis._vals) {
var rad = Lib.deg2rad(anchorAxis._tickAngles[anchorAxis._id + 'tick'] || 0);
var cosA = Math.abs(Math.cos(rad));
Expand All @@ -296,29 +297,24 @@ function adjustPadForInsideLabelsOnAnchorAxis(extrappad, ax, max) {
// use bounding boxes
anchorAxis._vals.forEach(function(t) {
if(t.bb) {
var w = t.bb.width;
var h = t.bb.height;
var w = 2 * TEXTPAD + t.bb.width;
var h = 2 * TEXTPAD + t.bb.height;

morePad = Math.max(morePad, isX ?
pad = Math.max(pad, isX ?
Math.max(w * cosA, h * sinA) :
Math.max(h * cosA, w * sinA)
);

// add extra pad around label
morePad += 3;
}
});
}

if(anchorAxis.ticks === 'inside' && anchorAxis.ticklabelposition === 'inside') {
morePad += anchorAxis.ticklen || 0;
pad += anchorAxis.ticklen || 0;
}

extrappad = Math.max(extrappad, morePad);
}
}

return extrappad;
return pad;
}

function concatExtremes(gd, ax, noMatch) {
Expand Down
31 changes: 20 additions & 11 deletions src/plots/cartesian/axes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3334,27 +3334,36 @@ function drawTitle(gd, ax) {
var axId = ax._id;
var axLetter = axId.charAt(0);
var fontSize = ax.title.font.size;

var titleStandoff;

if(ax.title.hasOwnProperty('standoff')) {
titleStandoff = ax._depth + ax.title.standoff + approxTitleDepth(ax);
} else {
var isInside = (ax.ticklabelposition || '').indexOf('inside') !== -1;

if(ax.type === 'multicategory') {
titleStandoff = ax._depth;
} else {
var offsetBase = 1.5;
titleStandoff = 10 + fontSize * offsetBase + (ax.linewidth ? ax.linewidth - 1 : 0);
var offsetBase = 1.5 * fontSize;
if(isInside) {
offsetBase = 0.5 * fontSize;
if(ax.ticks === 'outside') {
offsetBase += ax.ticklen;
}
}
titleStandoff = 10 + offsetBase + (ax.linewidth ? ax.linewidth - 1 : 0);
}

if(axLetter === 'x') {
titleStandoff += ax.side === 'top' ?
fontSize * (ax.showticklabels ? 1 : 0) :
fontSize * (ax.showticklabels ? 1.5 : 0.5);
} else {
titleStandoff += ax.side === 'right' ?
fontSize * (ax.showticklabels ? 1 : 0.5) :
fontSize * (ax.showticklabels ? 0.5 : 0);
if(!isInside) {
if(axLetter === 'x') {
titleStandoff += ax.side === 'top' ?
fontSize * (ax.showticklabels ? 1 : 0) :
fontSize * (ax.showticklabels ? 1.5 : 0.5);
} else {
titleStandoff += ax.side === 'right' ?
fontSize * (ax.showticklabels ? 1 : 0.5) :
fontSize * (ax.showticklabels ? 0.5 : 0);
}
}
}

Expand Down
Binary file modified test/image/baselines/ticklabelposition-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/ticklabelposition-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/image/baselines/ticklabelposition-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/image/baselines/ticklabelposition-4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/ticklabelposition-a.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/ticklabelposition-b.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/ticklabelposition-c.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/ticklabelposition-d.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
114 changes: 114 additions & 0 deletions test/image/mocks/ticklabelposition-3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
{
"data": [{
"xaxis": "x",
"yaxis": "y",
"type": "bar",
"orientation": "h",
"x": [1, 10, 100],
"y": [1, 10, 100]
}, {
"xaxis": "x2",
"yaxis": "y2",
"type": "heatmap",
"colorbar": {
"tickangle": -90,
"dtick": 10,
"ticks": "inside",
"ticklen": 12,
"tickwidth": 4,
"ticklabelposition": "inside top"
},
"x": [1, 10, 100],
"y": [1, 10, 100],
"z": [
[0, 50, 100],
[50, 100, 0],
[100, 0, 50]
]
}, {
"xaxis": "x3",
"yaxis": "y3",
"type": "scatter",
"mode": "lines",
"x": [-100, 0, 100],
"y": [-100, 0, 100]
}, {
"xaxis": "x4",
"yaxis": "y4",
"type": "bar",
"orientation": "v",
"x": [1, 10, 100],
"y": [1, 10, 100]
}],
"layout": {
"xaxis": {
"anchor": "y",
"domain": [0, 0.475],
"side": "bottom",
"ticklabelposition": "inside",
"gridcolor": "white"
},
"yaxis": {
"anchor": "x",
"domain": [0, 0.475],
"side": "left",
"ticklabelposition": "inside",
"gridcolor": "white"
},
"xaxis2": {
"anchor": "y2",
"domain": [0.525, 1],
"side": "bottom",
"ticklabelposition": "inside",
"gridcolor": "white"
},
"yaxis2": {
"anchor": "x2",
"domain": [0, 0.475],
"side": "right",
"ticklabelposition": "inside",
"gridcolor": "white"
},
"xaxis3": {
"anchor": "y3",
"domain": [0.525, 1],
"side": "top",
"ticklabelposition": "inside",
"gridcolor": "white"
},
"yaxis3": {
"anchor": "x3",
"domain": [0.525, 1],
"side": "right",
"ticklabelposition": "inside",
"gridcolor": "white"
},
"xaxis4": {
"anchor": "y4",
"domain": [0, 0.475],
"side": "top",
"ticklabelposition": "inside",
"gridcolor": "white"
},
"yaxis4": {
"anchor": "x4",
"domain": [0.525, 1],
"side": "left",
"ticklabelposition": "inside",
"gridcolor": "white"
},
"font": {
"size": 24
},
"plot_bgcolor": "lightblue",
"showlegend": false,
"width": 1000,
"height": 1000,
"margin": {
"t": 40,
"b": 40,
"l": 40,
"r": 40
}
}
}
Loading