|
| 1 | +/** |
| 2 | +* Copyright 2012-2015, Plotly, Inc. |
| 3 | +* All rights reserved. |
| 4 | +* |
| 5 | +* This source code is licensed under the MIT license found in the |
| 6 | +* LICENSE file in the root directory of this source tree. |
| 7 | +*/ |
| 8 | + |
| 9 | + |
| 10 | +'use strict'; |
| 11 | + |
| 12 | +var Plotly = require('../../plotly'); |
| 13 | +var Color = require('../../components/color'); |
| 14 | + |
| 15 | + |
| 16 | +module.exports = function hoverPoints(pointData, xval, yval, hovermode) { |
| 17 | + var cd = pointData.cd, |
| 18 | + trace = cd[0].trace, |
| 19 | + t = cd[0].t, |
| 20 | + xa = pointData.xa, |
| 21 | + ya = pointData.ya, |
| 22 | + barDelta = (hovermode==='closest') ? |
| 23 | + t.barwidth/2 : t.dbar*(1-xa._td._fullLayout.bargap)/2, |
| 24 | + barPos; |
| 25 | + |
| 26 | + if(hovermode!=='closest') barPos = function(di) { return di.p; }; |
| 27 | + else if(trace.orientation==='h') barPos = function(di) { return di.y; }; |
| 28 | + else barPos = function(di) { return di.x; }; |
| 29 | + |
| 30 | + var dx, dy; |
| 31 | + if(trace.orientation==='h') { |
| 32 | + dx = function(di){ |
| 33 | + // add a gradient so hovering near the end of a |
| 34 | + // bar makes it a little closer match |
| 35 | + return Plotly.Fx.inbox(di.b-xval, di.x-xval) + (di.x-xval)/(di.x-di.b); |
| 36 | + }; |
| 37 | + dy = function(di){ |
| 38 | + var centerPos = barPos(di) - yval; |
| 39 | + return Plotly.Fx.inbox(centerPos - barDelta, centerPos + barDelta); |
| 40 | + }; |
| 41 | + } |
| 42 | + else { |
| 43 | + dy = function(di){ |
| 44 | + return Plotly.Fx.inbox(di.b-yval, di.y-yval) + (di.y-yval)/(di.y-di.b); |
| 45 | + }; |
| 46 | + dx = function(di){ |
| 47 | + var centerPos = barPos(di) - xval; |
| 48 | + return Plotly.Fx.inbox(centerPos - barDelta, centerPos + barDelta); |
| 49 | + }; |
| 50 | + } |
| 51 | + |
| 52 | + var distfn = Plotly.Fx.getDistanceFunction(hovermode, dx, dy); |
| 53 | + Plotly.Fx.getClosest(cd, distfn, pointData); |
| 54 | + |
| 55 | + // skip the rest (for this trace) if we didn't find a close point |
| 56 | + if(pointData.index===false) return; |
| 57 | + |
| 58 | + // the closest data point |
| 59 | + var di = cd[pointData.index], |
| 60 | + mc = di.mcc || trace.marker.color, |
| 61 | + mlc = di.mlcc || trace.marker.line.color, |
| 62 | + mlw = di.mlw || trace.marker.line.width; |
| 63 | + if(Color.opacity(mc)) pointData.color = mc; |
| 64 | + else if(Color.opacity(mlc) && mlw) pointData.color = mlc; |
| 65 | + |
| 66 | + if(trace.orientation==='h') { |
| 67 | + pointData.x0 = pointData.x1 = xa.c2p(di.x, true); |
| 68 | + pointData.xLabelVal = di.s; |
| 69 | + |
| 70 | + pointData.y0 = ya.c2p(barPos(di) - barDelta, true); |
| 71 | + pointData.y1 = ya.c2p(barPos(di) + barDelta, true); |
| 72 | + pointData.yLabelVal = di.p; |
| 73 | + } |
| 74 | + else { |
| 75 | + pointData.y0 = pointData.y1 = ya.c2p(di.y,true); |
| 76 | + pointData.yLabelVal = di.s; |
| 77 | + |
| 78 | + pointData.x0 = xa.c2p(barPos(di) - barDelta, true); |
| 79 | + pointData.x1 = xa.c2p(barPos(di) + barDelta, true); |
| 80 | + pointData.xLabelVal = di.p; |
| 81 | + } |
| 82 | + |
| 83 | + if(di.tx) pointData.text = di.tx; |
| 84 | + |
| 85 | + Plotly.ErrorBars.hoverInfo(di, trace, pointData); |
| 86 | + |
| 87 | + return [pointData]; |
| 88 | +}; |
0 commit comments