@@ -1514,6 +1514,176 @@ describe('scatter hoverPoints', function() {
15141514 } ) ;
15151515} ) ;
15161516
1517+ describe ( 'scatter hoverFills' , function ( ) {
1518+ afterEach ( destroyGraphDiv ) ;
1519+
1520+ function _hover ( gd , xval , yval , hovermode , subplotId ) {
1521+ return gd . _fullData . map ( function ( trace , i ) {
1522+ var cd = gd . calcdata [ i ] ;
1523+ var subplot = gd . _fullLayout . _plots [ subplotId ] ;
1524+
1525+ var out = Scatter . hoverPoints ( {
1526+ index : false ,
1527+ distance : 20 ,
1528+ cd : cd ,
1529+ trace : trace ,
1530+ xa : subplot . xaxis ,
1531+ ya : subplot . yaxis
1532+ } , xval , yval , hovermode ) ;
1533+
1534+ return Array . isArray ( out ) ? out [ 0 ] : null ;
1535+ } ) ;
1536+ }
1537+
1538+ it ( 'should correctly detect the fill that is hovered over for self and next fills' , function ( done ) {
1539+ var gd = createGraphDiv ( ) ;
1540+ var mock = Lib . extendDeep ( { } , require ( '../../image/mocks/scatter_fill_self_next' ) ) ;
1541+
1542+ var testPoints = [
1543+ [ [ 2 , 2.9 ] , [ 2 , 2 ] , [ 1.1 , 2 ] , [ 5.99 , 3.01 ] , [ 4.6 , 3.5 ] ] ,
1544+ [ [ 2 , 3.1 ] , [ - 0.2 , 1.1 ] , [ 5 , 2.99 ] , [ 7 , 2 ] , [ 1.2 , 5.1 ] ] ,
1545+ [ [ 6 , 5 ] , [ 7 , 6 ] , [ 8 , 5 ] , [ 7 , 5 ] , [ 6.7 , 5.3 ] ]
1546+ ] ;
1547+
1548+ Plotly . newPlot ( gd , mock ) . then ( function ( ) {
1549+ return Plotly . restyle ( gd , 'hoveron' , 'fills' ) ;
1550+ } )
1551+ . then ( function ( ) {
1552+ for ( var i = 0 ; i < testPoints . length ; i ++ ) {
1553+ for ( var j = 0 ; j < testPoints [ i ] . length ; j ++ ) {
1554+ var testCoords = testPoints [ i ] [ j ] ;
1555+ var pts = _hover ( gd , testCoords [ 0 ] , testCoords [ 1 ] , 'x' , 'xy' ) ;
1556+ expect ( pts [ i ] ) . toBeTruthy (
1557+ 'correct trace not detected ' + testCoords . join ( ',' ) + ', should be ' + i
1558+ ) ;
1559+ for ( var k = 0 ; k < pts . length ; k ++ ) {
1560+ var traceId = ( i + 1 ) % pts . length ;
1561+ expect ( pts [ traceId ] ) . toBeFalsy (
1562+ 'wrong trace detected ' + testCoords . join ( ',' ) + '; got ' +
1563+ traceId + ' but should be ' + i
1564+ ) ;
1565+ }
1566+ }
1567+ }
1568+ } )
1569+ . then ( done , done . fail ) ;
1570+ } ) ;
1571+
1572+ it ( 'should correctly detect the fill that is hovered over for tozeroy and tonexty fills' , function ( done ) {
1573+ var gd = createGraphDiv ( ) ;
1574+ var mock = Lib . extendDeep ( { } , require ( '../../image/mocks/scatter_fill_corner_cases' ) ) ;
1575+
1576+ var traceOffset = 0 ;
1577+
1578+ var testPoints = [ // all the following points should be in fill region of corresponding tozeroy traces 0-4
1579+ [ [ 1.5 , 1.24 ] , [ 1.5 , 1.06 ] ] , // single point has "fill" along line to zero
1580+ [ [ 0.1 , 0.9 ] , [ 0.1 , 0.8 ] , [ 1.5 , 0.9 ] , [ 1.5 , 1.04 ] , [ 2 , 0.8 ] , [ 2 , 1.09 ] , [ 3 , 0.8 ] ] ,
1581+ [ [ 0.1 , 0.75 ] , [ 0.1 , 0.61 ] , [ 1.01 , 0.501 ] , [ 1.5 , 0.8 ] , [ 1.5 , 0.55 ] , [ 2 , 0.74 ] , [ 2 , 0.55 ] , [ 3 , 0.74 ] , [ 3 , 0.51 ] ] ,
1582+ [ [ 0.1 , 0.599 ] , [ 0.1 , 0.5 ] , [ 0.1 , 0.3 ] , [ 0.99 , 0.59 ] , [ 1 , 0.49 ] , [ 1 , 0.36 ] , [ 1.5 , 0.26 ] , [ 2 , 0.49 ] , [ 2 , 0.16 ] , [ 3 , 0.49 ] , [ 3 , 0.26 ] ] ,
1583+ [ [ 0.1 , 0.25 ] , [ 0.1 , 0.1 ] , [ 1 , 0.34 ] , [ 1.5 , 0.24 ] , [ 2 , 0.14 ] , [ 3 , 0.24 ] , [ 3 , 0.1 ] ] ,
1584+ ] ;
1585+
1586+ var outsidePoints = [ // all these should not result in a hover detection, for any trace
1587+ [ 1 , 1.1 ] , [ 2 , 1.14 ] ,
1588+ ] ;
1589+
1590+ Plotly . newPlot ( gd , mock ) . then ( function ( ) {
1591+ return Plotly . restyle ( gd , 'hoveron' , 'fills' ) ;
1592+ } )
1593+ . then ( function ( ) {
1594+ var testCoords , pts ;
1595+ var i , j , k ;
1596+ for ( i = 0 ; i < testPoints . length ; i ++ ) {
1597+ for ( j = 0 ; j < testPoints [ i ] . length ; j ++ ) {
1598+ testCoords = testPoints [ i ] [ j ] ;
1599+ pts = _hover ( gd , testCoords [ 0 ] , testCoords [ 1 ] , 'x' , 'xy' ) ;
1600+ expect ( pts [ traceOffset + i ] ) . toBeTruthy (
1601+ 'correct trace not detected ' + testCoords . join ( ',' ) + ', should be ' + ( traceOffset + i )
1602+ ) ;
1603+
1604+ // since all polygons do extend to the zero axis, many points will be detected by the
1605+ // correct trace and previous ones, but a point should not be detected as hover points
1606+ // by any trace defined later than the correct trace!
1607+ // (in actual hover detection, the real _hover takes care of the overlap with previous traces
1608+ // so that is not an issue in practice)
1609+ for ( k = i + 1 ; k < testPoints . length ; k ++ ) {
1610+ var traceId = traceOffset + k ;
1611+ expect ( pts [ traceId ] ) . toBeFalsy (
1612+ 'wrong trace detected ' + testCoords . join ( ',' ) + '; got ' +
1613+ traceId + ' but should be ' + ( traceOffset + i )
1614+ ) ;
1615+ }
1616+ }
1617+ }
1618+
1619+ for ( j = 0 ; j < outsidePoints . length ; j ++ ) {
1620+ testCoords = outsidePoints [ j ] ;
1621+ pts = _hover ( gd , testCoords [ 0 ] , testCoords [ 1 ] , 'x' , 'xy' ) ;
1622+ for ( k = 0 ; k < testPoints . length ; k ++ ) {
1623+ expect ( pts [ i ] ) . toBeFalsy (
1624+ 'trace detected for outside point ' + testCoords . join ( ',' ) + ', got ' + ( traceOffset + k )
1625+ ) ;
1626+ }
1627+ }
1628+ } )
1629+ . then ( done , done . fail ) ;
1630+ } ) ;
1631+
1632+
1633+ it ( 'should correctly detect the fill that is hovered over for tonexty fills' , function ( done ) {
1634+ var gd = createGraphDiv ( ) ;
1635+ var mock = Lib . extendDeep ( { } , require ( '../../image/mocks/scatter_fill_corner_cases' ) ) ;
1636+
1637+ var traceOffset = 10 ;
1638+
1639+ var testPoints = [ // all the following points should be in fill region of corresponding tonexty traces 10-14
1640+ [ ] ,
1641+ [ [ 1 , 1.1 ] , [ 1.5 , 1.24 ] , [ 1.5 , 1.06 ] , [ 2 , 1.14 ] ] ,
1642+ [ [ 0.1 , 0.9 ] , [ 0.1 , 0.8 ] , [ 1.5 , 0.9 ] , [ 1.5 , 1.04 ] , [ 2 , 0.8 ] , [ 2 , 1.09 ] , [ 3 , 0.8 ] ] ,
1643+ [ [ 0.1 , 0.75 ] , [ 0.1 , 0.61 ] , [ 1.01 , 0.501 ] , [ 1.5 , 0.8 ] , [ 1.5 , 0.55 ] , [ 2 , 0.74 ] , [ 2 , 0.55 ] , [ 3 , 0.74 ] , [ 3 , 0.51 ] ] ,
1644+ [ [ 0.1 , 0.599 ] , [ 0.1 , 0.5 ] , [ 0.1 , 0.3 ] , [ 0.99 , 0.59 ] , [ 1 , 0.49 ] , [ 1 , 0.36 ] , [ 1.5 , 0.26 ] , [ 2 , 0.49 ] , [ 2 , 0.16 ] , [ 3 , 0.49 ] , [ 3 , 0.26 ] ] ,
1645+ ] ;
1646+ var outsidePoints = [ // all these should not result in a hover detection, for any trace
1647+ [ 0.1 , 0.25 ] , [ 0.1 , 0.1 ] , [ 1 , 0.34 ] , [ 1.5 , 0.24 ] , [ 2 , 0.14 ] , [ 3 , 0.24 ] , [ 3 , 0.1 ] , [ 0.5 , 1.15 ] , [ 2.5 , 1.15 ] ,
1648+ ] ;
1649+
1650+ Plotly . newPlot ( gd , mock ) . then ( function ( ) {
1651+ return Plotly . restyle ( gd , 'hoveron' , 'fills' ) ;
1652+ } )
1653+ . then ( function ( ) {
1654+ var testCoords , pts ;
1655+ var i , j , k ;
1656+ for ( i = 0 ; i < testPoints . length ; i ++ ) {
1657+ for ( j = 0 ; j < testPoints [ i ] . length ; j ++ ) {
1658+ testCoords = testPoints [ i ] [ j ] ;
1659+ pts = _hover ( gd , testCoords [ 0 ] , testCoords [ 1 ] , 'x' , 'xy2' ) ;
1660+ expect ( pts [ traceOffset + i ] ) . toBeTruthy (
1661+ 'correct trace not detected ' + testCoords . join ( ',' ) + ', should be ' + ( traceOffset + i )
1662+ ) ;
1663+
1664+ for ( k = 1 ; k < testPoints . length ; k ++ ) {
1665+ var traceId = traceOffset + ( ( i + k ) % testPoints . length ) ;
1666+ expect ( pts [ traceId ] ) . toBeFalsy (
1667+ 'wrong trace detected ' + testCoords . join ( ',' ) + '; got ' +
1668+ traceId + ' but should be ' + ( traceOffset + i )
1669+ ) ;
1670+ }
1671+ }
1672+ }
1673+ for ( j = 0 ; j < outsidePoints . length ; j ++ ) {
1674+ testCoords = outsidePoints [ j ] ;
1675+ pts = _hover ( gd , testCoords [ 0 ] , testCoords [ 1 ] , 'x' , 'xy2' ) ;
1676+ for ( k = 0 ; k < testPoints . length ; k ++ ) {
1677+ expect ( pts [ traceOffset + k ] ) . toBeFalsy (
1678+ 'trace detected for outside point ' + testCoords . join ( ',' ) + ', got ' + ( traceOffset + k )
1679+ ) ;
1680+ }
1681+ }
1682+ } )
1683+ . then ( done , done . fail ) ;
1684+ } ) ;
1685+ } ) ;
1686+
15171687describe ( 'Test Scatter.style' , function ( ) {
15181688 var gd ;
15191689
0 commit comments