Skip to content

Commit 401f00f

Browse files
authored
Merge pull request #1107 from plotly/bar-hover-fix
Fix hover bar delta
2 parents 5acfb06 + 540c80b commit 401f00f

File tree

2 files changed

+93
-1
lines changed

2 files changed

+93
-1
lines changed

src/traces/bar/hover.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module.exports = function hoverPoints(pointData, xval, yval, hovermode) {
2222
ya = pointData.ya,
2323
barDelta = (hovermode === 'closest') ?
2424
t.barwidth / 2 :
25-
t.bargroupwidth,
25+
t.bargroupwidth / 2,
2626
barPos;
2727

2828
if(hovermode !== 'closest') barPos = function(di) { return di.p; };

test/jasmine/tests/bar_test.js

+92
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,98 @@ describe('A bar plot', function() {
676676
});
677677
});
678678

679+
describe('bar hover', function() {
680+
'use strict';
681+
682+
var gd;
683+
684+
beforeAll(function() {
685+
jasmine.addMatchers(customMatchers);
686+
});
687+
688+
afterEach(destroyGraphDiv);
689+
690+
function getPointData(gd) {
691+
var cd = gd.calcdata,
692+
subplot = gd._fullLayout._plots.xy;
693+
694+
return {
695+
index: false,
696+
distance: 20,
697+
cd: cd[0],
698+
trace: cd[0][0].trace,
699+
xa: subplot.xaxis,
700+
ya: subplot.yaxis
701+
};
702+
}
703+
704+
function _hover(gd, xval, yval, closest) {
705+
var pointData = getPointData(gd);
706+
var pt = Bar.hoverPoints(pointData, xval, yval, closest)[0];
707+
708+
return {
709+
style: [pt.index, pt.color, pt.xLabelVal, pt.yLabelVal],
710+
pos: [pt.x0, pt.x1, pt.y0, pt.y1]
711+
};
712+
}
713+
714+
function assertPos(actual, expected) {
715+
var TOL = 5;
716+
717+
actual.forEach(function(p, i) {
718+
expect(p).toBeWithin(expected[i], TOL);
719+
});
720+
}
721+
722+
describe('with orientation *v*', function() {
723+
beforeAll(function(done) {
724+
gd = createGraphDiv();
725+
726+
var mock = Lib.extendDeep({}, require('@mocks/11.json'));
727+
728+
Plotly.plot(gd, mock.data, mock.layout).then(done);
729+
});
730+
731+
it('should return the correct hover point data (case x)', function() {
732+
var out = _hover(gd, 0, 0, 'x');
733+
734+
expect(out.style).toEqual([0, 'rgb(255, 102, 97)', 0, 13.23]);
735+
assertPos(out.pos, [11.87, 106.8, 152.76, 152.76]);
736+
});
737+
738+
it('should return the correct hover point data (case closest)', function() {
739+
var out = _hover(gd, -0.2, 12, 'closest');
740+
741+
expect(out.style).toEqual([0, 'rgb(255, 102, 97)', 0, 13.23]);
742+
assertPos(out.pos, [11.87, 59.33, 152.76, 152.76]);
743+
});
744+
});
745+
746+
describe('with orientation *h*', function() {
747+
beforeAll(function(done) {
748+
gd = createGraphDiv();
749+
750+
var mock = Lib.extendDeep({}, require('@mocks/bar_attrs_group_norm.json'));
751+
752+
Plotly.plot(gd, mock.data, mock.layout).then(done);
753+
});
754+
755+
it('should return the correct hover point data (case y)', function() {
756+
var out = _hover(gd, 185.645, 0.15, 'y');
757+
758+
expect(out.style).toEqual([0, '#1f77b4', 75, 0]);
759+
assertPos(out.pos, [182.88, 182.88, 214.5, 170.5]);
760+
});
761+
762+
it('should return the correct hover point data (case closest)', function() {
763+
var out = _hover(gd, 135.88, -0.15, 'closest');
764+
765+
expect(out.style).toEqual([0, '#1f77b4', 75, 0]);
766+
assertPos(out.pos, [182.88, 182.88, 214.5, 192.5]);
767+
});
768+
});
769+
770+
});
679771

680772
function mockBarPlot(dataWithoutTraceType, layout) {
681773
var traceTemplate = { type: 'bar' };

0 commit comments

Comments
 (0)