Skip to content

Commit 790ce74

Browse files
committed
Improve rotation and padding calculation
1 parent 12bb851 commit 790ce74

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
@@ -406,7 +406,7 @@ module.exports = Element.extend({
406406
var options = me.options;
407407
var tickOpts = options.ticks;
408408
var ticks = me.getTicks();
409-
var labelSizes, maxLabelWidth, maxLabelHeight, tickWidth, maxHeight, maxLabelDiagonal;
409+
var labelSizes, maxLabelWidth, maxLabelHeight, maxWidth, tickWidth, maxHeight, maxLabelDiagonal;
410410

411411
var labelRotation = tickOpts.minRotation || 0;
412412

@@ -419,10 +419,12 @@ module.exports = Element.extend({
419419

420420
// Estimate the width of each grid based on the canvas width, the maximum
421421
// label width and the number of tick intervals
422-
tickWidth = (me.chart.width - maxLabelWidth) / (ticks.length - 1);
422+
maxWidth = Math.min(me.maxWidth, me.chart.width - maxLabelWidth);
423+
tickWidth = options.offset ? me.maxWidth / ticks.length : maxWidth / (ticks.length - 1);
423424

424425
// Allow 3 pixels x2 padding either side for label readability
425426
if (maxLabelWidth + 6 > tickWidth) {
427+
tickWidth = maxWidth / (ticks.length - (options.offset ? 0.5 : 1));
426428
maxHeight = me.maxHeight - getTickMarkLength(options.gridLines)
427429
- tickOpts.padding - getScaleLabelHeight(options.scaleLabel);
428430
maxLabelDiagonal = Math.sqrt(maxLabelWidth * maxLabelWidth + maxLabelHeight * maxLabelHeight);
@@ -521,8 +523,11 @@ module.exports = Element.extend({
521523
paddingLeft = firstLabelSize.width / 2;
522524
paddingRight = lastLabelSize.width / 2;
523525
}
524-
me.paddingLeft = Math.max(paddingLeft - offsetLeft, 0) + 3; // add 3 px to move away from canvas edges
525-
me.paddingRight = Math.max(paddingRight - offsetRight, 0) + 3;
526+
527+
// Adjust padding taking into account changes in offsets
528+
// and add 3 px to move away from canvas edges
529+
me.paddingLeft = Math.max((paddingLeft - offsetLeft) * me.width / (me.width - offsetLeft), 0) + 3;
530+
me.paddingRight = Math.max((paddingRight - offsetRight) * me.width / (me.width - offsetRight), 0) + 3;
526531
} else {
527532
// A vertical axis is more constrained by the width. Labels are the
528533
// dominant factor here, so get that length first and account for padding

0 commit comments

Comments
 (0)