Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/plots/cartesian/graph_interact.js
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ fx.getClosest = function(cd, distfn, pointData) {
// do this for 'closest'
for(var i=0; i<cd.length; i++) {
var newDistance = distfn(cd[i]);
if(newDistance < pointData.distance) {
if(newDistance <= pointData.distance) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What a fix! 👏

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, it's a little hacky. Ideally hoverinfo: 'none' traces wouldn't be passed to the hover label routine, but that caused a plotly_hover test to fail, so I decided to go with ⏫ .

pointData.index = i;
pointData.distance = newDistance;
}
Expand Down
64 changes: 64 additions & 0 deletions test/jasmine/tests/hover_label_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,70 @@ describe('hover info', function() {
});
});

describe('\'closest\' hover info (superimposed case)', function() {
var mockCopy = Lib.extendDeep({}, mock);

// superimposed traces
mockCopy.data.push(Lib.extendDeep({}, mockCopy.data[0]));
mockCopy.layout.hovermode = 'closest';

var gd;

beforeEach(function(done) {
gd = createGraphDiv();
Plotly.plot(gd, mockCopy.data, mockCopy.layout).then(done);
});

it('render hover labels of the above trace', function() {
Fx.hover('graph', evt, 'xy');

expect(gd._hoverdata.length).toEqual(1);

var hoverTrace = gd._hoverdata[0];

expect(hoverTrace.fullData.index).toEqual(1);
expect(hoverTrace.curveNumber).toEqual(1);
expect(hoverTrace.pointNumber).toEqual(16);
expect(hoverTrace.x).toEqual(0.33);
expect(hoverTrace.y).toEqual(1.25);

expect(d3.selectAll('g.axistext').size()).toEqual(0);
expect(d3.selectAll('g.hovertext').size()).toEqual(1);

var expectations = ['PV learning ...', '(0.33, 1.25)'];
d3.selectAll('g.hovertext').selectAll('text').each(function(_, i) {
expect(d3.select(this).html()).toEqual(expectations[i]);
});
});

it('render only non-hoverinfo \'none\' hover labels', function(done) {

Plotly.restyle(gd, 'hoverinfo', ['none', 'name']).then(function() {
Fx.hover('graph', evt, 'xy');

expect(gd._hoverdata.length).toEqual(1);

var hoverTrace = gd._hoverdata[0];

expect(hoverTrace.fullData.index).toEqual(1);
expect(hoverTrace.curveNumber).toEqual(1);
expect(hoverTrace.pointNumber).toEqual(16);
expect(hoverTrace.x).toEqual(0.33);
expect(hoverTrace.y).toEqual(1.25);

expect(d3.selectAll('g.axistext').size()).toEqual(0);
expect(d3.selectAll('g.hovertext').size()).toEqual(1);

var text = d3.selectAll('g.hovertext').select('text');
expect(text.size()).toEqual(1);
expect(text.html()).toEqual('PV learning ...');

done();
});

});
});

describe('hoverformat', function() {

var data = [{
Expand Down