diff --git a/draftlogs/7960_fix.md b/draftlogs/7960_fix.md new file mode 100644 index 00000000000..13e34e7a671 --- /dev/null +++ b/draftlogs/7960_fix.md @@ -0,0 +1,2 @@ +- Ensure that hoverlabels on category axes with `hovermode` "x", "y", "x unified" and "y unified" never include data points from categories other than the hovered one [[#7960](https://github.com/plotly/plotly.js/pull/7960)], with thanks to @my-tien for the contribution! +- Fix heatmap hover on category axes to report the cell center, not the cell edge, as the point position [[#7960](https://github.com/plotly/plotly.js/pull/7960)] diff --git a/src/components/fx/hover.js b/src/components/fx/hover.js index 342676cf654..0b9eb0ae51c 100644 --- a/src/components/fx/hover.js +++ b/src/components/fx/hover.js @@ -864,6 +864,23 @@ function _hover(gd, evt, subplot, noHoverEvent, eventTarget) { sortHoverData(); var axLetter = hovermode.charAt(0); + + // On category axes drop points outside the category under the cursor + const isCategoryAxis = (ax) => ax && ['category', 'multicategory'].includes(ax.type); + const axArray = axLetter === 'x' ? xaArray : yaArray; + const valArray = axLetter === 'x' ? xvalArray : yvalArray; + if (helpers.isXYhover(_mode) && valArray) { + const filteredHoverData = hoverData.filter((point) => { + const ax = point[axLetter + 'a']; + const i = axArray.indexOf(ax); + // Rounding works only on a hovered category axis, so keep other points unchanged + if (!isCategoryAxis(ax) || i === -1) return true; + return point[axLetter + 'LabelVal'] === Math.round(valArray[i]); + }); + // Keep the old behavior when no point falls in the hovered category + if (filteredHoverData.length) hoverData = filteredHoverData; + } + var spikeOnWinning = (axLetter === 'x' || axLetter === 'y') && hoverData[0] && cartesianScatterPoints[hoverData[0].trace.type]; @@ -972,6 +989,21 @@ function _hover(gd, evt, subplot, noHoverEvent, eventTarget) { for (k = hoverData.length - 1; k > initLen - 1; k--) { insert(hoverData[k]); } + + // on category axes only keep points that fall in the hovered category + var winAx = winningPoint[axLetter + 'a']; + if (winAx && (winAx.type === 'category' || winAx.type === 'multicategory')) { + var winCat = winningPoint[axLetter + 'LabelVal']; + finalPoints = finalPoints.filter(function (point) { + var pointAx = point[axLetter + 'a']; + if (!pointAx || (pointAx.type !== 'category' && pointAx.type !== 'multicategory')) { + // rare case of data point on overlaying axis + return true; + } + return point[axLetter + 'LabelVal'] === winCat; + }); + } + hoverData = finalPoints; sortHoverData(); } diff --git a/src/components/fx/layout_attributes.js b/src/components/fx/layout_attributes.js index fd7fae8dbb1..180dc8b32db 100644 --- a/src/components/fx/layout_attributes.js +++ b/src/components/fx/layout_attributes.js @@ -142,7 +142,10 @@ module.exports = { 'This is only a real distance for hovering on point-like objects,', 'like scatter points. For area-like objects (bars, scatter fills, etc)', 'hovering is on inside the area and off outside, but these objects', - 'will not supersede hover on point-like objects in case of conflict.' + 'will not supersede hover on point-like objects in case of conflict.', + 'For hovermodes *x*, *y*, *x unified* and *y unified*', + 'on a category axis, only points that fall in the hovered category', + 'are shown in the hover label, regardless of this distance.' ].join(' ') }, spikedistance: { diff --git a/src/traces/heatmap/hover.js b/src/traces/heatmap/hover.js index b67317b26eb..706ffad8ded 100644 --- a/src/traces/heatmap/hover.js +++ b/src/traces/heatmap/hover.js @@ -82,8 +82,9 @@ module.exports = function hoverPoints(pointData, xval, yval, hovermode, opts) { xl = xc ? _x[nx] : ((_x[nx] + _x[nx + 1]) / 2); yl = yc ? _y[ny] : ((_y[ny] + _y[ny + 1]) / 2); - if(xa && xa.type === 'category') xl = x[nx]; - if(ya && ya.type === 'category') yl = y[ny]; + // Report the cell center, which is the category position + if (xa?.type === 'category') xl = (x[nx] + x[nx + 1]) / 2; + if (ya?.type === 'category') yl = (y[ny] + y[ny + 1]) / 2; if(trace.zsmooth) { x0 = x1 = xa.c2p(xl); diff --git a/src/types/generated/schema.d.ts b/src/types/generated/schema.d.ts index 8a855665e73..1a50b413e3a 100644 --- a/src/types/generated/schema.d.ts +++ b/src/types/generated/schema.d.ts @@ -16256,7 +16256,7 @@ export interface Layout { */ hoveranywhere?: boolean; /** - * Sets the default distance (in pixels) to look for data to add hover labels (-1 means no cutoff, 0 means no looking for data). This is only a real distance for hovering on point-like objects, like scatter points. For area-like objects (bars, scatter fills, etc) hovering is on inside the area and off outside, but these objects will not supersede hover on point-like objects in case of conflict. + * Sets the default distance (in pixels) to look for data to add hover labels (-1 means no cutoff, 0 means no looking for data). This is only a real distance for hovering on point-like objects, like scatter points. For area-like objects (bars, scatter fills, etc) hovering is on inside the area and off outside, but these objects will not supersede hover on point-like objects in case of conflict. For hovermodes *x*, *y*, *x unified* and *y unified* on a category axis, only points that fall in the hovered category are shown in the hover label, regardless of this distance. * @default 20 * Minimum: -1 */ diff --git a/test/jasmine/tests/heatmap_test.js b/test/jasmine/tests/heatmap_test.js index ceb527f4132..16f61078247 100644 --- a/test/jasmine/tests/heatmap_test.js +++ b/test/jasmine/tests/heatmap_test.js @@ -1016,7 +1016,7 @@ describe('heatmap hover', function() { it('should find closest point (case 1) and should', function() { var pt = _hover(gd, 3, 1)[0]; expect(pt.index).toEqual([1, 3], 'have correct index'); - assertLabels(pt, 2.5, 0.5, 0); + assertLabels(pt, 3, 1, 0); }); }); diff --git a/test/jasmine/tests/hover_test.js b/test/jasmine/tests/hover_test.js index 597f4224959..cee2c7831dc 100644 --- a/test/jasmine/tests/hover_test.js +++ b/test/jasmine/tests/hover_test.js @@ -5532,6 +5532,106 @@ describe('hover distance', function () { .then(done, done.fail); }); + it('only selects points in the category under the cursor on a category axis', (done) => { + // Categories 21px wide put the line point at 'c28' within the default hoverdistance of 'Other' (see #7152) + const x = Array.from({ length: 29 }, (_, i) => 'c' + i).concat('Other'); + const mock = { + data: [ + { type: 'bar', x, y: x.map((_, i) => i + 1) }, + { mode: 'lines', x, y: x.map((_, i) => (i === 29 ? null : 40 - i)) } + ], + layout: { + width: 700, + showlegend: false, + hovermode: 'x', + margin: { l: 0, r: 0 }, + xaxis: { range: [-1.753, 31.588] } + } + }; + const hoveredCategories = (xval) => { + Fx.hover(gd, { xval }); + Lib.clearThrottle(); + return gd._hoverdata.map((d) => d.x); + }; + + Plotly.newPlot(gd, mock) + .then(() => { + expect(hoveredCategories(29)).toEqual(['Other']); + expect(hoveredCategories(28.6)).toEqual(['Other']); + expect(hoveredCategories(28)).toEqual(['c28', 'c28']); + }) + .then(done, done.fail); + }); + + it('keeps heatmap points in the category under the cursor on a category axis', (done) => { + // A heatmap must report the cell center, not the cell edge, as its position on a category axis + const x = Array.from({ length: 29 }, (_, i) => 'c' + i).concat('Other'); + const z = x.map((_, i) => i + 1); + const mock = { + data: [ + { type: 'heatmap', name: 'hm', x, y: [0, 1], z: [z, z], showscale: false }, + { type: 'bar', name: 'bar', x, y: x.map(() => 1) } + ], + layout: { + width: 700, + showlegend: false, + hovermode: 'x', + margin: { l: 0, r: 0 }, + xaxis: { range: [-1.753, 31.588] } + } + }; + const hoveredTraces = (xval) => { + Fx.hover(gd, { xval }); + Lib.clearThrottle(); + return gd._hoverdata.map((d) => d.data.name).sort(); + }; + + Plotly.newPlot(gd, mock) + .then(() => { + expect(hoveredTraces(28)).toEqual(['bar', 'hm']); + expect(hoveredTraces(29)).toEqual(['bar', 'hm']); + }) + .then(done, done.fail); + }); + + it('drops points in the next category on an overlaying category axis', (done) => { + // Categories 21px wide put the line point at 'k28' within the default hoverdistance of 'Other' (see #7152) + const x = Array.from({ length: 29 }, (_, i) => 'c' + i).concat('Other'); + const mock = { + data: [ + { type: 'bar', name: 'bar', x, y: x.map((_, i) => i + 1) }, + { + mode: 'lines', + name: 'line', + xaxis: 'x2', + x: x.map((_, i) => 'k' + i), + y: x.map((_, i) => (i === 29 ? null : 40 - i)) + } + ], + layout: { + width: 700, + showlegend: false, + hovermode: 'x', + margin: { l: 0, r: 0 }, + xaxis: { range: [-1.753, 31.588] }, + xaxis2: { overlaying: 'x', side: 'top', range: [-1.753, 31.588] } + } + }; + const hoveredPoints = (xval) => { + Fx.hover(gd, { xval }); + Lib.clearThrottle(); + return gd._hoverdata.map((d) => d.data.name + '@' + d.x).sort(); + }; + + Plotly.newPlot(gd, mock) + .then(() => { + expect(hoveredPoints(29)).toEqual(['bar@Other']); + expect(hoveredPoints(28.6)).toEqual(['bar@Other']); + expect(hoveredPoints(28)).toEqual(['bar@c28', 'line@k28']); + }) + .then(done, done.fail); + }); + it('selects all points at the same position on a date axis', function (done) { var x = ['2018', '2019', '2020']; var mock = { @@ -7635,6 +7735,63 @@ describe('hovermode: (x|y)unified', function () { }) .then(done, done.fail); }); + + it('hover label should only show values of hovered category', (done) => { + Plotly.newPlot(gd, { + data: [ + { name: 'bar', x: ['A', 'B'], y: [1, 2], type: 'bar' }, + { + name: 'scatter', + x: ['A', 'B'], + y: [10, null], + type: 'scatter' + } + ], + layout: { + width: 400, + hovermode: 'x unified', + hoverdistance: 110 + } + }) + .then(() => { + _hover(gd, { xval: 1.1 }); + assertLabel({ title: 'B', items: ['bar : 2'] }); + }) + .then(() => { + _hover(gd, { xval: 0 }); + assertLabel({ title: 'A', items: ['bar : 1', 'scatter : 10'] }); + }) + .then(done, done.fail); + }); + + it('hover label should show the category under the cursor when a line point in the next category is within hoverdistance', (done) => { + // Categories 21px wide put the line point at 'c28' within the default hoverdistance of 'Other' (see #7152) + const x = Array.from({ length: 29 }, (_, i) => 'c' + i).concat('Other'); + Plotly.newPlot(gd, { + data: [ + { name: 'bar', type: 'bar', x, y: x.map((_, i) => i + 1) }, + { name: 'line', mode: 'lines', x, y: x.map((_, i) => (i === 29 ? null : 40 - i)) } + ], + layout: { + width: 700, + showlegend: false, + hovermode: 'x unified', + margin: { l: 0, r: 0 }, + xaxis: { range: [-1.753, 31.588] } + } + }) + .then(() => { + _hover(gd, { xval: 29 }); + assertLabel({ title: 'Other', items: ['bar : 30'] }); + + _hover(gd, { xval: 28.6 }); + assertLabel({ title: 'Other', items: ['bar : 30'] }); + + _hover(gd, { xval: 28 }); + assertLabel({ title: 'c28', items: ['bar : 29', 'line : 12'] }); + }) + .then(done, done.fail); + }); }); describe('hover on traces with (x|y)hoverformat', function () { diff --git a/test/plot-schema.json b/test/plot-schema.json index 4d51312e82d..2adb0136367 100644 --- a/test/plot-schema.json +++ b/test/plot-schema.json @@ -2822,7 +2822,7 @@ "valType": "boolean" }, "hoverdistance": { - "description": "Sets the default distance (in pixels) to look for data to add hover labels (-1 means no cutoff, 0 means no looking for data). This is only a real distance for hovering on point-like objects, like scatter points. For area-like objects (bars, scatter fills, etc) hovering is on inside the area and off outside, but these objects will not supersede hover on point-like objects in case of conflict.", + "description": "Sets the default distance (in pixels) to look for data to add hover labels (-1 means no cutoff, 0 means no looking for data). This is only a real distance for hovering on point-like objects, like scatter points. For area-like objects (bars, scatter fills, etc) hovering is on inside the area and off outside, but these objects will not supersede hover on point-like objects in case of conflict. For hovermodes *x*, *y*, *x unified* and *y unified* on a category axis, only points that fall in the hovered category are shown in the hover label, regardless of this distance.", "dflt": 20, "editType": "none", "min": -1,