Skip to content

Commit 6762130

Browse files
committed
remove old (buggy) hovertext cruft, clean up and test edge cases
1 parent 77f745e commit 6762130

File tree

2 files changed

+152
-27
lines changed

2 files changed

+152
-27
lines changed

src/plots/cartesian/axes.js

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -872,7 +872,7 @@ function autoTickRound(ax) {
872872
// not necessarily *all* the information in tick0 though, if it's really odd
873873
// minimal string length for tick0: 'd' is 10, 'M' is 16, 'S' is 19
874874
// take off a leading minus (year < 0 so length is consistent)
875-
var tick0ms = Lib.dateTime2ms(ax.tick0) || 0,
875+
var tick0ms = Lib.dateTime2ms(ax.tick0),
876876
tick0str = Lib.ms2DateTime(tick0ms).replace(/^-/, ''),
877877
tick0len = tick0str.length;
878878

@@ -1106,7 +1106,6 @@ function tickTextObj(ax, x, text) {
11061106
function formatDate(ax, out, hover, extraPrecision) {
11071107
var x = out.x,
11081108
tr = ax._tickround,
1109-
trOriginal = tr,
11101109
d = new Date(x),
11111110
// suffix completes the full date info, to be included
11121111
// with only the first tick or if any info before what's
@@ -1145,17 +1144,13 @@ function formatDate(ax, out, hover, extraPrecision) {
11451144
.substr(1);
11461145
}
11471146
}
1148-
else if(trOriginal === 'd') {
1149-
// for hover on axes with day ticks, minuteFormat (which
1150-
// only includes %H:%M) isn't enough, you want the date too
1151-
tt = dayFormat(d) + ' ' + tt;
1152-
}
11531147
}
11541148
}
11551149
}
1156-
if(ax.tickmode === 'array') {
1157-
// we get extra precision in array mode, but it may be useless, strip it off
1158-
if(tt === '00:00:00') {
1150+
if(hover || ax.tickmode === 'array') {
1151+
// we get extra precision in array mode or hover,
1152+
// but it may be useless, strip it off
1153+
if(tt === '00:00:00' || tt === '00:00') {
11591154
tt = suffix;
11601155
suffix = '';
11611156
}
@@ -1169,7 +1164,9 @@ function formatDate(ax, out, hover, extraPrecision) {
11691164
if(suffix) {
11701165
if(hover) {
11711166
// hover puts it all on one line, so suffix works best up front
1172-
tt = suffix + (tt ? ' ' + tt : '');
1167+
// except for year suffix: turn this into "Jan 1, 2000" etc.
1168+
if(tr === 'd') tt += ', ' + suffix;
1169+
else tt = suffix + (tt ? ', ' + tt : '');
11731170
}
11741171
else if(!ax._inCalcTicks || (suffix !== ax._prevSuffix)) {
11751172
tt += '<br>' + suffix;

test/jasmine/tests/axes_test.js

Lines changed: 144 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1393,22 +1393,36 @@ describe('Test axes', function() {
13931393
});
13941394
});
13951395

1396-
describe('calcTicks', function() {
1396+
describe('calcTicks and tickText', function() {
13971397
function mockCalc(ax) {
13981398
Axes.setConvert(ax);
13991399
ax.tickfont = {};
14001400
ax._gd = {_fullLayout: {separators: '.,'}};
14011401
return Axes.calcTicks(ax).map(function(v) { return v.text; });
14021402
}
14031403

1404+
function mockHoverText(ax, x) {
1405+
var xCalc = (ax.d2l_noadd || ax.d2c)(x);
1406+
var tickTextObj = Axes.tickText(ax, xCalc, true);
1407+
return tickTextObj.text;
1408+
}
1409+
1410+
function checkHovers(ax, specArray) {
1411+
specArray.forEach(function(v) {
1412+
expect(mockHoverText(ax, v[0]))
1413+
.toBe(v[1], ax.dtick + ' - ' + v[0]);
1414+
});
1415+
}
1416+
14041417
it('provides a new date suffix whenever the suffix changes', function() {
1405-
var textOut = mockCalc({
1418+
var ax = {
14061419
type: 'date',
14071420
tickmode: 'linear',
14081421
tick0: '2000-01-01',
14091422
dtick: 14 * 24 * 3600 * 1000, // 14 days
14101423
range: ['1999-12-01', '2000-02-15']
1411-
});
1424+
};
1425+
var textOut = mockCalc(ax);
14121426

14131427
var expectedText = [
14141428
'Dec 4<br>1999',
@@ -1419,14 +1433,17 @@ describe('Test axes', function() {
14191433
'Feb 12'
14201434
];
14211435
expect(textOut).toEqual(expectedText);
1436+
expect(mockHoverText(ax, ax.d2c('1999-12-18 15:34:33.3')))
1437+
.toBe('Dec 18, 1999, 15:34');
14221438

1423-
textOut = mockCalc({
1439+
ax = {
14241440
type: 'date',
14251441
tickmode: 'linear',
14261442
tick0: '2000-01-01',
14271443
dtick: 12 * 3600 * 1000, // 12 hours
14281444
range: ['2000-01-03 11:00', '2000-01-06']
1429-
});
1445+
};
1446+
textOut = mockCalc(ax);
14301447

14311448
expectedText = [
14321449
'12:00<br>Jan 3, 2000',
@@ -1437,14 +1454,17 @@ describe('Test axes', function() {
14371454
'00:00<br>Jan 6, 2000'
14381455
];
14391456
expect(textOut).toEqual(expectedText);
1457+
expect(mockHoverText(ax, ax.d2c('2000-01-04 15:34:33.3')))
1458+
.toBe('Jan 4, 2000, 15:34:33');
14401459

1441-
textOut = mockCalc({
1460+
ax = {
14421461
type: 'date',
14431462
tickmode: 'linear',
14441463
tick0: '2000-01-01',
14451464
dtick: 1000, // 1 sec
14461465
range: ['2000-02-03 23:59:57', '2000-02-04 00:00:02']
1447-
});
1466+
};
1467+
textOut = mockCalc(ax);
14481468

14491469
expectedText = [
14501470
'23:59:57<br>Feb 3, 2000',
@@ -1455,16 +1475,21 @@ describe('Test axes', function() {
14551475
'00:00:02'
14561476
];
14571477
expect(textOut).toEqual(expectedText);
1478+
expect(mockHoverText(ax, ax.d2c('2000-02-04 00:00:00.123456')))
1479+
.toBe('Feb 4, 2000, 00:00:00.1235');
1480+
expect(mockHoverText(ax, ax.d2c('2000-02-04 00:00:00')))
1481+
.toBe('Feb 4, 2000');
14581482
});
14591483

14601484
it('should give dates extra precision if tick0 is weird', function() {
1461-
var textOut = mockCalc({
1485+
var ax = {
14621486
type: 'date',
14631487
tickmode: 'linear',
14641488
tick0: '2000-01-01 00:05',
14651489
dtick: 14 * 24 * 3600 * 1000, // 14 days
14661490
range: ['1999-12-01', '2000-02-15']
1467-
});
1491+
};
1492+
var textOut = mockCalc(ax);
14681493

14691494
var expectedText = [
14701495
'00:05<br>Dec 4, 1999',
@@ -1475,16 +1500,21 @@ describe('Test axes', function() {
14751500
'00:05<br>Feb 12, 2000'
14761501
];
14771502
expect(textOut).toEqual(expectedText);
1503+
expect(mockHoverText(ax, ax.d2c('2000-02-04 00:00:00.123456')))
1504+
.toBe('Feb 4, 2000');
1505+
expect(mockHoverText(ax, ax.d2c('2000-02-04 00:00:05.123456')))
1506+
.toBe('Feb 4, 2000, 00:00:05');
14781507
});
14791508

14801509
it('should never give dates more than 100 microsecond precision', function() {
1481-
var textOut = mockCalc({
1510+
var ax = {
14821511
type: 'date',
14831512
tickmode: 'linear',
14841513
tick0: '2000-01-01',
14851514
dtick: 1.1333,
14861515
range: ['2000-01-01', '2000-01-01 00:00:00.01']
1487-
});
1516+
};
1517+
var textOut = mockCalc(ax);
14881518

14891519
var expectedText = [
14901520
'00:00:00<br>Jan 1, 2000',
@@ -1501,7 +1531,7 @@ describe('Test axes', function() {
15011531
});
15021532

15031533
it('should handle edge cases with dates and tickvals', function() {
1504-
var textOut = mockCalc({
1534+
var ax = {
15051535
type: 'date',
15061536
tickmode: 'array',
15071537
tickvals: [
@@ -1518,7 +1548,8 @@ describe('Test axes', function() {
15181548
// required to get calcTicks to run
15191549
range: ['2011-12-10', '2013-01-23'],
15201550
nticks: 10
1521-
});
1551+
};
1552+
var textOut = mockCalc(ax);
15221553

15231554
var expectedText = [
15241555
'New year',
@@ -1528,19 +1559,24 @@ describe('Test axes', function() {
15281559
'00:00:01<br>Jan 1, 2013'
15291560
];
15301561
expect(textOut).toEqual(expectedText);
1562+
expect(mockHoverText(ax, ax.d2c('2012-01-01')))
1563+
.toBe('New year');
1564+
expect(mockHoverText(ax, ax.d2c('2012-01-01 12:34:56.1234')))
1565+
.toBe('Jan 1, 2012, 12:34:56');
15311566
});
15321567

15331568
it('should handle tickvals edge cases with linear and log axes', function() {
15341569
['linear', 'log'].forEach(function(axType) {
1535-
var textOut = mockCalc({
1570+
var ax = {
15361571
type: axType,
15371572
tickmode: 'array',
15381573
tickvals: [1, 1.5, 2.6999999, 30, 39.999, 100, 0.1],
15391574
ticktext: ['One', '...and a half'],
15401575
// I'll be so happy when I can finally get rid of this switch!
15411576
range: axType === 'log' ? [-0.2, 1.8] : [0.5, 50],
15421577
nticks: 10
1543-
});
1578+
};
1579+
var textOut = mockCalc(ax);
15441580

15451581
var expectedText = [
15461582
'One',
@@ -1551,6 +1587,8 @@ describe('Test axes', function() {
15511587
// 10 and 0.1 are off scale
15521588
];
15531589
expect(textOut).toEqual(expectedText, axType);
1590+
expect(mockHoverText(ax, ax.c2l(1))).toBe('One');
1591+
expect(mockHoverText(ax, ax.c2l(19.999))).toBe('19.999');
15541592
});
15551593
});
15561594

@@ -1578,8 +1616,98 @@ describe('Test axes', function() {
15781616
// 'e', 5, -2: bad category and numbers out of range: omitted
15791617
];
15801618
expect(textOut).toEqual(expectedText);
1581-
// make sure we didn't add any more axes accidentally
1619+
expect(mockHoverText(ax, 0)).toBe('A!');
1620+
expect(mockHoverText(ax, 2)).toBe('c');
1621+
expect(mockHoverText(ax, 4)).toBe('');
1622+
1623+
// make sure we didn't add any more categories accidentally
15821624
expect(ax._categories).toEqual(['a', 'b', 'c', 'd']);
15831625
});
1626+
1627+
it('should always start at year for date axis hover', function() {
1628+
var ax = {
1629+
type: 'date',
1630+
tickmode: 'linear',
1631+
tick0: '2000-01-01',
1632+
dtick: 'M1200',
1633+
range: ['1000-01-01', '3000-01-01'],
1634+
nticks: 10
1635+
};
1636+
mockCalc(ax);
1637+
1638+
checkHovers(ax, [
1639+
['2000-01-01', 'Jan 2000'],
1640+
['2000-01-01 11:00', 'Jan 2000'],
1641+
['2000-01-01 11:14', 'Jan 2000'],
1642+
['2000-01-01 11:00:15', 'Jan 2000'],
1643+
['2000-01-01 11:00:00.1', 'Jan 2000'],
1644+
['2000-01-01 11:00:00.0001', 'Jan 2000']
1645+
]);
1646+
1647+
ax.dtick = 'M1';
1648+
ax.range = ['1999-06-01', '2000-06-01'];
1649+
mockCalc(ax);
1650+
1651+
checkHovers(ax, [
1652+
['2000-01-01', 'Jan 1, 2000'],
1653+
['2000-01-01 11:00', 'Jan 1, 2000'],
1654+
['2000-01-01 11:14', 'Jan 1, 2000'],
1655+
['2000-01-01 11:00:15', 'Jan 1, 2000'],
1656+
['2000-01-01 11:00:00.1', 'Jan 1, 2000'],
1657+
['2000-01-01 11:00:00.0001', 'Jan 1, 2000']
1658+
]);
1659+
1660+
ax.dtick = 24 * 3600000; // one day
1661+
ax.range = ['1999-12-15', '2000-01-15'];
1662+
mockCalc(ax);
1663+
1664+
checkHovers(ax, [
1665+
['2000-01-01', 'Jan 1, 2000'],
1666+
['2000-01-01 11:00', 'Jan 1, 2000, 11:00'],
1667+
['2000-01-01 11:14', 'Jan 1, 2000, 11:14'],
1668+
['2000-01-01 11:00:15', 'Jan 1, 2000, 11:00'],
1669+
['2000-01-01 11:00:00.1', 'Jan 1, 2000, 11:00'],
1670+
['2000-01-01 11:00:00.0001', 'Jan 1, 2000, 11:00']
1671+
]);
1672+
1673+
ax.dtick = 3600000; // one hour
1674+
ax.range = ['1999-12-31', '2000-01-02'];
1675+
mockCalc(ax);
1676+
1677+
checkHovers(ax, [
1678+
['2000-01-01', 'Jan 1, 2000'],
1679+
['2000-01-01 11:00', 'Jan 1, 2000, 11:00'],
1680+
['2000-01-01 11:14', 'Jan 1, 2000, 11:14'],
1681+
['2000-01-01 11:00:15', 'Jan 1, 2000, 11:00:15'],
1682+
['2000-01-01 11:00:00.1', 'Jan 1, 2000, 11:00'],
1683+
['2000-01-01 11:00:00.0001', 'Jan 1, 2000, 11:00']
1684+
]);
1685+
1686+
ax.dtick = 60000; // one minute
1687+
ax.range = ['1999-12-31 23:00', '2000-01-01 01:00'];
1688+
mockCalc(ax);
1689+
1690+
checkHovers(ax, [
1691+
['2000-01-01', 'Jan 1, 2000'],
1692+
['2000-01-01 11:00', 'Jan 1, 2000, 11:00'],
1693+
['2000-01-01 11:14', 'Jan 1, 2000, 11:14'],
1694+
['2000-01-01 11:00:15', 'Jan 1, 2000, 11:00:15'],
1695+
['2000-01-01 11:00:00.1', 'Jan 1, 2000, 11:00'],
1696+
['2000-01-01 11:00:00.0001', 'Jan 1, 2000, 11:00']
1697+
]);
1698+
1699+
ax.dtick = 1000; // one second
1700+
ax.range = ['1999-12-31 23:59', '2000-01-01 00:01'];
1701+
mockCalc(ax);
1702+
1703+
checkHovers(ax, [
1704+
['2000-01-01', 'Jan 1, 2000'],
1705+
['2000-01-01 11:00', 'Jan 1, 2000, 11:00'],
1706+
['2000-01-01 11:14', 'Jan 1, 2000, 11:14'],
1707+
['2000-01-01 11:00:15', 'Jan 1, 2000, 11:00:15'],
1708+
['2000-01-01 11:00:00.1', 'Jan 1, 2000, 11:00:00.1'],
1709+
['2000-01-01 11:00:00.0001', 'Jan 1, 2000, 11:00:00.0001']
1710+
]);
1711+
});
15841712
});
15851713
});

0 commit comments

Comments
 (0)