From 015d3f0659c1563c181e0e920575c431c5c2388b Mon Sep 17 00:00:00 2001 From: My-Tien Nguyen Date: Thu, 13 Aug 2026 15:06:03 +0200 Subject: [PATCH 1/9] Fix #7152 (Hover label on category sometimes shows label from neighboring category) Now, data points within hoverdistance but outside the hovered category are omitted from the hover label. Within the category, the hoverdistance still filters away too distant data points. --- src/components/fx/hover.js | 15 +++++++++++++++ src/components/fx/layout_attributes.js | 5 ++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/components/fx/hover.js b/src/components/fx/hover.js index 543a4606f46..97600420bda 100644 --- a/src/components/fx/hover.js +++ b/src/components/fx/hover.js @@ -878,6 +878,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 73c76275dc1..4117c95d237 100644 --- a/src/components/fx/layout_attributes.js +++ b/src/components/fx/layout_attributes.js @@ -137,7 +137,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: { From 25f83cd97273358c5cc41bc3f4c2dfc584c2782b Mon Sep 17 00:00:00 2001 From: My-Tien Nguyen Date: Thu, 13 Aug 2026 15:15:11 +0200 Subject: [PATCH 2/9] Update plot-schema.json with changes to hoverdistance --- test/plot-schema.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/plot-schema.json b/test/plot-schema.json index 78614b86693..a1ace0c0d3a 100644 --- a/test/plot-schema.json +++ b/test/plot-schema.json @@ -2825,7 +2825,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, From 2f8ffa2defb87c8c644fde64629045857c0e8631 Mon Sep 17 00:00:00 2001 From: My-Tien Nguyen Date: Thu, 13 Aug 2026 15:20:59 +0200 Subject: [PATCH 3/9] Add draftlog for fix PR 7960. --- draftlogs/7960_fix.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 draftlogs/7960_fix.md diff --git a/draftlogs/7960_fix.md b/draftlogs/7960_fix.md new file mode 100644 index 00000000000..69ad9ca9d90 --- /dev/null +++ b/draftlogs/7960_fix.md @@ -0,0 +1 @@ +- Ensure that hoverlabels on category axes with hovermode "x", "y", "x unified" and "y unified" never include data points from other categories than the hovered one [[#7960](https://github.com/plotly/plotly.js/pull/7960)] From fcebdfc24e221dc2ff3476aa52677d12b290f94f Mon Sep 17 00:00:00 2001 From: My-Tien Nguyen Date: Thu, 13 Aug 2026 16:28:06 +0200 Subject: [PATCH 4/9] Add jasmine test for fix that hover label should only include data from hovered category --- test/jasmine/tests/hover_label_test.js | 32 ++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/test/jasmine/tests/hover_label_test.js b/test/jasmine/tests/hover_label_test.js index 2076c3fcf41..13c8c2a3b5b 100644 --- a/test/jasmine/tests/hover_label_test.js +++ b/test/jasmine/tests/hover_label_test.js @@ -7436,6 +7436,38 @@ describe('hovermode: (x|y)unified', function () { }) .then(done, done.fail); }); + + fit('hover label should only show values of hovered category', function (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(function () { + _hover(gd, { xval: 1.1 }); + assertLabel({ title: 'B', items: ['bar : 2'] }); + }) + .then(function () { + _hover(gd, { xval: 0 }); + assertLabel({ title: 'A', items: ['bar : 1', 'scatter : 10'] }); + }) + .then(done, done.fail); + }); }); describe('hover on traces with (x|y)hoverformat', function () { From 4da35d883630b813153aa63ef154642e56ecf69b Mon Sep 17 00:00:00 2001 From: My-Tien Nguyen Date: Thu, 13 Aug 2026 16:36:42 +0200 Subject: [PATCH 5/9] Remove accidental commit of `fit` call in jasmine test --- test/jasmine/tests/hover_label_test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/jasmine/tests/hover_label_test.js b/test/jasmine/tests/hover_label_test.js index 13c8c2a3b5b..b530f08ec4d 100644 --- a/test/jasmine/tests/hover_label_test.js +++ b/test/jasmine/tests/hover_label_test.js @@ -7437,7 +7437,7 @@ describe('hovermode: (x|y)unified', function () { .then(done, done.fail); }); - fit('hover label should only show values of hovered category', function (done) { + it('hover label should only show values of hovered category', function (done) { Plotly.newPlot(gd, { data: [ { name: 'bar', From ce47785a4c74b04e05d22bbe26c7f946a095df7e Mon Sep 17 00:00:00 2001 From: My-Tien Nguyen Date: Tue, 27 Oct 2026 10:29:17 +0100 Subject: [PATCH 6/9] Regenerate schema.d.ts for updated hoverdistance description --- src/types/generated/schema.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 */ From 81d07c0eb7dc19160e321a26282ba0f3916a43b4 Mon Sep 17 00:00:00 2001 From: Cameron DeCoster Date: Thu, 19 Nov 2026 19:45:56 -0700 Subject: [PATCH 7/9] Report center of hovered cell for heatmap with category axis --- draftlogs/7960_fix.md | 1 + src/traces/heatmap/hover.js | 5 +++-- test/jasmine/tests/heatmap_test.js | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/draftlogs/7960_fix.md b/draftlogs/7960_fix.md index 69ad9ca9d90..e56701acc46 100644 --- a/draftlogs/7960_fix.md +++ b/draftlogs/7960_fix.md @@ -1 +1,2 @@ - Ensure that hoverlabels on category axes with hovermode "x", "y", "x unified" and "y unified" never include data points from other categories than the hovered one [[#7960](https://github.com/plotly/plotly.js/pull/7960)] +- 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/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/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); }); }); From 3529284e5e799b95d9d012c2e989867cb30646ca Mon Sep 17 00:00:00 2001 From: Cameron DeCoster Date: Thu, 19 Nov 2026 20:02:49 -0700 Subject: [PATCH 8/9] Filter hover points by the category under the cursor --- src/components/fx/hover.js | 17 ++++ test/jasmine/tests/hover_test.js | 153 ++++++++++++++++++++++++++++--- 2 files changed, 156 insertions(+), 14 deletions(-) diff --git a/src/components/fx/hover.js b/src/components/fx/hover.js index d5db091eb54..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]; diff --git a/test/jasmine/tests/hover_test.js b/test/jasmine/tests/hover_test.js index e4074bf499d..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 = { @@ -7636,14 +7736,10 @@ describe('hovermode: (x|y)unified', function () { .then(done, done.fail); }); - it('hover label should only show values of hovered category', function (done) { + 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: 'bar', x: ['A', 'B'], y: [1, 2], type: 'bar' }, { name: 'scatter', x: ['A', 'B'], @@ -7657,15 +7753,44 @@ describe('hovermode: (x|y)unified', function () { hoverdistance: 110 } }) - .then(function () { - _hover(gd, { xval: 1.1 }); - assertLabel({ title: 'B', items: ['bar : 2'] }); - }) - .then(function () { - _hover(gd, { xval: 0 }); - assertLabel({ title: 'A', items: ['bar : 1', 'scatter : 10'] }); + .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(done, done.fail); + .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); }); }); From 6f4d1565a62720b59898fea65c68495f80ecb95d Mon Sep 17 00:00:00 2001 From: Cameron DeCoster Date: Thu, 19 Nov 2026 20:14:02 -0700 Subject: [PATCH 9/9] Update draftlog --- draftlogs/7960_fix.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/draftlogs/7960_fix.md b/draftlogs/7960_fix.md index e56701acc46..13e34e7a671 100644 --- a/draftlogs/7960_fix.md +++ b/draftlogs/7960_fix.md @@ -1,2 +1,2 @@ -- Ensure that hoverlabels on category axes with hovermode "x", "y", "x unified" and "y unified" never include data points from other categories than the hovered one [[#7960](https://github.com/plotly/plotly.js/pull/7960)] +- 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)]