Skip to content

Commit 3529284

Browse files
committed
Filter hover points by the category under the cursor
1 parent 81d07c0 commit 3529284

2 files changed

Lines changed: 156 additions & 14 deletions

File tree

‎src/components/fx/hover.js‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,23 @@ function _hover(gd, evt, subplot, noHoverEvent, eventTarget) {
864864
sortHoverData();
865865

866866
var axLetter = hovermode.charAt(0);
867+
868+
// On category axes drop points outside the category under the cursor
869+
const isCategoryAxis = (ax) => ax && ['category', 'multicategory'].includes(ax.type);
870+
const axArray = axLetter === 'x' ? xaArray : yaArray;
871+
const valArray = axLetter === 'x' ? xvalArray : yvalArray;
872+
if (helpers.isXYhover(_mode) && valArray) {
873+
const filteredHoverData = hoverData.filter((point) => {
874+
const ax = point[axLetter + 'a'];
875+
const i = axArray.indexOf(ax);
876+
// Rounding works only on a hovered category axis, so keep other points unchanged
877+
if (!isCategoryAxis(ax) || i === -1) return true;
878+
return point[axLetter + 'LabelVal'] === Math.round(valArray[i]);
879+
});
880+
// Keep the old behavior when no point falls in the hovered category
881+
if (filteredHoverData.length) hoverData = filteredHoverData;
882+
}
883+
867884
var spikeOnWinning =
868885
(axLetter === 'x' || axLetter === 'y') && hoverData[0] && cartesianScatterPoints[hoverData[0].trace.type];
869886

‎test/jasmine/tests/hover_test.js‎

Lines changed: 139 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5532,6 +5532,106 @@ describe('hover distance', function () {
55325532
.then(done, done.fail);
55335533
});
55345534

5535+
it('only selects points in the category under the cursor on a category axis', (done) => {
5536+
// Categories 21px wide put the line point at 'c28' within the default hoverdistance of 'Other' (see #7152)
5537+
const x = Array.from({ length: 29 }, (_, i) => 'c' + i).concat('Other');
5538+
const mock = {
5539+
data: [
5540+
{ type: 'bar', x, y: x.map((_, i) => i + 1) },
5541+
{ mode: 'lines', x, y: x.map((_, i) => (i === 29 ? null : 40 - i)) }
5542+
],
5543+
layout: {
5544+
width: 700,
5545+
showlegend: false,
5546+
hovermode: 'x',
5547+
margin: { l: 0, r: 0 },
5548+
xaxis: { range: [-1.753, 31.588] }
5549+
}
5550+
};
5551+
const hoveredCategories = (xval) => {
5552+
Fx.hover(gd, { xval });
5553+
Lib.clearThrottle();
5554+
return gd._hoverdata.map((d) => d.x);
5555+
};
5556+
5557+
Plotly.newPlot(gd, mock)
5558+
.then(() => {
5559+
expect(hoveredCategories(29)).toEqual(['Other']);
5560+
expect(hoveredCategories(28.6)).toEqual(['Other']);
5561+
expect(hoveredCategories(28)).toEqual(['c28', 'c28']);
5562+
})
5563+
.then(done, done.fail);
5564+
});
5565+
5566+
it('keeps heatmap points in the category under the cursor on a category axis', (done) => {
5567+
// A heatmap must report the cell center, not the cell edge, as its position on a category axis
5568+
const x = Array.from({ length: 29 }, (_, i) => 'c' + i).concat('Other');
5569+
const z = x.map((_, i) => i + 1);
5570+
const mock = {
5571+
data: [
5572+
{ type: 'heatmap', name: 'hm', x, y: [0, 1], z: [z, z], showscale: false },
5573+
{ type: 'bar', name: 'bar', x, y: x.map(() => 1) }
5574+
],
5575+
layout: {
5576+
width: 700,
5577+
showlegend: false,
5578+
hovermode: 'x',
5579+
margin: { l: 0, r: 0 },
5580+
xaxis: { range: [-1.753, 31.588] }
5581+
}
5582+
};
5583+
const hoveredTraces = (xval) => {
5584+
Fx.hover(gd, { xval });
5585+
Lib.clearThrottle();
5586+
return gd._hoverdata.map((d) => d.data.name).sort();
5587+
};
5588+
5589+
Plotly.newPlot(gd, mock)
5590+
.then(() => {
5591+
expect(hoveredTraces(28)).toEqual(['bar', 'hm']);
5592+
expect(hoveredTraces(29)).toEqual(['bar', 'hm']);
5593+
})
5594+
.then(done, done.fail);
5595+
});
5596+
5597+
it('drops points in the next category on an overlaying category axis', (done) => {
5598+
// Categories 21px wide put the line point at 'k28' within the default hoverdistance of 'Other' (see #7152)
5599+
const x = Array.from({ length: 29 }, (_, i) => 'c' + i).concat('Other');
5600+
const mock = {
5601+
data: [
5602+
{ type: 'bar', name: 'bar', x, y: x.map((_, i) => i + 1) },
5603+
{
5604+
mode: 'lines',
5605+
name: 'line',
5606+
xaxis: 'x2',
5607+
x: x.map((_, i) => 'k' + i),
5608+
y: x.map((_, i) => (i === 29 ? null : 40 - i))
5609+
}
5610+
],
5611+
layout: {
5612+
width: 700,
5613+
showlegend: false,
5614+
hovermode: 'x',
5615+
margin: { l: 0, r: 0 },
5616+
xaxis: { range: [-1.753, 31.588] },
5617+
xaxis2: { overlaying: 'x', side: 'top', range: [-1.753, 31.588] }
5618+
}
5619+
};
5620+
const hoveredPoints = (xval) => {
5621+
Fx.hover(gd, { xval });
5622+
Lib.clearThrottle();
5623+
return gd._hoverdata.map((d) => d.data.name + '@' + d.x).sort();
5624+
};
5625+
5626+
Plotly.newPlot(gd, mock)
5627+
.then(() => {
5628+
expect(hoveredPoints(29)).toEqual(['bar@Other']);
5629+
expect(hoveredPoints(28.6)).toEqual(['bar@Other']);
5630+
expect(hoveredPoints(28)).toEqual(['bar@c28', 'line@k28']);
5631+
})
5632+
.then(done, done.fail);
5633+
});
5634+
55355635
it('selects all points at the same position on a date axis', function (done) {
55365636
var x = ['2018', '2019', '2020'];
55375637
var mock = {
@@ -7636,14 +7736,10 @@ describe('hovermode: (x|y)unified', function () {
76367736
.then(done, done.fail);
76377737
});
76387738

7639-
it('hover label should only show values of hovered category', function (done) {
7739+
it('hover label should only show values of hovered category', (done) => {
76407740
Plotly.newPlot(gd, {
76417741
data: [
7642-
{ name: 'bar',
7643-
x: ['A', 'B'],
7644-
y: [1, 2],
7645-
type: 'bar'
7646-
},
7742+
{ name: 'bar', x: ['A', 'B'], y: [1, 2], type: 'bar' },
76477743
{
76487744
name: 'scatter',
76497745
x: ['A', 'B'],
@@ -7657,15 +7753,44 @@ describe('hovermode: (x|y)unified', function () {
76577753
hoverdistance: 110
76587754
}
76597755
})
7660-
.then(function () {
7661-
_hover(gd, { xval: 1.1 });
7662-
assertLabel({ title: 'B', items: ['bar : 2'] });
7663-
})
7664-
.then(function () {
7665-
_hover(gd, { xval: 0 });
7666-
assertLabel({ title: 'A', items: ['bar : 1', 'scatter : 10'] });
7756+
.then(() => {
7757+
_hover(gd, { xval: 1.1 });
7758+
assertLabel({ title: 'B', items: ['bar : 2'] });
7759+
})
7760+
.then(() => {
7761+
_hover(gd, { xval: 0 });
7762+
assertLabel({ title: 'A', items: ['bar : 1', 'scatter : 10'] });
7763+
})
7764+
.then(done, done.fail);
7765+
});
7766+
7767+
it('hover label should show the category under the cursor when a line point in the next category is within hoverdistance', (done) => {
7768+
// Categories 21px wide put the line point at 'c28' within the default hoverdistance of 'Other' (see #7152)
7769+
const x = Array.from({ length: 29 }, (_, i) => 'c' + i).concat('Other');
7770+
Plotly.newPlot(gd, {
7771+
data: [
7772+
{ name: 'bar', type: 'bar', x, y: x.map((_, i) => i + 1) },
7773+
{ name: 'line', mode: 'lines', x, y: x.map((_, i) => (i === 29 ? null : 40 - i)) }
7774+
],
7775+
layout: {
7776+
width: 700,
7777+
showlegend: false,
7778+
hovermode: 'x unified',
7779+
margin: { l: 0, r: 0 },
7780+
xaxis: { range: [-1.753, 31.588] }
7781+
}
76677782
})
7668-
.then(done, done.fail);
7783+
.then(() => {
7784+
_hover(gd, { xval: 29 });
7785+
assertLabel({ title: 'Other', items: ['bar : 30'] });
7786+
7787+
_hover(gd, { xval: 28.6 });
7788+
assertLabel({ title: 'Other', items: ['bar : 30'] });
7789+
7790+
_hover(gd, { xval: 28 });
7791+
assertLabel({ title: 'c28', items: ['bar : 29', 'line : 12'] });
7792+
})
7793+
.then(done, done.fail);
76697794
});
76707795
});
76717796

0 commit comments

Comments
 (0)