-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathhover.js
109 lines (79 loc) · 2.88 KB
/
hover.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Fx = require('../../plots/cartesian/graph_interact');
var Axes = require('../../plots/cartesian/axes');
var BADNUM = require('../../constants/numerical').BADNUM;
var getTraceColor = require('../scatter/get_trace_color');
var attributes = require('./attributes');
module.exports = function hoverPoints(pointData) {
var cd = pointData.cd,
trace = cd[0].trace,
xa = pointData.xa,
ya = pointData.ya,
geo = pointData.subplot;
function c2p(lonlat) {
return geo.projection(lonlat);
}
function distFn(d) {
var lonlat = d.lonlat;
if(lonlat[0] === BADNUM) return Infinity;
if(geo.isLonLatOverEdges(lonlat)) return Infinity;
var pos = c2p(lonlat);
var xPx = xa.c2p(),
yPx = ya.c2p();
var dx = Math.abs(xPx - pos[0]),
dy = Math.abs(yPx - pos[1]),
rad = Math.max(3, d.mrc || 0);
// N.B. d.mrc is the calculated marker radius
// which is only set for trace with 'markers' mode.
return Math.max(Math.sqrt(dx * dx + dy * dy) - rad, 1 - 3 / rad);
}
Fx.getClosest(cd, distFn, pointData);
// skip the rest (for this trace) if we didn't find a close point
if(pointData.index === false) return;
var di = cd[pointData.index],
lonlat = di.lonlat,
pos = c2p(lonlat),
rad = di.mrc || 1;
pointData.x0 = pos[0] - rad;
pointData.x1 = pos[0] + rad;
pointData.y0 = pos[1] - rad;
pointData.y1 = pos[1] + rad;
pointData.loc = di.loc;
pointData.lat = lonlat[0];
pointData.lon = lonlat[1];
pointData.color = getTraceColor(trace, di);
pointData.extraText = getExtraText(trace, di, geo.mockAxis);
return [pointData];
};
function getExtraText(trace, pt, axis) {
var hoverinfo = trace.hoverinfo;
var parts = (hoverinfo === 'all') ?
attributes.hoverinfo.flags :
hoverinfo.split('+');
var hasLocation = parts.indexOf('location') !== -1 && Array.isArray(trace.locations),
hasLon = (parts.indexOf('lon') !== -1),
hasLat = (parts.indexOf('lat') !== -1),
hasText = (parts.indexOf('text') !== -1);
var text = [];
function format(val) {
return Axes.tickText(axis, axis.c2l(val), 'hover').text + '\u00B0';
}
if(hasLocation) text.push(pt.loc);
else if(hasLon && hasLat) {
text.push('(' + format(pt.lonlat[0]) + ', ' + format(pt.lonlat[1]) + ')');
}
else if(hasLon) text.push('lon: ' + format(pt.lonlat[0]));
else if(hasLat) text.push('lat: ' + format(pt.lonlat[1]));
if(hasText) {
var tx = pt.tx || trace.text;
if(!Array.isArray(tx)) text.push(tx);
}
return text.join('<br>');
}