-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathhover.js
91 lines (73 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
87
88
89
90
91
/**
* Copyright 2012-2019, 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 hoverLabelText = require('../../plots/cartesian/axes').hoverLabelText;
var opacity = require('../../components/color').opacity;
var hoverOnBars = require('../bar/hover').hoverOnBars;
var DIRSYMBOL = {
increasing: '▲',
decreasing: '▼'
};
module.exports = function hoverPoints(pointData, xval, yval, hovermode) {
var point = hoverOnBars(pointData, xval, yval, hovermode);
if(!point) return;
var cd = point.cd;
var trace = cd[0].trace;
var isHorizontal = (trace.orientation === 'h');
var vAxis = isHorizontal ? pointData.xa : pointData.ya;
function formatNumber(a) {
return hoverLabelText(vAxis, a);
}
// 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;
} else {
point.final = size;
point.initial = di.b;
point.delta = point.final - point.initial;
}
point.finalLabel = formatNumber(point.final);
point.deltaLabel = formatNumber(Math.abs(point.delta));
point.initialLabel = formatNumber(point.initial, 1);
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(hasFlag('final') && point.finalLabel !== '') {
text.push('Final: ' + point.finalLabel);
}
if(hasFlag('delta') && point.deltaLabel !== '') {
if(size < 0) {
text.push('(' + point.deltaLabel + ') ' + DIRSYMBOL.decreasing);
} else {
text.push(point.deltaLabel + ' ' + DIRSYMBOL.increasing);
}
}
if(hasFlag('initial') && point.initialLabel !== '') {
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;
}