Skip to content

Commit 4f9e1fb

Browse files
committed
Improve rotation and padding calculation
1 parent 1145446 commit 4f9e1fb

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

src/core/core.scale.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ module.exports = Element.extend({
414414
var options = me.options;
415415
var tickOpts = options.ticks;
416416
var ticks = me.getTicks();
417-
var labelSizes, maxLabelWidth, maxLabelHeight, tickWidth, maxHeight, maxLabelDiagonal;
417+
var labelSizes, maxLabelWidth, maxLabelHeight, maxWidth, tickWidth, maxHeight, maxLabelDiagonal;
418418

419419
var labelRotation = tickOpts.minRotation || 0;
420420

@@ -427,10 +427,12 @@ module.exports = Element.extend({
427427

428428
// Estimate the width of each grid based on the canvas width, the maximum
429429
// label width and the number of tick intervals
430-
tickWidth = (me.chart.width - maxLabelWidth) / (ticks.length - 1);
430+
maxWidth = Math.min(me.maxWidth, me.chart.width - maxLabelWidth);
431+
tickWidth = options.offset ? me.maxWidth / ticks.length : maxWidth / (ticks.length - 1);
431432

432433
// Allow 3 pixels x2 padding either side for label readability
433434
if (maxLabelWidth + 6 > tickWidth) {
435+
tickWidth = maxWidth / (ticks.length - (options.offset ? 0.5 : 1));
434436
maxHeight = me.maxHeight - getTickMarkLength(options.gridLines)
435437
- tickOpts.padding - getScaleLabelHeight(options.scaleLabel);
436438
maxLabelDiagonal = Math.sqrt(maxLabelWidth * maxLabelWidth + maxLabelHeight * maxLabelHeight);
@@ -529,8 +531,11 @@ module.exports = Element.extend({
529531
paddingLeft = firstLabelSize.width / 2;
530532
paddingRight = lastLabelSize.width / 2;
531533
}
532-
me.paddingLeft = Math.max(paddingLeft - offsetLeft, 0) + 3; // add 3 px to move away from canvas edges
533-
me.paddingRight = Math.max(paddingRight - offsetRight, 0) + 3;
534+
535+
// Adjust padding taking into account changes in offsets
536+
// and add 3 px to move away from canvas edges
537+
me.paddingLeft = Math.max((paddingLeft - offsetLeft) * me.width / (me.width - offsetLeft), 0) + 3;
538+
me.paddingRight = Math.max((paddingRight - offsetRight) * me.width / (me.width - offsetRight), 0) + 3;
534539
} else {
535540
// A vertical axis is more constrained by the width. Labels are the
536541
// dominant factor here, so get that length first and account for padding

0 commit comments

Comments
 (0)