Skip to content

Commit 7c45fda

Browse files
nagixsimonbrunel
authored andcommitted
Remove gaps on the left and right when the axis offset is true (#5884)
1 parent bfa635e commit 7c45fda

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

src/core/core.scale.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,7 @@ module.exports = Element.extend({
397397
var scaleLabelOpts = opts.scaleLabel;
398398
var gridLineOpts = opts.gridLines;
399399
var display = opts.display;
400+
var position = opts.position;
400401
var isHorizontal = me.isHorizontal();
401402

402403
var tickFont = parseFontOptions(tickOpts);
@@ -456,16 +457,21 @@ module.exports = Element.extend({
456457
me.ctx.font = tickFont.font;
457458
var firstLabelWidth = computeTextSize(me.ctx, labels[0], tickFont.font);
458459
var lastLabelWidth = computeTextSize(me.ctx, labels[labels.length - 1], tickFont.font);
460+
var offsetLeft = me.getPixelForTick(0) - me.left;
461+
var offsetRight = me.right - me.getPixelForTick(labels.length - 1);
462+
var paddingLeft, paddingRight;
459463

460464
// Ensure that our ticks are always inside the canvas. When rotated, ticks are right aligned
461465
// which means that the right padding is dominated by the font height
462466
if (me.labelRotation !== 0) {
463-
me.paddingLeft = opts.position === 'bottom' ? (cosRotation * firstLabelWidth) + 3 : (cosRotation * lineSpace) + 3; // add 3 px to move away from canvas edges
464-
me.paddingRight = opts.position === 'bottom' ? (cosRotation * lineSpace) + 3 : (cosRotation * lastLabelWidth) + 3;
467+
paddingLeft = position === 'bottom' ? (cosRotation * firstLabelWidth) : (cosRotation * lineSpace);
468+
paddingRight = position === 'bottom' ? (cosRotation * lineSpace) : (cosRotation * lastLabelWidth);
465469
} else {
466-
me.paddingLeft = firstLabelWidth / 2 + 3; // add 3 px to move away from canvas edges
467-
me.paddingRight = lastLabelWidth / 2 + 3;
470+
paddingLeft = firstLabelWidth / 2;
471+
paddingRight = lastLabelWidth / 2;
468472
}
473+
me.paddingLeft = Math.max(paddingLeft - offsetLeft, 0) + 3; // add 3 px to move away from canvas edges
474+
me.paddingRight = Math.max(paddingRight - offsetRight, 0) + 3;
469475
} else {
470476
// A vertical axis is more constrained by the width. Labels are the
471477
// dominant factor here, so get that length first and account for padding

test/specs/scale.category.tests.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,11 @@ describe('Category scale tests', function() {
220220
xScale.options.offset = true;
221221
chart.update();
222222

223-
expect(xScale.getPixelForValue(0, 0, 0)).toBeCloseToPixel(69 + 6); // plus lineHeight
223+
expect(xScale.getPixelForValue(0, 0, 0)).toBeCloseToPixel(71 + 6); // plus lineHeight
224224
expect(xScale.getValueForPixel(69)).toBe(0);
225225

226-
expect(xScale.getPixelForValue(0, 4, 0)).toBeCloseToPixel(441);
227-
expect(xScale.getValueForPixel(397)).toBe(4);
226+
expect(xScale.getPixelForValue(0, 4, 0)).toBeCloseToPixel(461);
227+
expect(xScale.getValueForPixel(417)).toBe(4);
228228
});
229229

230230
it ('Should get the correct pixel for a value when there are repeated labels', function() {
@@ -295,8 +295,8 @@ describe('Category scale tests', function() {
295295
xScale.options.offset = true;
296296
chart.update();
297297

298-
expect(xScale.getPixelForValue(0, 1, 0)).toBeCloseToPixel(102 + 6); // plus lineHeight
299-
expect(xScale.getPixelForValue(0, 3, 0)).toBeCloseToPixel(417);
298+
expect(xScale.getPixelForValue(0, 1, 0)).toBeCloseToPixel(103 + 6); // plus lineHeight
299+
expect(xScale.getPixelForValue(0, 3, 0)).toBeCloseToPixel(429);
300300
});
301301

302302
it ('should get the correct pixel for a value when vertical', function() {

0 commit comments

Comments
 (0)