@@ -1393,22 +1393,36 @@ describe('Test axes', function() {
1393
1393
} ) ;
1394
1394
} ) ;
1395
1395
1396
- describe ( 'calcTicks' , function ( ) {
1396
+ describe ( 'calcTicks and tickText ' , function ( ) {
1397
1397
function mockCalc ( ax ) {
1398
1398
Axes . setConvert ( ax ) ;
1399
1399
ax . tickfont = { } ;
1400
1400
ax . _gd = { _fullLayout : { separators : '.,' } } ;
1401
1401
return Axes . calcTicks ( ax ) . map ( function ( v ) { return v . text ; } ) ;
1402
1402
}
1403
1403
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
+
1404
1417
it ( 'provides a new date suffix whenever the suffix changes' , function ( ) {
1405
- var textOut = mockCalc ( {
1418
+ var ax = {
1406
1419
type : 'date' ,
1407
1420
tickmode : 'linear' ,
1408
1421
tick0 : '2000-01-01' ,
1409
1422
dtick : 14 * 24 * 3600 * 1000 , // 14 days
1410
1423
range : [ '1999-12-01' , '2000-02-15' ]
1411
- } ) ;
1424
+ } ;
1425
+ var textOut = mockCalc ( ax ) ;
1412
1426
1413
1427
var expectedText = [
1414
1428
'Dec 4<br>1999' ,
@@ -1419,14 +1433,17 @@ describe('Test axes', function() {
1419
1433
'Feb 12'
1420
1434
] ;
1421
1435
expect ( textOut ) . toEqual ( expectedText ) ;
1436
+ expect ( mockHoverText ( ax , ax . d2c ( '1999-12-18 15:34:33.3' ) ) )
1437
+ . toBe ( 'Dec 18, 1999, 15:34' ) ;
1422
1438
1423
- textOut = mockCalc ( {
1439
+ ax = {
1424
1440
type : 'date' ,
1425
1441
tickmode : 'linear' ,
1426
1442
tick0 : '2000-01-01' ,
1427
1443
dtick : 12 * 3600 * 1000 , // 12 hours
1428
1444
range : [ '2000-01-03 11:00' , '2000-01-06' ]
1429
- } ) ;
1445
+ } ;
1446
+ textOut = mockCalc ( ax ) ;
1430
1447
1431
1448
expectedText = [
1432
1449
'12:00<br>Jan 3, 2000' ,
@@ -1437,14 +1454,17 @@ describe('Test axes', function() {
1437
1454
'00:00<br>Jan 6, 2000'
1438
1455
] ;
1439
1456
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' ) ;
1440
1459
1441
- textOut = mockCalc ( {
1460
+ ax = {
1442
1461
type : 'date' ,
1443
1462
tickmode : 'linear' ,
1444
1463
tick0 : '2000-01-01' ,
1445
1464
dtick : 1000 , // 1 sec
1446
1465
range : [ '2000-02-03 23:59:57' , '2000-02-04 00:00:02' ]
1447
- } ) ;
1466
+ } ;
1467
+ textOut = mockCalc ( ax ) ;
1448
1468
1449
1469
expectedText = [
1450
1470
'23:59:57<br>Feb 3, 2000' ,
@@ -1455,16 +1475,21 @@ describe('Test axes', function() {
1455
1475
'00:00:02'
1456
1476
] ;
1457
1477
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' ) ;
1458
1482
} ) ;
1459
1483
1460
1484
it ( 'should give dates extra precision if tick0 is weird' , function ( ) {
1461
- var textOut = mockCalc ( {
1485
+ var ax = {
1462
1486
type : 'date' ,
1463
1487
tickmode : 'linear' ,
1464
1488
tick0 : '2000-01-01 00:05' ,
1465
1489
dtick : 14 * 24 * 3600 * 1000 , // 14 days
1466
1490
range : [ '1999-12-01' , '2000-02-15' ]
1467
- } ) ;
1491
+ } ;
1492
+ var textOut = mockCalc ( ax ) ;
1468
1493
1469
1494
var expectedText = [
1470
1495
'00:05<br>Dec 4, 1999' ,
@@ -1475,16 +1500,21 @@ describe('Test axes', function() {
1475
1500
'00:05<br>Feb 12, 2000'
1476
1501
] ;
1477
1502
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' ) ;
1478
1507
} ) ;
1479
1508
1480
1509
it ( 'should never give dates more than 100 microsecond precision' , function ( ) {
1481
- var textOut = mockCalc ( {
1510
+ var ax = {
1482
1511
type : 'date' ,
1483
1512
tickmode : 'linear' ,
1484
1513
tick0 : '2000-01-01' ,
1485
1514
dtick : 1.1333 ,
1486
1515
range : [ '2000-01-01' , '2000-01-01 00:00:00.01' ]
1487
- } ) ;
1516
+ } ;
1517
+ var textOut = mockCalc ( ax ) ;
1488
1518
1489
1519
var expectedText = [
1490
1520
'00:00:00<br>Jan 1, 2000' ,
@@ -1501,7 +1531,7 @@ describe('Test axes', function() {
1501
1531
} ) ;
1502
1532
1503
1533
it ( 'should handle edge cases with dates and tickvals' , function ( ) {
1504
- var textOut = mockCalc ( {
1534
+ var ax = {
1505
1535
type : 'date' ,
1506
1536
tickmode : 'array' ,
1507
1537
tickvals : [
@@ -1518,7 +1548,8 @@ describe('Test axes', function() {
1518
1548
// required to get calcTicks to run
1519
1549
range : [ '2011-12-10' , '2013-01-23' ] ,
1520
1550
nticks : 10
1521
- } ) ;
1551
+ } ;
1552
+ var textOut = mockCalc ( ax ) ;
1522
1553
1523
1554
var expectedText = [
1524
1555
'New year' ,
@@ -1528,19 +1559,24 @@ describe('Test axes', function() {
1528
1559
'00:00:01<br>Jan 1, 2013'
1529
1560
] ;
1530
1561
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' ) ;
1531
1566
} ) ;
1532
1567
1533
1568
it ( 'should handle tickvals edge cases with linear and log axes' , function ( ) {
1534
1569
[ 'linear' , 'log' ] . forEach ( function ( axType ) {
1535
- var textOut = mockCalc ( {
1570
+ var ax = {
1536
1571
type : axType ,
1537
1572
tickmode : 'array' ,
1538
1573
tickvals : [ 1 , 1.5 , 2.6999999 , 30 , 39.999 , 100 , 0.1 ] ,
1539
1574
ticktext : [ 'One' , '...and a half' ] ,
1540
1575
// I'll be so happy when I can finally get rid of this switch!
1541
1576
range : axType === 'log' ? [ - 0.2 , 1.8 ] : [ 0.5 , 50 ] ,
1542
1577
nticks : 10
1543
- } ) ;
1578
+ } ;
1579
+ var textOut = mockCalc ( ax ) ;
1544
1580
1545
1581
var expectedText = [
1546
1582
'One' ,
@@ -1551,6 +1587,8 @@ describe('Test axes', function() {
1551
1587
// 10 and 0.1 are off scale
1552
1588
] ;
1553
1589
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' ) ;
1554
1592
} ) ;
1555
1593
} ) ;
1556
1594
@@ -1578,8 +1616,98 @@ describe('Test axes', function() {
1578
1616
// 'e', 5, -2: bad category and numbers out of range: omitted
1579
1617
] ;
1580
1618
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
1582
1624
expect ( ax . _categories ) . toEqual ( [ 'a' , 'b' , 'c' , 'd' ] ) ;
1583
1625
} ) ;
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
+ } ) ;
1584
1712
} ) ;
1585
1713
} ) ;
0 commit comments