Skip to content

Commit

Permalink
Merge pull request #2912 from plotly/date-precision-bug
Browse files Browse the repository at this point in the history
fix #2892 - don't allow auto date ticks < our 100 microseconds limit
  • Loading branch information
alexcjohnson authored Aug 16, 2018
2 parents 5bd5783 + 92864b8 commit 24a0f91
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
9 changes: 9 additions & 0 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 @@ -785,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
27 changes: 27 additions & 0 deletions test/jasmine/tests/axes_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2057,6 +2057,33 @@ describe('Test axes', function() {
expect(textOut).toEqual(expectedText);
});

it('never gives date dtick < 100 microseconds (autotick case)', function() {
var ax = {
type: 'date',
tickmode: 'auto',
nticks: '100',
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('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 24a0f91

Please sign in to comment.