Skip to content

Commit b7121c3

Browse files
committed
fix #1058 - make sure no date ever returns sub-100microsec precision
1 parent 55b313c commit b7121c3

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

src/plots/cartesian/axes.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -850,7 +850,8 @@ function autoTickRound(ax) {
850850
// not necessarily *all* the information in tick0 though, if it's really odd
851851
// minimal string length for tick0: 'd' is 10, 'M' is 16, 'S' is 19
852852
// take off a leading minus (year < 0 so length is consistent)
853-
var tick0str = Lib.ms2DateTime(Lib.dateTime2ms(ax.tick0)).replace(/^-/, ''),
853+
var tick0ms = Lib.dateTime2ms(ax.tick0),
854+
tick0str = Lib.ms2DateTime(tick0ms).replace(/^-/, ''),
854855
tick0len = tick0str.length;
855856

856857
if(String(dtick).charAt(0) === 'M') {
@@ -862,7 +863,12 @@ function autoTickRound(ax) {
862863
else if((dtick >= ONEDAY && tick0len <= 10) || (dtick >= ONEDAY * 15)) ax._tickround = 'd';
863864
else if((dtick >= ONEMIN && tick0len <= 16) || (dtick >= ONEHOUR)) ax._tickround = 'M';
864865
else if((dtick >= ONESEC && tick0len <= 19) || (dtick >= ONEMIN)) ax._tickround = 'S';
865-
else ax._tickround = Math.max(3 - Math.round(Math.log(dtick / 2) / Math.LN10), tick0len - 20);
866+
else {
867+
// of any two adjacent ticks, at least one will have the maximum fractional digits
868+
// of all possible ticks - so take the max. length of tick0 and the next one
869+
var tick1len = Lib.ms2DateTime(tick0ms + dtick).replace(/^-/, '').length;
870+
ax._tickround = Math.max(tick0len, tick1len) - 20;
871+
}
866872
}
867873
else if(isNumeric(dtick) || dtick.charAt(0) === 'L') {
868874
// linear or log (except D1, D2)
@@ -1112,7 +1118,7 @@ function formatDate(ax, out, hover, extraPrecision) {
11121118
if(tr !== 'M') {
11131119
tt += secondFormat(d);
11141120
if(tr !== 'S') {
1115-
tt += numFormat(mod(x / 1000, 1), ax, 'none', hover)
1121+
tt += numFormat(d3.round(mod(x / 1000, 1), 4), ax, 'none', hover)
11161122
.substr(1);
11171123
}
11181124
}

test/jasmine/tests/axes_test.js

+24
Original file line numberDiff line numberDiff line change
@@ -1397,6 +1397,7 @@ describe('Test axes', function() {
13971397
function mockCalc(ax) {
13981398
Axes.setConvert(ax);
13991399
ax.tickfont = {};
1400+
ax._gd = {_fullLayout: {separators: '.,'}};
14001401
return Axes.calcTicks(ax).map(function(v) { return v.text; });
14011402
}
14021403

@@ -1475,5 +1476,28 @@ describe('Test axes', function() {
14751476
];
14761477
expect(textOut).toEqual(expectedText);
14771478
});
1479+
1480+
it('should never give dates more than 100 microsecond precision', function() {
1481+
var textOut = mockCalc({
1482+
type: 'date',
1483+
tickmode: 'linear',
1484+
tick0: '2000-01-01',
1485+
dtick: 1.1333,
1486+
range: ['2000-01-01', '2000-01-01 00:00:00.01']
1487+
});
1488+
1489+
var expectedText = [
1490+
'00:00:00<br>Jan 1, 2000',
1491+
'00:00:00.0011',
1492+
'00:00:00.0023',
1493+
'00:00:00.0034',
1494+
'00:00:00.0045',
1495+
'00:00:00.0057',
1496+
'00:00:00.0068',
1497+
'00:00:00.0079',
1498+
'00:00:00.0091'
1499+
];
1500+
expect(textOut).toEqual(expectedText);
1501+
});
14781502
});
14791503
});

0 commit comments

Comments
 (0)