Skip to content

Commit

Permalink
more robust limiting of date dtick to 100 microseconds
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcjohnson committed Aug 16, 2018
1 parent c62896b commit 92864b8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
12 changes: 9 additions & 3 deletions src/plots/cartesian/axes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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') {
Expand Down
16 changes: 15 additions & 1 deletion test/jasmine/tests/axes_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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<br>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',
Expand Down

0 comments on commit 92864b8

Please sign in to comment.