Skip to content

Commit

Permalink
Improve rotation and padding calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
nagix committed Jan 11, 2019
1 parent db3c08e commit a55b265
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/core/core.scale.js
Expand Up @@ -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;

Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a55b265

Please sign in to comment.