|
9 | 9 |
|
10 | 10 | 'use strict';
|
11 | 11 |
|
| 12 | +var Lib = require('../../lib'); |
12 | 13 | var Fx = require('../../plots/cartesian/graph_interact');
|
13 | 14 | var ErrorBars = require('../../components/errorbars');
|
14 | 15 | var getTraceColor = require('./get_trace_color');
|
| 16 | +var Color = require('../../components/color'); |
15 | 17 |
|
16 | 18 |
|
17 | 19 | module.exports = function hoverPoints(pointData, xval, yval, hovermode) {
|
18 | 20 | var cd = pointData.cd,
|
19 | 21 | trace = cd[0].trace,
|
20 | 22 | xa = pointData.xa,
|
21 | 23 | ya = pointData.ya,
|
22 |
| - dx = function(di) { |
23 |
| - // scatter points: d.mrc is the calculated marker radius |
24 |
| - // adjust the distance so if you're inside the marker it |
25 |
| - // always will show up regardless of point size, but |
26 |
| - // prioritize smaller points |
27 |
| - var rad = Math.max(3, di.mrc || 0); |
28 |
| - return Math.max(Math.abs(xa.c2p(di.x) - xa.c2p(xval)) - rad, 1 - 3 / rad); |
29 |
| - }, |
30 |
| - dy = function(di) { |
31 |
| - var rad = Math.max(3, di.mrc || 0); |
32 |
| - return Math.max(Math.abs(ya.c2p(di.y) - ya.c2p(yval)) - rad, 1 - 3 / rad); |
33 |
| - }, |
34 |
| - dxy = function(di) { |
35 |
| - var rad = Math.max(3, di.mrc || 0), |
36 |
| - dx = Math.abs(xa.c2p(di.x) - xa.c2p(xval)), |
37 |
| - dy = Math.abs(ya.c2p(di.y) - ya.c2p(yval)); |
38 |
| - return Math.max(Math.sqrt(dx * dx + dy * dy) - rad, 1 - 3 / rad); |
39 |
| - }, |
40 |
| - distfn = Fx.getDistanceFunction(hovermode, dx, dy, dxy); |
41 |
| - |
42 |
| - Fx.getClosest(cd, distfn, pointData); |
43 |
| - |
44 |
| - // skip the rest (for this trace) if we didn't find a close point |
45 |
| - if(pointData.index === false) return; |
46 |
| - |
47 |
| - // the closest data point |
48 |
| - var di = cd[pointData.index], |
49 |
| - xc = xa.c2p(di.x, true), |
50 |
| - yc = ya.c2p(di.y, true), |
51 |
| - rad = di.mrc || 1; |
52 |
| - |
53 |
| - pointData.color = getTraceColor(trace, di); |
54 |
| - |
55 |
| - pointData.x0 = xc - rad; |
56 |
| - pointData.x1 = xc + rad; |
57 |
| - pointData.xLabelVal = di.x; |
58 |
| - |
59 |
| - pointData.y0 = yc - rad; |
60 |
| - pointData.y1 = yc + rad; |
61 |
| - pointData.yLabelVal = di.y; |
62 |
| - |
63 |
| - if(di.tx) pointData.text = di.tx; |
64 |
| - else if(trace.text) pointData.text = trace.text; |
65 |
| - |
66 |
| - ErrorBars.hoverInfo(di, trace, pointData); |
67 |
| - |
68 |
| - return [pointData]; |
| 24 | + xpx = xa.c2p(xval), |
| 25 | + ypx = ya.c2p(yval), |
| 26 | + pt = [xpx, ypx]; |
| 27 | + |
| 28 | + // even if hoveron is 'fills', only use it if we have polygons too |
| 29 | + if(trace.hoveron === 'fills' && trace._polygons) { |
| 30 | + var polygons = trace._polygons, |
| 31 | + inside = false, |
| 32 | + x0 = Infinity, |
| 33 | + x1 = -Infinity, |
| 34 | + y0 = Infinity, |
| 35 | + y1 = -Infinity; |
| 36 | + |
| 37 | + for(var i = 0; i < polygons.length; i++) { |
| 38 | + var polygon = polygons[i]; |
| 39 | + // TODO: this is not going to work right for curved edges, it will |
| 40 | + // act as though they're straight. That's probably going to need |
| 41 | + // the elements themselves to capture the events. Worth it? |
| 42 | + if(polygon.contains(pt)) { |
| 43 | + inside = !inside; |
| 44 | + // TODO: need better than just the overall bounding box |
| 45 | + x0 = Math.min(x0, polygon.xmin); |
| 46 | + x1 = Math.max(x1, polygon.xmax); |
| 47 | + y0 = Math.min(y0, polygon.ymin); |
| 48 | + y1 = Math.max(y1, polygon.ymax); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + if(inside) { |
| 53 | + // get only fill or line color for the hover color |
| 54 | + var color = Color.defaultLine; |
| 55 | + if(Color.opacity(trace.fillcolor)) color = trace.fillcolor; |
| 56 | + else if(Color.opacity((trace.line || {}).color)) { |
| 57 | + color = trace.line.color; |
| 58 | + } |
| 59 | + |
| 60 | + Lib.extendFlat(pointData, { |
| 61 | + // never let a 2D override 1D type as closest point |
| 62 | + distance: Fx.MAXDIST + 10, |
| 63 | + x0: x0, |
| 64 | + x1: x1, |
| 65 | + y0: y0, |
| 66 | + y1: y1, |
| 67 | + color: color |
| 68 | + }); |
| 69 | + |
| 70 | + delete pointData.index; |
| 71 | + return [pointData]; |
| 72 | + } |
| 73 | + } |
| 74 | + else { |
| 75 | + var dx = function(di) { |
| 76 | + // scatter points: d.mrc is the calculated marker radius |
| 77 | + // adjust the distance so if you're inside the marker it |
| 78 | + // always will show up regardless of point size, but |
| 79 | + // prioritize smaller points |
| 80 | + var rad = Math.max(3, di.mrc || 0); |
| 81 | + return Math.max(Math.abs(xa.c2p(di.x) - xa.c2p(xval)) - rad, 1 - 3 / rad); |
| 82 | + }, |
| 83 | + dy = function(di) { |
| 84 | + var rad = Math.max(3, di.mrc || 0); |
| 85 | + return Math.max(Math.abs(ya.c2p(di.y) - ya.c2p(yval)) - rad, 1 - 3 / rad); |
| 86 | + }, |
| 87 | + dxy = function(di) { |
| 88 | + var rad = Math.max(3, di.mrc || 0), |
| 89 | + dx = Math.abs(xa.c2p(di.x) - xa.c2p(xval)), |
| 90 | + dy = Math.abs(ya.c2p(di.y) - ya.c2p(yval)); |
| 91 | + return Math.max(Math.sqrt(dx * dx + dy * dy) - rad, 1 - 3 / rad); |
| 92 | + }, |
| 93 | + distfn = Fx.getDistanceFunction(hovermode, dx, dy, dxy); |
| 94 | + |
| 95 | + Fx.getClosest(cd, distfn, pointData); |
| 96 | + |
| 97 | + // skip the rest (for this trace) if we didn't find a close point |
| 98 | + if(pointData.index === false) return; |
| 99 | + |
| 100 | + // the closest data point |
| 101 | + var di = cd[pointData.index], |
| 102 | + xc = xa.c2p(di.x, true), |
| 103 | + yc = ya.c2p(di.y, true), |
| 104 | + rad = di.mrc || 1; |
| 105 | + |
| 106 | + pointData.color = getTraceColor(trace, di); |
| 107 | + |
| 108 | + pointData.x0 = xc - rad; |
| 109 | + pointData.x1 = xc + rad; |
| 110 | + pointData.xLabelVal = di.x; |
| 111 | + |
| 112 | + pointData.y0 = yc - rad; |
| 113 | + pointData.y1 = yc + rad; |
| 114 | + pointData.yLabelVal = di.y; |
| 115 | + |
| 116 | + if(di.tx) pointData.text = di.tx; |
| 117 | + else if(trace.text) pointData.text = trace.text; |
| 118 | + |
| 119 | + ErrorBars.hoverInfo(di, trace, pointData); |
| 120 | + |
| 121 | + return [pointData]; |
| 122 | + } |
69 | 123 | };
|
0 commit comments