Skip to content

Commit c84e2a9

Browse files
committed
generalize bar delta computation
- so that traces with set 'width' (array or scalar) use the correct bar span.
1 parent 7a4154f commit c84e2a9

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

src/traces/bar/hover.js

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,35 +19,38 @@ module.exports = function hoverPoints(pointData, xval, yval, hovermode) {
1919
trace = cd[0].trace,
2020
t = cd[0].t,
2121
xa = pointData.xa,
22-
ya = pointData.ya,
23-
barDelta = (hovermode === 'closest') ?
24-
t.barwidth / 2 :
25-
t.bargroupwidth / 2,
26-
barPos;
22+
ya = pointData.ya;
2723

24+
var barPos;
2825
if(hovermode !== 'closest') barPos = function(di) { return di.p; };
2926
else if(trace.orientation === 'h') barPos = function(di) { return di.y; };
3027
else barPos = function(di) { return di.x; };
3128

29+
var barDelta = (hovermode === 'closest') ?
30+
function(i) { return (t.barwidth[i] || t.barwidth) / 2; } :
31+
function() { return t.bargroupwidth / 2; };
32+
3233
var dx, dy;
3334
if(trace.orientation === 'h') {
3435
dx = function(di) {
3536
// add a gradient so hovering near the end of a
3637
// bar makes it a little closer match
3738
return Fx.inbox(di.b - xval, di.x - xval) + (di.x - xval) / (di.x - di.b);
3839
};
39-
dy = function(di) {
40+
dy = function(di, i) {
4041
var centerPos = barPos(di) - yval;
41-
return Fx.inbox(centerPos - barDelta, centerPos + barDelta);
42+
var delta = barDelta(i);
43+
return Fx.inbox(centerPos - delta, centerPos + delta);
4244
};
4345
}
4446
else {
4547
dy = function(di) {
4648
return Fx.inbox(di.b - yval, di.y - yval) + (di.y - yval) / (di.y - di.b);
4749
};
48-
dx = function(di) {
50+
dx = function(di, i) {
4951
var centerPos = barPos(di) - xval;
50-
return Fx.inbox(centerPos - barDelta, centerPos + barDelta);
52+
var delta = barDelta(i);
53+
return Fx.inbox(centerPos - delta, centerPos + delta);
5154
};
5255
}
5356

@@ -58,7 +61,8 @@ module.exports = function hoverPoints(pointData, xval, yval, hovermode) {
5861
if(pointData.index === false) return;
5962

6063
// the closest data point
61-
var di = cd[pointData.index],
64+
var index = pointData.index,
65+
di = cd[index],
6266
mc = di.mcc || trace.marker.color,
6367
mlc = di.mlcc || trace.marker.line.color,
6468
mlw = di.mlw || trace.marker.line.width;
@@ -70,16 +74,16 @@ module.exports = function hoverPoints(pointData, xval, yval, hovermode) {
7074
pointData.x0 = pointData.x1 = xa.c2p(di.x, true);
7175
pointData.xLabelVal = size;
7276

73-
pointData.y0 = ya.c2p(barPos(di) - barDelta, true);
74-
pointData.y1 = ya.c2p(barPos(di) + barDelta, true);
77+
pointData.y0 = ya.c2p(barPos(di) - barDelta(index), true);
78+
pointData.y1 = ya.c2p(barPos(di) + barDelta(index), true);
7579
pointData.yLabelVal = di.p;
7680
}
7781
else {
7882
pointData.y0 = pointData.y1 = ya.c2p(di.y, true);
7983
pointData.yLabelVal = size;
8084

81-
pointData.x0 = xa.c2p(barPos(di) - barDelta, true);
82-
pointData.x1 = xa.c2p(barPos(di) + barDelta, true);
85+
pointData.x0 = xa.c2p(barPos(di) - barDelta(index), true);
86+
pointData.x1 = xa.c2p(barPos(di) + barDelta(index), true);
8387
pointData.xLabelVal = di.p;
8488
}
8589

0 commit comments

Comments
 (0)