Skip to content

Commit

Permalink
Apply offset regardless of min/max time scale options (#6264)
Browse files Browse the repository at this point in the history
  • Loading branch information
kurkle authored and simonbrunel committed May 12, 2019
1 parent 89f2e04 commit 1686ce0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
24 changes: 10 additions & 14 deletions src/scales/scale.time.js
Expand Up @@ -387,21 +387,17 @@ function computeOffsets(table, ticks, min, max, options) {
var first, last;

if (options.offset && ticks.length) {
if (!getMin(options)) {
first = interpolate(table, 'time', ticks[0], 'pos');
if (ticks.length === 1) {
start = 1 - first;
} else {
start = (interpolate(table, 'time', ticks[1], 'pos') - first) / 2;
}
first = interpolate(table, 'time', ticks[0], 'pos');
if (ticks.length === 1) {
start = 1 - first;
} else {
start = (interpolate(table, 'time', ticks[1], 'pos') - first) / 2;
}
if (!getMax(options)) {
last = interpolate(table, 'time', ticks[ticks.length - 1], 'pos');
if (ticks.length === 1) {
end = last;
} else {
end = (last - interpolate(table, 'time', ticks[ticks.length - 2], 'pos')) / 2;
}
last = interpolate(table, 'time', ticks[ticks.length - 1], 'pos');
if (ticks.length === 1) {
end = last;
} else {
end = (last - interpolate(table, 'time', ticks[ticks.length - 2], 'pos')) / 2;
}
}

Expand Down
9 changes: 6 additions & 3 deletions test/specs/scale.time.tests.js
Expand Up @@ -1456,7 +1456,7 @@ describe('Time scale tests', function() {
expect(scale.getPixelForValue('2051')).toBeCloseToPixel(scale.left + scale.width);
});

it ('should not add offset if min and max extend the labels range and offset is true', function() {
it ('should add offset if min and max extend the labels range and offset is true', function() {
var chart = this.chart;
var scale = chart.scales.x;
var options = chart.options.scales.xAxes[0];
Expand All @@ -1466,8 +1466,11 @@ describe('Time scale tests', function() {
options.offset = true;
chart.update();

expect(scale.getPixelForValue('2012')).toBeCloseToPixel(scale.left);
expect(scale.getPixelForValue('2051')).toBeCloseToPixel(scale.left + scale.width);
var numTicks = scale.ticks.length;
var firstTickInterval = scale.getPixelForTick(1) - scale.getPixelForTick(0);
var lastTickInterval = scale.getPixelForTick(numTicks - 1) - scale.getPixelForTick(numTicks - 2);
expect(scale.getPixelForValue('2012')).toBeCloseToPixel(scale.left + firstTickInterval / 2);
expect(scale.getPixelForValue('2051')).toBeCloseToPixel(scale.left + scale.width - lastTickInterval / 2);
});
});
});
Expand Down

0 comments on commit 1686ce0

Please sign in to comment.