-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathhover.js
86 lines (69 loc) · 2.6 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
'use strict';
var hoverLabelText = require('../../plots/cartesian/axes').hoverLabelText;
var opacity = require('../../components/color').opacity;
var hoverOnBars = require('../bar/hover').hoverOnBars;
var delta = require('../../constants/delta.js');
var DIRSYMBOL = {
increasing: delta.INCREASING.SYMBOL,
decreasing: delta.DECREASING.SYMBOL
};
module.exports = function hoverPoints(pointData, xval, yval, hovermode, opts) {
var point = hoverOnBars(pointData, xval, yval, hovermode, opts);
if(!point) return;
var cd = point.cd;
var trace = cd[0].trace;
var isHorizontal = (trace.orientation === 'h');
var vLetter = isHorizontal ? 'x' : 'y';
var vAxis = isHorizontal ? pointData.xa : pointData.ya;
function formatNumber(a) {
return hoverLabelText(vAxis, a, trace[vLetter + 'hoverformat']);
}
// the closest data point
var index = point.index;
var di = cd[index];
var size = (di.isSum) ? di.b + di.s : di.rawS;
if(!di.isSum) {
point.initial = di.b + di.s - size;
point.delta = size;
point.final = point.initial + point.delta;
var v = formatNumber(Math.abs(point.delta));
point.deltaLabel = size < 0 ? '(' + v + ')' : v;
point.finalLabel = formatNumber(point.final);
point.initialLabel = formatNumber(point.initial);
}
var hoverinfo = di.hi || trace.hoverinfo;
var text = [];
if(hoverinfo && hoverinfo !== 'none' && hoverinfo !== 'skip') {
var isAll = (hoverinfo === 'all');
var parts = hoverinfo.split('+');
var hasFlag = function(flag) { return isAll || parts.indexOf(flag) !== -1; };
if(!di.isSum) {
if(hasFlag('final') &&
(isHorizontal ? !hasFlag('x') : !hasFlag('y')) // don't display redundant info.
) {
text.push(point.finalLabel);
}
if(hasFlag('delta')) {
if(size < 0) {
text.push(point.deltaLabel + ' ' + DIRSYMBOL.decreasing);
} else {
text.push(point.deltaLabel + ' ' + DIRSYMBOL.increasing);
}
}
if(hasFlag('initial')) {
text.push('Initial: ' + point.initialLabel);
}
}
}
if(text.length) point.extraText = text.join('<br>');
point.color = getTraceColor(trace, di);
return [point];
};
function getTraceColor(trace, di) {
var cont = trace[di.dir].marker;
var mc = cont.color;
var mlc = cont.line.color;
var mlw = cont.line.width;
if(opacity(mc)) return mc;
else if(opacity(mlc) && mlw) return mlc;
}