Skip to content
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

Apply offset regardless of min/max setting #6264

Merged
merged 1 commit into from May 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -1421,7 +1421,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 @@ -1431,8 +1431,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