Skip to content

Commit 4573f34

Browse files
committed
Output bounding box to hover event data
1 parent cfa7720 commit 4573f34

File tree

12 files changed

+84
-7
lines changed

12 files changed

+84
-7
lines changed

src/components/fx/helpers.js

+5
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,11 @@ exports.makeEventData = function(pt, trace, cd) {
138138
if('yVal' in pt) out.y = pt.yVal;
139139
else if('y' in pt) out.y = pt.y;
140140

141+
if ('x0' in pt) out.x0 = pt.x0;
142+
if ('x1' in pt) out.x1 = pt.x1;
143+
if ('y0' in pt) out.y0 = pt.y0;
144+
if ('y1' in pt) out.y1 = pt.y1;
145+
141146
if(pt.xa) out.xaxis = pt.xa;
142147
if(pt.ya) out.yaxis = pt.ya;
143148
if(pt.zLabelVal !== undefined) out.z = pt.zLabelVal;

src/components/fx/hover.js

+14
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,12 @@ function _hover(gd, evt, subplot, noHoverEvent) {
680680
var oldhoverdata = gd._hoverdata;
681681
var newhoverdata = [];
682682

683+
// Top/left hover offsets relative to graph div. As long as hover content is
684+
// a sibling of the graph div, it will be positioned correctly relative to
685+
// the offset parent, whatever that may be.
686+
var hot = gd.offsetTop + gd.clientTop;
687+
var hol = gd.offsetLeft + gd.clientLeft;
688+
683689
// pull out just the data that's useful to
684690
// other people and send it to the event
685691
for(itemnum = 0; itemnum < hoverData.length; itemnum++) {
@@ -694,6 +700,14 @@ function _hover(gd, evt, subplot, noHoverEvent) {
694700
pt.hovertemplate = ht || pt.trace.hovertemplate || false;
695701
}
696702

703+
var bbox = {};
704+
eventData.bbox = bbox;
705+
706+
if ('x0' in pt) bbox.x0 = hol + pt.x0 + pt.xa._offset;
707+
if ('x1' in pt) bbox.x1 = hol + pt.x1 + pt.xa._offset;
708+
if ('y0' in pt) bbox.y0 = hot + pt.y0 + pt.ya._offset;
709+
if ('y1' in pt) bbox.y1 = hot + pt.y1 + pt.ya._offset;
710+
697711
pt.eventData = [eventData];
698712
newhoverdata.push(eventData);
699713
}

src/traces/bar/event_data.js

+5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ module.exports = function eventData(out, pt, trace) {
77
if(pt.xa) out.xaxis = pt.xa;
88
if(pt.ya) out.yaxis = pt.ya;
99

10+
if ('x0' in pt) out.x0 = pt.x0;
11+
if ('x1' in pt) out.x1 = pt.x1;
12+
if ('y0' in pt) out.y0 = pt.y0;
13+
if ('y1' in pt) out.y1 = pt.y1;
14+
1015
if(trace.orientation === 'h') {
1116
out.label = out.y;
1217
out.value = out.x;

src/traces/box/event_data.js

+5
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,10 @@ module.exports = function eventData(out, pt) {
1111
if(pt.xa) out.xaxis = pt.xa;
1212
if(pt.ya) out.yaxis = pt.ya;
1313

14+
if ('x0' in pt) out.x0 = pt.x0;
15+
if ('x1' in pt) out.x1 = pt.x1;
16+
if ('y0' in pt) out.y0 = pt.y0;
17+
if ('y1' in pt) out.y1 = pt.y1;
18+
1419
return out;
1520
};

src/traces/funnel/event_data.js

+5
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,10 @@ module.exports = function eventData(out, pt /* , trace, cd, pointNumber */) {
1313
if(pt.xa) out.xaxis = pt.xa;
1414
if(pt.ya) out.yaxis = pt.ya;
1515

16+
if ('x0' in pt) out.x0 = pt.x0;
17+
if ('x1' in pt) out.x1 = pt.x1;
18+
if ('y0' in pt) out.y0 = pt.y0;
19+
if ('y1' in pt) out.y1 = pt.y1;
20+
1621
return out;
1722
};

src/traces/histogram/event_data.js

+5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ module.exports = function eventData(out, pt, trace, cd, pointNumber) {
1111
if(pt.xa) out.xaxis = pt.xa;
1212
if(pt.ya) out.yaxis = pt.ya;
1313

14+
if ('x0' in pt) out.x0 = pt.x0;
15+
if ('x1' in pt) out.x1 = pt.x1;
16+
if ('y0' in pt) out.y0 = pt.y0;
17+
if ('y1' in pt) out.y1 = pt.y1;
18+
1419
// specific to histogram - CDFs do not have pts (yet?)
1520
if(!(trace.cumulative || {}).enabled) {
1621
var pts = Array.isArray(pointNumber) ?

src/traces/image/event_data.js

+6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
module.exports = function eventData(out, pt) {
44
if('xVal' in pt) out.x = pt.xVal;
55
if('yVal' in pt) out.y = pt.yVal;
6+
7+
if ('x0' in pt) out.x0 = pt.x0;
8+
if ('x1' in pt) out.x1 = pt.x1;
9+
if ('y0' in pt) out.y0 = pt.y0;
10+
if ('y1' in pt) out.y1 = pt.y1;
11+
612
if(pt.xa) out.xaxis = pt.xa;
713
if(pt.ya) out.yaxis = pt.ya;
814
out.color = pt.color;

src/traces/pie/event_data.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,16 @@ module.exports = function eventData(pt, trace) {
1818
text: pt.text,
1919

2020
// pt.v (and pt.i below) for backward compatibility
21-
v: pt.v
21+
v: pt.v,
22+
23+
// TODO: These coordinates aren't quite correct and don't take into account some offset
24+
// I still haven't quite located (similar to xa._offset)
25+
bbox: {
26+
x0: pt.x0,
27+
x1: pt.x1,
28+
y0: pt.y0,
29+
y1: pt.y1,
30+
},
2231
};
2332

2433
// Only include pointNumber if it's unambiguous

src/traces/pie/plot.js

+14-6
Original file line numberDiff line numberDiff line change
@@ -379,12 +379,20 @@ function attachFxHandlers(sliceTop, gd, cd) {
379379

380380
if(hoverinfo === 'all') hoverinfo = 'label+text+value+percent+name';
381381

382+
// If hoverinfo === 'none', we still want the *coordinates* of hover to be
383+
// output, just not the hover to actually display
384+
var rInscribed = pt.rInscribed || 0;
385+
var hoverCenterX = cx + pt.pxmid[0] * (1 - rInscribed);
386+
var hoverCenterY = cy + pt.pxmid[1] * (1 - rInscribed);
387+
pt.x0 = hoverCenterX - rInscribed * cd0.r;
388+
pt.x1 = hoverCenterX + rInscribed * cd0.r;
389+
pt.y0 = hoverCenterY;
390+
pt.y1 = hoverCenterY;
391+
382392
// in case we dragged over the pie from another subplot,
383393
// or if hover is turned off
384394
if(trace2.hovertemplate || (hoverinfo !== 'none' && hoverinfo !== 'skip' && hoverinfo)) {
385-
var rInscribed = pt.rInscribed || 0;
386-
var hoverCenterX = cx + pt.pxmid[0] * (1 - rInscribed);
387-
var hoverCenterY = cy + pt.pxmid[1] * (1 - rInscribed);
395+
388396
var separators = fullLayout2.separators;
389397
var text = [];
390398

@@ -406,9 +414,9 @@ function attachFxHandlers(sliceTop, gd, cd) {
406414

407415
Fx.loneHover({
408416
trace: trace,
409-
x0: hoverCenterX - rInscribed * cd0.r,
410-
x1: hoverCenterX + rInscribed * cd0.r,
411-
y: hoverCenterY,
417+
x0: pt.x0,
418+
x1: pt.x1,
419+
y: pt.y0,
412420
text: text.join('<br>'),
413421
name: (trace2.hovertemplate || hoverinfo.indexOf('name') !== -1) ? trace2.name : undefined,
414422
idealAlign: pt.pxmid[0] < 0 ? 'left' : 'right',

src/traces/scattercarpet/event_data.js

+5
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,10 @@ module.exports = function eventData(out, pt, trace, cd, pointNumber) {
77
out.b = cdi.b;
88
out.y = cdi.y;
99

10+
if ('x0' in pt) out.x0 = pt.x0;
11+
if ('x1' in pt) out.x1 = pt.x1;
12+
if ('y0' in pt) out.y0 = pt.y0;
13+
if ('y1' in pt) out.y1 = pt.y1;
14+
1015
return out;
1116
};

src/traces/scatterternary/event_data.js

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ module.exports = function eventData(out, pt, trace, cd, pointNumber) {
44
if(pt.xa) out.xaxis = pt.xa;
55
if(pt.ya) out.yaxis = pt.ya;
66

7+
if ('x0' in pt) out.x0 = pt.x0;
8+
if ('x1' in pt) out.x1 = pt.x1;
9+
if ('y0' in pt) out.y0 = pt.y0;
10+
if ('y1' in pt) out.y1 = pt.y1;
11+
712
if(cd[pointNumber]) {
813
var cdi = cd[pointNumber];
914

src/traces/waterfall/event_data.js

+5
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,10 @@ module.exports = function eventData(out, pt /* , trace, cd, pointNumber */) {
1313
if(pt.xa) out.xaxis = pt.xa;
1414
if(pt.ya) out.yaxis = pt.ya;
1515

16+
if ('x0' in pt) out.x0 = pt.x0;
17+
if ('x1' in pt) out.x1 = pt.x1;
18+
if ('y0' in pt) out.y0 = pt.y0;
19+
if ('y1' in pt) out.y1 = pt.y1;
20+
1621
return out;
1722
};

0 commit comments

Comments
 (0)