From 71aac6968165c874dd918ca0ec3c23ae6071ddc9 Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Sun, 20 Oct 2019 20:04:37 -0700 Subject: [PATCH] Fix unit determination when autoSkip is enabled --- src/scales/scale.time.js | 19 ++++++++++++------- test/specs/scale.time.tests.js | 8 ++++---- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/scales/scale.time.js b/src/scales/scale.time.js index 38428c8ffd7..83bf4e496e5 100644 --- a/src/scales/scale.time.js +++ b/src/scales/scale.time.js @@ -257,7 +257,7 @@ function determineUnitForAutoTicks(minUnit, min, max, capacity) { for (i = UNITS.indexOf(minUnit); i < ilen - 1; ++i) { interval = INTERVALS[UNITS[i]]; - factor = interval.steps ? interval.steps / 2 : MAX_INTEGER; + factor = interval.steps ? interval.steps : MAX_INTEGER; if (interval.common && Math.ceil((max - min) / (factor * interval.size)) <= capacity) { return UNITS[i]; @@ -270,12 +270,12 @@ function determineUnitForAutoTicks(minUnit, min, max, capacity) { /** * Figures out what unit to format a set of ticks with */ -function determineUnitForFormatting(scale, ticks, minUnit, min, max) { +function determineUnitForFormatting(scale, numTicks, minUnit, min, max) { var i, unit; for (i = UNITS.length - 1; i >= UNITS.indexOf(minUnit); i--) { unit = UNITS[i]; - if (INTERVALS[unit].common && scale._adapter.diff(max, min, unit) >= ticks.length - 1) { + if (INTERVALS[unit].common && scale._adapter.diff(max, min, unit) >= numTicks - 1) { return unit; } } @@ -562,11 +562,12 @@ module.exports = Scale.extend({ var min = me.min; var max = me.max; var options = me.options; + var tickOpts = options.ticks; var timeOpts = options.time; var timestamps = me._timestamps; var ticks = []; var capacity = me.getLabelCapacity(min); - var source = options.ticks.source; + var source = tickOpts.source; var distribution = options.distribution; var i, ilen, timestamp; @@ -599,13 +600,17 @@ module.exports = Scale.extend({ me.max = max; // PRIVATE - me._unit = timeOpts.unit || determineUnitForFormatting(me, ticks, timeOpts.minUnit, me.min, me.max); - me._majorUnit = !options.ticks.major.enabled || me._unit === 'year' ? undefined + // determineUnitForFormatting relies on the number of ticks so we don't use it when + // autoSkip is enabled because we don't yet know what the final number of ticks will be + me._unit = timeOpts.unit || (tickOpts.autoSkip + ? determineUnitForAutoTicks(timeOpts.minUnit, me.min, me.max, capacity) + : determineUnitForFormatting(me, ticks.length, timeOpts.minUnit, me.min, me.max)); + me._majorUnit = !tickOpts.major.enabled || me._unit === 'year' ? undefined : determineMajorUnit(me._unit); me._table = buildLookupTable(me._timestamps.data, min, max, distribution); me._offsets = computeOffsets(me._table, ticks, min, max, options); - if (options.ticks.reverse) { + if (tickOpts.reverse) { ticks.reverse(); } diff --git a/test/specs/scale.time.tests.js b/test/specs/scale.time.tests.js index 593a846b0f9..71967491d4c 100755 --- a/test/specs/scale.time.tests.js +++ b/test/specs/scale.time.tests.js @@ -128,7 +128,7 @@ describe('Time scale tests', function() { var ticks = getTicksLabels(scale); // `bounds === 'data'`: first and last ticks removed since outside the data range - expect(ticks).toEqual(['Jan 2', 'Jan 3', 'Jan 4', 'Jan 5', 'Jan 6', 'Jan 7', 'Jan 8', 'Jan 9', 'Jan 10']); + expect(ticks.length).toEqual(217); }); it('should accept labels as date objects', function() { @@ -139,7 +139,7 @@ describe('Time scale tests', function() { var ticks = getTicksLabels(scale); // `bounds === 'data'`: first and last ticks removed since outside the data range - expect(ticks).toEqual(['Jan 2', 'Jan 3', 'Jan 4', 'Jan 5', 'Jan 6', 'Jan 7', 'Jan 8', 'Jan 9', 'Jan 10']); + expect(ticks.length).toEqual(217); }); it('should accept data as xy points', function() { @@ -187,7 +187,7 @@ describe('Time scale tests', function() { var ticks = getTicksLabels(xScale); // `bounds === 'data'`: first and last ticks removed since outside the data range - expect(ticks).toEqual(['Jan 2', 'Jan 3', 'Jan 4', 'Jan 5', 'Jan 6', 'Jan 7', 'Jan 8', 'Jan 9', 'Jan 10']); + expect(ticks.length).toEqual(217); }); it('should accept data as ty points', function() { @@ -235,7 +235,7 @@ describe('Time scale tests', function() { var ticks = getTicksLabels(tScale); // `bounds === 'data'`: first and last ticks removed since outside the data range - expect(ticks).toEqual(['Jan 2', 'Jan 3', 'Jan 4', 'Jan 5', 'Jan 6', 'Jan 7', 'Jan 8', 'Jan 9', 'Jan 10']); + expect(ticks.length).toEqual(217); }); });