Skip to content

Commit 4e00196

Browse files
authored
Merge pull request #5325 from plotly/pad-for-insideticklabels
Provide minimal padding for inside tick labels case of bar, heatmap, line plots
2 parents 9ffa03d + 8199d38 commit 4e00196

13 files changed

+3485
-36
lines changed

src/plots/cartesian/autorange.js

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,11 @@ function makePadFn(ax, max) {
218218
if(axReverse) max = !max;
219219
}
220220

221-
extrappad = adjustPadForInsideLabelsOnAnchorAxis(extrappad, ax, max);
222-
extrappad = adjustPadForInsideLabelsOnThisAxis(extrappad, ax, max);
221+
var A = padInsideLabelsOnAnchorAxis(ax, max);
222+
var B = padInsideLabelsOnThisAxis(ax, max);
223+
224+
var zero = Math.max(A, B);
225+
extrappad = Math.max(zero, extrappad);
223226

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

231-
return function getPad(pt) { return pt.pad + (pt.extrapad ? extrappad : 0); };
234+
return function getPad(pt) { return pt.pad + (pt.extrapad ? extrappad : zero); };
232235
}
233236

234237
var TEXTPAD = 3;
235238

236-
function adjustPadForInsideLabelsOnThisAxis(extrappad, ax, max) {
239+
function padInsideLabelsOnThisAxis(ax, max) {
237240
var ticklabelposition = ax.ticklabelposition || '';
238241
var has = function(str) {
239242
return ticklabelposition.indexOf(str) !== -1;
240243
};
241244

242-
if(!has('inside')) return extrappad;
245+
if(!has('inside')) return 0;
243246
var isTop = has('top');
244247
var isLeft = has('left');
245248
var isRight = has('right');
@@ -250,27 +253,26 @@ function adjustPadForInsideLabelsOnThisAxis(extrappad, ax, max) {
250253
(max && (isLeft || isBottom)) ||
251254
(!max && (isRight || isTop))
252255
) {
253-
return extrappad;
256+
return 0;
254257
}
255258

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

261264
if(isAligned) {
262-
morePad *= 2;
263-
morePad += (ax.tickwidth || 0) / 2;
265+
pad *= 2;
266+
pad += (ax.tickwidth || 0) / 2;
264267
}
265268

266-
morePad += TEXTPAD;
267-
268-
extrappad = Math.max(extrappad, morePad);
269+
pad += TEXTPAD;
269270

270-
return extrappad;
271+
return pad;
271272
}
272273

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

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

302-
morePad = Math.max(morePad, isX ?
303+
pad = Math.max(pad, isX ?
303304
Math.max(w * cosA, h * sinA) :
304305
Math.max(h * cosA, w * sinA)
305306
);
306-
307-
// add extra pad around label
308-
morePad += 3;
309307
}
310308
});
311309
}
312310

313311
if(anchorAxis.ticks === 'inside' && anchorAxis.ticklabelposition === 'inside') {
314-
morePad += anchorAxis.ticklen || 0;
312+
pad += anchorAxis.ticklen || 0;
315313
}
316-
317-
extrappad = Math.max(extrappad, morePad);
318314
}
319315
}
320316

321-
return extrappad;
317+
return pad;
322318
}
323319

324320
function concatExtremes(gd, ax, noMatch) {

src/plots/cartesian/axes.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3334,27 +3334,36 @@ function drawTitle(gd, ax) {
33343334
var axId = ax._id;
33353335
var axLetter = axId.charAt(0);
33363336
var fontSize = ax.title.font.size;
3337-
33383337
var titleStandoff;
33393338

33403339
if(ax.title.hasOwnProperty('standoff')) {
33413340
titleStandoff = ax._depth + ax.title.standoff + approxTitleDepth(ax);
33423341
} else {
3342+
var isInside = (ax.ticklabelposition || '').indexOf('inside') !== -1;
3343+
33433344
if(ax.type === 'multicategory') {
33443345
titleStandoff = ax._depth;
33453346
} else {
3346-
var offsetBase = 1.5;
3347-
titleStandoff = 10 + fontSize * offsetBase + (ax.linewidth ? ax.linewidth - 1 : 0);
3347+
var offsetBase = 1.5 * fontSize;
3348+
if(isInside) {
3349+
offsetBase = 0.5 * fontSize;
3350+
if(ax.ticks === 'outside') {
3351+
offsetBase += ax.ticklen;
3352+
}
3353+
}
3354+
titleStandoff = 10 + offsetBase + (ax.linewidth ? ax.linewidth - 1 : 0);
33483355
}
33493356

3350-
if(axLetter === 'x') {
3351-
titleStandoff += ax.side === 'top' ?
3352-
fontSize * (ax.showticklabels ? 1 : 0) :
3353-
fontSize * (ax.showticklabels ? 1.5 : 0.5);
3354-
} else {
3355-
titleStandoff += ax.side === 'right' ?
3356-
fontSize * (ax.showticklabels ? 1 : 0.5) :
3357-
fontSize * (ax.showticklabels ? 0.5 : 0);
3357+
if(!isInside) {
3358+
if(axLetter === 'x') {
3359+
titleStandoff += ax.side === 'top' ?
3360+
fontSize * (ax.showticklabels ? 1 : 0) :
3361+
fontSize * (ax.showticklabels ? 1.5 : 0.5);
3362+
} else {
3363+
titleStandoff += ax.side === 'right' ?
3364+
fontSize * (ax.showticklabels ? 1 : 0.5) :
3365+
fontSize * (ax.showticklabels ? 0.5 : 0);
3366+
}
33583367
}
33593368
}
33603369

-1.34 KB
Loading
714 Bytes
Loading
106 KB
Loading
99.4 KB
Loading
-2.08 KB
Loading
1.72 KB
Loading
101 Bytes
Loading
171 Bytes
Loading

0 commit comments

Comments
 (0)