From 92864b826ddc97c8290013376afa59dd4314e240 Mon Sep 17 00:00:00 2001 From: alexcjohnson Date: Thu, 16 Aug 2018 11:14:25 -0400 Subject: [PATCH] more robust limiting of date dtick to 100 microseconds --- src/plots/cartesian/axes.js | 12 +++++++++--- test/jasmine/tests/axes_test.js | 16 +++++++++++++++- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/plots/cartesian/axes.js b/src/plots/cartesian/axes.js index 30d693b50c8..d691d3f7911 100644 --- a/src/plots/cartesian/axes.js +++ b/src/plots/cartesian/axes.js @@ -503,6 +503,10 @@ axes.prepTicks = function(ax) { ax.tick0 = (ax.type === 'date') ? '2000-01-01' : 0; } + // ensure we don't try to make ticks below our minimum precision + // see https://github.com/plotly/plotly.js/issues/2892 + if(ax.type === 'date' && ax.dtick < 0.1) ax.dtick = 0.1; + // now figure out rounding of tick values autoTickRound(ax); }; @@ -692,9 +696,6 @@ axes.autoTicks = function(ax, roughDTick) { base = getBase(10); ax.dtick = roundDTick(roughDTick, base, roundBase10); } - // ensure we don't try to make ticks below our minimum precision - // see https://github.com/plotly/plotly.js/issues/2892 - if(ax.dtick < 0.1) ax.dtick = 0.1; } else if(ax.type === 'log') { ax.tick0 = 0; @@ -788,6 +789,11 @@ function autoTickRound(ax) { // of all possible ticks - so take the max. length of tick0 and the next one var tick1len = ax.l2r(tick0ms + dtick).replace(/^-/, '').length; ax._tickround = Math.max(tick0len, tick1len) - 20; + + // We shouldn't get here... but in case there's a situation I'm + // not thinking of where tick0str and tick1str are identical or + // something, fall back on maximum precision + if(ax._tickround < 0) ax._tickround = 4; } } else if(isNumeric(dtick) || dtick.charAt(0) === 'L') { diff --git a/test/jasmine/tests/axes_test.js b/test/jasmine/tests/axes_test.js index e87a9d40ae7..0ad2d656877 100644 --- a/test/jasmine/tests/axes_test.js +++ b/test/jasmine/tests/axes_test.js @@ -2057,7 +2057,7 @@ describe('Test axes', function() { expect(textOut).toEqual(expectedText); }); - it('never gives date dtick < 100 microseconds', function() { + it('never gives date dtick < 100 microseconds (autotick case)', function() { var ax = { type: 'date', tickmode: 'auto', @@ -2070,6 +2070,20 @@ describe('Test axes', function() { expect(textOut).toEqual(expectedText); }); + it('never gives date dtick < 100 microseconds (explicit tick case)', function() { + var ax = { + type: 'date', + tickmode: 'linear', + tick0: '2000-01-01', + dtick: 0.01, + range: ['2017-02-08 05:21:18.145', '2017-02-08 05:21:18.1451'] + }; + + var textOut = mockCalc(ax); + var expectedText = ['05:21:18.145
Feb 8, 2017', '05:21:18.1451']; + expect(textOut).toEqual(expectedText); + }); + it('should handle edge cases with dates and tickvals', function() { var ax = { type: 'date',