New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement scale label padding #4646
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,7 +36,14 @@ defaults._set('scale', { | |
// actual label | ||
labelString: '', | ||
|
||
lineHeight: 1.2 | ||
// line height | ||
lineHeight: 1.2, | ||
|
||
// top/bottom padding | ||
padding: { | ||
top: 4, | ||
bottom: 4 | ||
} | ||
}, | ||
|
||
// label settings | ||
|
@@ -391,7 +398,6 @@ module.exports = function(Chart) { | |
|
||
var tickFont = parseFontOptions(tickOpts); | ||
var tickMarkLength = opts.gridLines.tickMarkLength; | ||
var scaleLabelLineHeight = parseLineHeight(scaleLabelOpts); | ||
|
||
// Width | ||
if (isHorizontal) { | ||
|
@@ -410,10 +416,14 @@ module.exports = function(Chart) { | |
|
||
// Are we showing a title for the scale? | ||
if (scaleLabelOpts.display && display) { | ||
var scaleLabelLineHeight = parseLineHeight(scaleLabelOpts); | ||
var scaleLabelPadding = helpers.options.toPadding(scaleLabelOpts.padding); | ||
var deltaHeight = scaleLabelLineHeight + scaleLabelPadding.height; | ||
|
||
if (isHorizontal) { | ||
minSize.height += scaleLabelLineHeight; | ||
minSize.height += deltaHeight; | ||
} else { | ||
minSize.width += scaleLabelLineHeight; | ||
minSize.width += deltaHeight; | ||
} | ||
} | ||
|
||
|
@@ -435,16 +445,17 @@ module.exports = function(Chart) { | |
// TODO - improve this calculation | ||
var labelHeight = (sinRotation * largestTextWidth) | ||
+ (tickFont.size * tallestLabelHeightInLines) | ||
+ (lineSpace * tallestLabelHeightInLines); | ||
+ (lineSpace * (tallestLabelHeightInLines - 1)) | ||
+ lineSpace; // padding | ||
|
||
minSize.height = Math.min(me.maxHeight, minSize.height + labelHeight + tickPadding); | ||
me.ctx.font = tickFont.font; | ||
|
||
me.ctx.font = tickFont.font; | ||
var firstLabelWidth = computeTextSize(me.ctx, labels[0], tickFont.font); | ||
var lastLabelWidth = computeTextSize(me.ctx, labels[labels.length - 1], tickFont.font); | ||
|
||
// Ensure that our ticks are always inside the canvas. When rotated, ticks are right aligned which means that the right padding is dominated | ||
// by the font height | ||
// Ensure that our ticks are always inside the canvas. When rotated, ticks are right aligned | ||
// which means that the right padding is dominated by the font height | ||
if (me.labelRotation !== 0) { | ||
me.paddingLeft = opts.position === 'bottom' ? (cosRotation * firstLabelWidth) + 3 : (cosRotation * lineSpace) + 3; // add 3 px to move away from canvas edges | ||
me.paddingRight = opts.position === 'bottom' ? (cosRotation * lineSpace) + 3 : (cosRotation * lastLabelWidth) + 3; | ||
|
@@ -453,15 +464,18 @@ module.exports = function(Chart) { | |
me.paddingRight = lastLabelWidth / 2 + 3; | ||
} | ||
} else { | ||
// A vertical axis is more constrained by the width. Labels are the dominant factor here, so get that length first | ||
// Account for padding | ||
|
||
// A vertical axis is more constrained by the width. Labels are the | ||
// dominant factor here, so get that length first and account for padding | ||
if (tickOpts.mirror) { | ||
largestTextWidth = 0; | ||
} else { | ||
largestTextWidth += tickPadding; | ||
// use lineSpace for consistency with horizontal axis | ||
// tickPadding is not implemented for horizontal | ||
largestTextWidth += tickPadding + lineSpace; | ||
} | ||
|
||
minSize.width = Math.min(me.maxWidth, minSize.width + largestTextWidth); | ||
|
||
me.paddingTop = tickFont.size / 2; | ||
me.paddingBottom = tickFont.size / 2; | ||
} | ||
|
@@ -663,6 +677,7 @@ module.exports = function(Chart) { | |
|
||
var scaleLabelFontColor = helpers.valueOrDefault(scaleLabel.fontColor, globalDefaults.defaultFontColor); | ||
var scaleLabelFont = parseFontOptions(scaleLabel); | ||
var scaleLabelPadding = helpers.options.toPadding(scaleLabel.padding); | ||
var labelRotationRadians = helpers.toRadians(me.labelRotation); | ||
|
||
var itemsToDraw = []; | ||
|
@@ -840,10 +855,14 @@ module.exports = function(Chart) { | |
|
||
if (isHorizontal) { | ||
scaleLabelX = me.left + ((me.right - me.left) / 2); // midpoint of the width | ||
scaleLabelY = options.position === 'bottom' ? me.bottom - halfLineHeight : me.top + halfLineHeight; | ||
scaleLabelY = options.position === 'bottom' | ||
? me.bottom - halfLineHeight - scaleLabelPadding.bottom | ||
: me.top + halfLineHeight + scaleLabelPadding.top; | ||
} else { | ||
var isLeft = options.position === 'left'; | ||
scaleLabelX = isLeft ? me.left + halfLineHeight : me.right - halfLineHeight; | ||
scaleLabelX = isLeft | ||
? me.left + halfLineHeight + scaleLabelPadding.top | ||
: me.right - halfLineHeight - scaleLabelPadding.top; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nit] would put There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can do but it looks kinda asymetric and hard to grasp:
Is this really desired? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fair enough, that's not better. Lets leave it as is |
||
scaleLabelY = me.top + ((me.bottom - me.top) / 2); | ||
rotation = isLeft ? -0.5 * Math.PI : 0.5 * Math.PI; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,22 @@ | ||
describe('Core.scale', function() { | ||
describe('auto', jasmine.specsFromFixtures('core.scale')); | ||
|
||
it('should provide default scale label options', function() { | ||
expect(Chart.defaults.scale.scaleLabel).toEqual({ | ||
// display property | ||
display: false, | ||
|
||
// actual label | ||
labelString: '', | ||
|
||
// actual label | ||
lineHeight: 1.2, | ||
|
||
// top/bottom padding | ||
padding: { | ||
top: 4, | ||
bottom: 4 | ||
} | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the reason for this change? It looks equivalent to the old code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is actually equivalent but prepares for implementing tick label padding.
lineSpace
is only needed lines-1 times. The lastlineSpace
is really outer padding and could indeed be any other value.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gotcha