From 4f9e1fbf211aa1ff8aec3cc8f11db3b1cc9e7730 Mon Sep 17 00:00:00 2001 From: Akihiko Kusanagi Date: Fri, 11 Jan 2019 22:57:02 +0800 Subject: [PATCH] Improve rotation and padding calculation --- src/core/core.scale.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/core/core.scale.js b/src/core/core.scale.js index f4d273d41e5..1ac815adeb5 100644 --- a/src/core/core.scale.js +++ b/src/core/core.scale.js @@ -414,7 +414,7 @@ module.exports = Element.extend({ var options = me.options; var tickOpts = options.ticks; var ticks = me.getTicks(); - var labelSizes, maxLabelWidth, maxLabelHeight, tickWidth, maxHeight, maxLabelDiagonal; + var labelSizes, maxLabelWidth, maxLabelHeight, maxWidth, tickWidth, maxHeight, maxLabelDiagonal; var labelRotation = tickOpts.minRotation || 0; @@ -427,10 +427,12 @@ module.exports = Element.extend({ // Estimate the width of each grid based on the canvas width, the maximum // label width and the number of tick intervals - tickWidth = (me.chart.width - maxLabelWidth) / (ticks.length - 1); + maxWidth = Math.min(me.maxWidth, me.chart.width - maxLabelWidth); + tickWidth = options.offset ? me.maxWidth / ticks.length : maxWidth / (ticks.length - 1); // Allow 3 pixels x2 padding either side for label readability if (maxLabelWidth + 6 > tickWidth) { + tickWidth = maxWidth / (ticks.length - (options.offset ? 0.5 : 1)); maxHeight = me.maxHeight - getTickMarkLength(options.gridLines) - tickOpts.padding - getScaleLabelHeight(options.scaleLabel); maxLabelDiagonal = Math.sqrt(maxLabelWidth * maxLabelWidth + maxLabelHeight * maxLabelHeight); @@ -529,8 +531,11 @@ module.exports = Element.extend({ paddingLeft = firstLabelSize.width / 2; paddingRight = lastLabelSize.width / 2; } - me.paddingLeft = Math.max(paddingLeft - offsetLeft, 0) + 3; // add 3 px to move away from canvas edges - me.paddingRight = Math.max(paddingRight - offsetRight, 0) + 3; + + // Adjust padding taking into account changes in offsets + // and add 3 px to move away from canvas edges + me.paddingLeft = Math.max((paddingLeft - offsetLeft) * me.width / (me.width - offsetLeft), 0) + 3; + me.paddingRight = Math.max((paddingRight - offsetRight) * me.width / (me.width - offsetRight), 0) + 3; } else { // A vertical axis is more constrained by the width. Labels are the // dominant factor here, so get that length first and account for padding