Improvements to hoveranywhere / clickanywhere feature - #7966
Conversation
| @@ -0,0 +1 @@ | |||
| - Add top-level `xPixel` and `yPixel` keys to hover and click event data, corresponding to the pixel position of the cursor relative to the top-left corner of the graph div. Also, when `hoveranywhere` is enabled, emit a `plotly_unhover` event when the cursor leaves the plot area. [[#7966](https://github.com/plotly/plotly.js/pull/7966)] | |||
There was a problem hiding this comment.
| - Add top-level `xPixel` and `yPixel` keys to hover and click event data, corresponding to the pixel position of the cursor relative to the top-left corner of the graph div. Also, when `hoveranywhere` is enabled, emit a `plotly_unhover` event when the cursor leaves the plot area. [[#7966](https://github.com/plotly/plotly.js/pull/7966)] | |
| - Add top-level `xPixel` and `yPixel` keys to hover and click event data, corresponding to the pixel position of the cursor relative to the top-left corner of the graph div [[#7966](https://github.com/plotly/plotly.js/pull/7966)] | |
| - When `hoveranywhere` is enabled, a `plotly_unhover` event will be emitted when the cursor leaves the plot area | |
| if(gd._hoverAnywhereActive) { | ||
| gd._hoverAnywhereActive = false; | ||
|
|
||
| if(evt && evt.target && !oldhoverdata) { |
There was a problem hiding this comment.
| if(evt && evt.target && !oldhoverdata) { | |
| if(evt?.target && !oldhoverdata) { |
| it('does not emit unhover on leaving empty space when hoveranywhere is false', (done) => { | ||
| var events = []; | ||
|
|
||
| makePlot(gd) |
There was a problem hiding this comment.
It might be good to make this explicit:
| makePlot(gd) | |
| makePlot(gd, { hoveranywhere: false }) |
There was a problem hiding this comment.
Could you run npm run schema to regenerate the TS types with these new descriptions? You might also need to update the hand written types.
| gd._hoverPointerX = evt.pointerX; | ||
| gd._hoverPointerY = evt.pointerY; |
There was a problem hiding this comment.
Do these need to be moved out of this block to ensure that they're available without clickanywhere?
| // Special handling for `hoveranywhere`, to ensure we emit exactly one unhover event | ||
| // when the cursor leaves the plot area. | ||
| // gd._hoverAnywhereActive is set in fx/hover.js when we emit an empty-space hover event. | ||
| if(gd._hoverAnywhereActive) { |
There was a problem hiding this comment.
Could we update the guard to also check if hoveranywhere is active? Someone trying to break things could turn off hoveranywhere with relayout and then _hoverAnywhereActive would still be true.
| if(gd._hoverAnywhereActive) { | |
| if (gd._fullLayout?.hoveranywhere && gd._hoverAnywhereActive) { |
| gd.emit('plotly_unhover', { | ||
| event: evt, | ||
| points: [] | ||
| }); |
There was a problem hiding this comment.
This event doesn't check to see if plotly_beforehover is false, but it probably should to preserve that behavior. The current check happens inside raw. You could save that result on gd (or somewhere else) and use it in the conditional check.
Closes #7886
This PR adds two enhancements to the
hoveranywhere/clickanywherefeature, as suggested in #7886:xPixelandyPixelkeys to hover and click event data, corresponding to the cursor's pixel position relative to the top-left corner of the graph divxPixelandyPixelto all hover and click events, regardless of whetherhoveranywhereorclickanywhereare enabledxPixelandyPixelkeys for individual points in thepointsarray correspond to the pixel position of the point itself, while top-levelxPixelandyPixelcorrespond to the pixel position of the cursorhoveranywhereis enabled, emit aplotly_unhoverevent when the cursor leaves the plot areaThe PR also adds and updates Jasmine tests for the above behavior.
Steps for testing
npm ci && npm run buildDetails
plotly_click,plotly_hover, andplotly_unhoverevent. Forplotly_clickandplotly_hover, thexPixelandyPixelvalues will also be logged.xPixelandyPixelare always present and that their values seem accurate, regardless of whetherhoveranywhereorclickanywhereare enabledplotly_unhoverevent is emitted when the cursor leaves the plot area whenhoveranywhereis enabledv4.0and runnpm ci && npm run build(or edit the script tag to link to the plotly.js 4.0.0 RC on the CDN)xPixelandyPixelare never present in the event data, and also that aplotly_unhoverevent is never emitted when the cursor leaves the plot area