Skip to content

Commit

Permalink
Merge pull request #495 from plotly/hover-bug
Browse files Browse the repository at this point in the history
Fix hovermode 'closest'  + hoverinfo 'none' with superimposed data bug
  • Loading branch information
etpinard committed May 2, 2016
2 parents d87fc35 + 416fa8b commit 1c79eaa
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
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) {
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

0 comments on commit 1c79eaa

Please sign in to comment.