Skip to content

Commit f0709c3

Browse files
committed
rewrite and reduce the number of calls to estimate padding
1 parent f62f186 commit f0709c3

File tree

1 file changed

+17
-22
lines changed

1 file changed

+17
-22
lines changed

src/plots/cartesian/autorange.js

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -218,12 +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);
223223

224-
var pad0 = 0;
225-
pad0 = adjustPadForInsideLabelsOnAnchorAxis(pad0, ax, max);
226-
pad0 = adjustPadForInsideLabelsOnThisAxis(pad0, ax, max);
224+
var zero = Math.max(A, B);
225+
extrappad = Math.max(zero, extrappad);
227226

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

235-
return function getPad(pt) { return pt.pad + (pt.extrapad ? extrappad : pad0); };
234+
return function getPad(pt) { return pt.pad + (pt.extrapad ? extrappad : zero); };
236235
}
237236

238237
var TEXTPAD = 3;
239238

240-
function adjustPadForInsideLabelsOnThisAxis(pad, ax, max) {
239+
function padInsideLabelsOnThisAxis(ax, max) {
241240
var ticklabelposition = ax.ticklabelposition || '';
242241
var has = function(str) {
243242
return ticklabelposition.indexOf(str) !== -1;
244243
};
245244

246-
if(!has('inside')) return pad;
245+
if(!has('inside')) return 0;
247246
var isTop = has('top');
248247
var isLeft = has('left');
249248
var isRight = has('right');
@@ -254,27 +253,26 @@ function adjustPadForInsideLabelsOnThisAxis(pad, ax, max) {
254253
(max && (isLeft || isBottom)) ||
255254
(!max && (isRight || isTop))
256255
) {
257-
return pad;
256+
return 0;
258257
}
259258

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

265264
if(isAligned) {
266-
morePad *= 2;
267-
morePad += (ax.tickwidth || 0) / 2;
265+
pad *= 2;
266+
pad += (ax.tickwidth || 0) / 2;
268267
}
269268

270-
morePad += TEXTPAD;
271-
272-
pad = Math.max(pad, morePad);
269+
pad += TEXTPAD;
273270

274271
return pad;
275272
}
276273

277-
function adjustPadForInsideLabelsOnAnchorAxis(pad, ax, max) {
274+
function padInsideLabelsOnAnchorAxis(ax, max) {
275+
var pad = 0;
278276
var anchorAxis = (ax._anchorAxis || {});
279277
if((anchorAxis.ticklabelposition || '').indexOf('inside') !== -1) {
280278
// increase padding to make more room for inside tick labels of the counter axis
@@ -291,7 +289,6 @@ function adjustPadForInsideLabelsOnAnchorAxis(pad, ax, max) {
291289
)) {
292290
var isX = ax._id.charAt(0) === 'x';
293291

294-
var morePad = 0;
295292
if(anchorAxis._vals) {
296293
var rad = Lib.deg2rad(anchorAxis._tickAngles[anchorAxis._id + 'tick'] || 0);
297294
var cosA = Math.abs(Math.cos(rad));
@@ -303,22 +300,20 @@ function adjustPadForInsideLabelsOnAnchorAxis(pad, ax, max) {
303300
var w = t.bb.width;
304301
var h = t.bb.height;
305302

306-
morePad = Math.max(morePad, isX ?
303+
pad = Math.max(pad, isX ?
307304
Math.max(w * cosA, h * sinA) :
308305
Math.max(h * cosA, w * sinA)
309306
);
310307

311308
// add extra pad around label
312-
morePad += 3;
309+
pad += 3;
313310
}
314311
});
315312
}
316313

317314
if(anchorAxis.ticks === 'inside' && anchorAxis.ticklabelposition === 'inside') {
318-
morePad += anchorAxis.ticklen || 0;
315+
pad += anchorAxis.ticklen || 0;
319316
}
320-
321-
pad = Math.max(pad, morePad);
322317
}
323318
}
324319

0 commit comments

Comments
 (0)