|
8 | 8 |
|
9 | 9 | 'use strict';
|
10 | 10 |
|
11 |
| -var barHover = require('../bar/hover'); |
12 |
| -var makeHoverPointText = require('../scatterpolar/hover').makeHoverPointText; |
13 |
| -var Color = require('../../components/color') |
14 | 11 | var Fx = require('../../components/fx');
|
15 | 12 | var Lib = require('../../lib');
|
| 13 | +var getTraceColor = require('../bar/hover').getTraceColor; |
| 14 | +var makeHoverPointText = require('../scatterpolar/hover').makeHoverPointText; |
16 | 15 | var isPtInsidePolygon = require('../../plots/polar/helpers').isPtInsidePolygon;
|
17 | 16 |
|
18 |
| -module.exports = function hoverPoints(pointData, xval, yval, hovermode) { |
| 17 | +module.exports = function hoverPoints(pointData, xval, yval) { |
19 | 18 | var cd = pointData.cd;
|
20 | 19 | var trace = cd[0].trace;
|
21 |
| - var t = cd[0].t; |
22 | 20 |
|
23 | 21 | var subplot = pointData.subplot;
|
24 | 22 | var radialAxis = subplot.radialAxis;
|
25 | 23 | var angularAxis = subplot.angularAxis;
|
26 |
| - var xa = subplot.xaxis; |
27 |
| - var ya = subplot.yaxis; |
28 | 24 | var inboxFn = subplot.vangles ? isPtInsidePolygon : Lib.isPtInsideSector;
|
| 25 | + var maxHoverDistance = pointData.maxHoverDistance; |
| 26 | + var period = angularAxis._period || 2 * Math.PI; |
29 | 27 |
|
| 28 | + // polar.(x|y)axis.p2c doesn't get the reversed radial axis range case right |
30 | 29 | if(radialAxis.range[0] > radialAxis.range[1]) {
|
31 |
| - xval = -xval; |
32 |
| - yval = -yval; |
| 30 | + xval *= -1; |
| 31 | + yval *= -1; |
33 | 32 | }
|
34 | 33 |
|
35 | 34 | var rVal = Math.abs(radialAxis.g2p(Math.sqrt(xval * xval + yval * yval)));
|
36 | 35 | var thetaVal = Math.atan2(yval, xval);
|
37 | 36 |
|
38 |
| - // TODO add padding around sector to show labels, |
39 |
| - // when hovering "close to" them |
40 | 37 | var distFn = function(di) {
|
41 |
| - var rBnds = [di.s0, di.s1].map(radialAxis.c2p); |
42 |
| - var thetaBnds = [di.p0, di.p1].map(angularAxis.c2g).map(Lib.rad2deg); |
43 |
| - return inboxFn(rVal, thetaVal, rBnds, thetaBnds, subplot.vangles) ? |
44 |
| - 1 : |
45 |
| - Infinity; |
| 38 | + if(inboxFn(rVal, thetaVal, |
| 39 | + [di.rp0, di.rp1], |
| 40 | + [di.thetag0, di.thetag1].map(Lib.rad2deg), |
| 41 | + subplot.vangles) |
| 42 | + ) { |
| 43 | + return maxHoverDistance + |
| 44 | + // add a little to the pseudo-distance for wider bars, so that like scatter, |
| 45 | + // if you are over two overlapping bars, the narrower one wins. |
| 46 | + Math.min(1, Math.abs(di.thetag1 - di.thetag0) / period) - 1 + |
| 47 | + // add a gradient so hovering near the end of a |
| 48 | + // bar makes it a little closer match |
| 49 | + (di.rp1 - rVal) / (di.rp1 - di.rp0) - 1; |
| 50 | + } else { |
| 51 | + return Infinity; |
| 52 | + } |
46 | 53 | };
|
47 | 54 |
|
48 | 55 | Fx.getClosest(cd, distFn, pointData);
|
49 |
| - |
50 |
| - // skip the rest (for this trace) if we didn't find a close point |
51 | 56 | if(pointData.index === false) return;
|
52 | 57 |
|
53 | 58 | var index = pointData.index;
|
54 | 59 | var cdi = cd[index];
|
55 |
| - var rg = radialAxis.c2g(cdi.s1); |
56 |
| - // TODO include offset here? |
57 |
| - var thetag = angularAxis.c2g(cdi.p); |
58 |
| - var xp = xa.c2p(rg * Math.cos(thetag)); |
59 |
| - var yp = ya.c2p(rg * Math.sin(thetag)); |
60 | 60 |
|
61 |
| - // TODO use 'extents' like in Bar.hover? |
62 |
| - pointData.x0 = pointData.x1 = xp; |
63 |
| - pointData.y0 = pointData.y1 = yp; |
| 61 | + pointData.x0 = pointData.x1 = cdi.ct[0]; |
| 62 | + pointData.y0 = pointData.y1 = cdi.ct[1]; |
64 | 63 |
|
65 | 64 | var _cdi = Lib.extendFlat({}, cdi, {r: cdi.s, theta: cdi.p});
|
66 | 65 | pointData.extraText = makeHoverPointText(_cdi, trace, subplot);
|
67 |
| - pointData.xLabelVal = undefined; |
68 |
| - pointData.yLabelVal = undefined; |
69 |
| - |
70 |
| - // TODO DRY-up with Bar.hover |
71 |
| - var mc = cdi.mcc || trace.marker.color; |
72 |
| - var mlc = cdi.mlcc || trace.marker.line.color; |
73 |
| - var mlw = cdi.mlw || trace.marker.line.width; |
74 |
| - if(Color.opacity(mc)) pointData.color = mc; |
75 |
| - else if(Color.opacity(mlc) && mlw) pointData.color = mlc; |
| 66 | + pointData.color = getTraceColor(trace, cdi); |
| 67 | + pointData.xLabelVal = pointData.yLabelVal = undefined; |
76 | 68 |
|
77 | 69 | return [pointData];
|
78 | 70 | };
|
0 commit comments