|
| 1 | +/** |
| 2 | +* Copyright 2012-2019, 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 Fx = require('../../components/fx'); |
| 12 | +var Lib = require('../../lib'); |
| 13 | +var constants = require('./constants'); |
| 14 | + |
| 15 | +// var Axes = require('../../plots/cartesian/axes'); |
| 16 | + |
| 17 | +module.exports = function hoverPoints(pointData, xval, yval) { |
| 18 | + var cd0 = pointData.cd[0]; |
| 19 | + var trace = cd0.trace; |
| 20 | + var xa = pointData.xa; |
| 21 | + var ya = pointData.ya; |
| 22 | + |
| 23 | + // Return early if not on image |
| 24 | + if(Fx.inbox(xval - cd0.x0, xval - (cd0.x0 + cd0.w * trace.dx), 0) > 0 || |
| 25 | + Fx.inbox(yval - cd0.y0, yval - (cd0.y0 + cd0.h * trace.dy), 0) > 0) { |
| 26 | + return; |
| 27 | + } |
| 28 | + |
| 29 | + // Find nearest pixel's index and pixel center |
| 30 | + var nx = Math.floor((xval - cd0.x0) / trace.dx); |
| 31 | + var ny = Math.floor(Math.abs(yval - cd0.y0) / trace.dy); |
| 32 | + |
| 33 | + var hoverinfo = cd0.hi || trace.hoverinfo; |
| 34 | + var fmtColor; |
| 35 | + if(hoverinfo) { |
| 36 | + var parts = hoverinfo.split('+'); |
| 37 | + if(parts.indexOf('all') !== -1) parts = ['color']; |
| 38 | + if(parts.indexOf('color') !== -1) fmtColor = true; |
| 39 | + } |
| 40 | + |
| 41 | + var colormodel = trace.colormodel; |
| 42 | + var dims = colormodel.length; |
| 43 | + var c = trace._scaler(cd0.z[ny][nx]); |
| 44 | + var s = constants.colormodel[colormodel].suffix; |
| 45 | + |
| 46 | + var colorstring = []; |
| 47 | + if(trace.hovertemplate || fmtColor) { |
| 48 | + colorstring.push('[' + [c[0] + s[0], c[1] + s[1], c[2] + s[2]].join(', ')); |
| 49 | + if(dims === 4) colorstring.push(', ' + c[3] + s[3]); |
| 50 | + colorstring.push(']'); |
| 51 | + colorstring = colorstring.join(''); |
| 52 | + pointData.extraText = '<span style="text-transform:uppercase">' + colormodel + '</span>: ' + colorstring; |
| 53 | + } |
| 54 | + |
| 55 | + var py = ya.c2p(cd0.y0 + (ny + 0.5) * trace.dy); |
| 56 | + var xVal = cd0.x0 + (nx + 0.5) * trace.dx; |
| 57 | + var yVal = cd0.y0 + (ny + 0.5) * trace.dy; |
| 58 | + var zLabel = '[' + cd0.z[ny][nx].slice(0, trace.colormodel.length).join(', ') + ']'; |
| 59 | + return [Lib.extendFlat(pointData, { |
| 60 | + index: [ny, nx], |
| 61 | + x0: xa.c2p(cd0.x0 + nx * trace.dx), |
| 62 | + x1: xa.c2p(cd0.x0 + (nx + 1) * trace.dx), |
| 63 | + y0: py, |
| 64 | + y1: py, |
| 65 | + c: c, |
| 66 | + xVal: xVal, |
| 67 | + xLabelVal: xVal, |
| 68 | + yVal: yVal, |
| 69 | + yLabelVal: yVal, |
| 70 | + zLabelVal: zLabel, |
| 71 | + hovertemplateLabels: { |
| 72 | + 'zLabel': zLabel, |
| 73 | + 'cLabel': colorstring, |
| 74 | + 'c[0]Label': c[0] + s[0], |
| 75 | + 'c[1]Label': c[1] + s[1], |
| 76 | + 'c[2]Label': c[2] + s[2] |
| 77 | + } |
| 78 | + })]; |
| 79 | +}; |
0 commit comments