|
| 1 | +/** |
| 2 | +* Copyright 2012-2017, 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 | +'use strict'; |
| 10 | + |
| 11 | +var constants = require('./constants'); |
| 12 | + |
| 13 | +// look for either subplot or xaxis and yaxis attributes |
| 14 | +exports.getSubplot = function getSubplot(trace) { |
| 15 | + return trace.subplot || (trace.xaxis + trace.yaxis) || trace.geo; |
| 16 | +}; |
| 17 | + |
| 18 | +// convenience functions for mapping all relevant axes |
| 19 | +exports.flat = function flat(subplots, v) { |
| 20 | + var out = []; |
| 21 | + for(var i = subplots.length; i > 0; i--) out.push(v); |
| 22 | + return out; |
| 23 | +}; |
| 24 | + |
| 25 | +exports.p2c = function p2c(axArray, v) { |
| 26 | + var out = []; |
| 27 | + for(var i = 0; i < axArray.length; i++) out.push(axArray[i].p2c(v)); |
| 28 | + return out; |
| 29 | +}; |
| 30 | + |
| 31 | +exports.getDistanceFunction = function getDistanceFunction(mode, dx, dy, dxy) { |
| 32 | + if(mode === 'closest') return dxy || quadrature(dx, dy); |
| 33 | + return mode === 'x' ? dx : dy; |
| 34 | +}; |
| 35 | + |
| 36 | +exports.getClosest = function getClosest(cd, distfn, pointData) { |
| 37 | + // do we already have a point number? (array mode only) |
| 38 | + if(pointData.index !== false) { |
| 39 | + if(pointData.index >= 0 && pointData.index < cd.length) { |
| 40 | + pointData.distance = 0; |
| 41 | + } |
| 42 | + else pointData.index = false; |
| 43 | + } |
| 44 | + else { |
| 45 | + // apply the distance function to each data point |
| 46 | + // this is the longest loop... if this bogs down, we may need |
| 47 | + // to create pre-sorted data (by x or y), not sure how to |
| 48 | + // do this for 'closest' |
| 49 | + for(var i = 0; i < cd.length; i++) { |
| 50 | + var newDistance = distfn(cd[i]); |
| 51 | + if(newDistance <= pointData.distance) { |
| 52 | + pointData.index = i; |
| 53 | + pointData.distance = newDistance; |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + return pointData; |
| 58 | +}; |
| 59 | + |
| 60 | +// for bar charts and others with finite-size objects: you must be inside |
| 61 | +// it to see its hover info, so distance is infinite outside. |
| 62 | +// But make distance inside be at least 1/4 MAXDIST, and a little bigger |
| 63 | +// for bigger bars, to prioritize scatter and smaller bars over big bars |
| 64 | +// |
| 65 | +// note that for closest mode, two inbox's will get added in quadrature |
| 66 | +// args are (signed) difference from the two opposite edges |
| 67 | +// count one edge as in, so that over continuous ranges you never get a gap |
| 68 | +exports.inbox = function inbox(v0, v1) { |
| 69 | + if(v0 * v1 < 0 || v0 === 0) { |
| 70 | + return constants.MAXDIST * (0.6 - 0.3 / Math.max(3, Math.abs(v0 - v1))); |
| 71 | + } |
| 72 | + return Infinity; |
| 73 | +}; |
| 74 | + |
| 75 | +function quadrature(dx, dy) { |
| 76 | + return function(di) { |
| 77 | + var x = dx(di), |
| 78 | + y = dy(di); |
| 79 | + return Math.sqrt(x * x + y * y); |
| 80 | + }; |
| 81 | +} |
0 commit comments