From 1686ce0aa7e09249165732f5a35bee60393064d7 Mon Sep 17 00:00:00 2001 From: Jukka Kurkela Date: Sun, 12 May 2019 17:52:30 +0300 Subject: [PATCH] Apply offset regardless of min/max time scale options (#6264) --- src/scales/scale.time.js | 24 ++++++++++-------------- test/specs/scale.time.tests.js | 9 ++++++--- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/scales/scale.time.js b/src/scales/scale.time.js index 7d21f47b53d..849dd8dcc6e 100644 --- a/src/scales/scale.time.js +++ b/src/scales/scale.time.js @@ -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; } } diff --git a/test/specs/scale.time.tests.js b/test/specs/scale.time.tests.js index 5f80dd7b551..80f127da527 100755 --- a/test/specs/scale.time.tests.js +++ b/test/specs/scale.time.tests.js @@ -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]; @@ -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); }); }); });