-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathcalc.js
96 lines (76 loc) · 2.71 KB
/
calc.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
'use strict';
var Axes = require('../../plots/cartesian/axes');
var alignPeriod = require('../../plots/cartesian/align_period');
var arraysToCalcdata = require('./arrays_to_calcdata');
var calcSelection = require('../scatter/calc_selection');
var BADNUM = require('../../constants/numerical').BADNUM;
module.exports = function calc(gd, trace) {
var xa = Axes.getFromId(gd, trace.xaxis || 'x');
var ya = Axes.getFromId(gd, trace.yaxis || 'y');
var size, pos, origPos, pObj, hasPeriod, i, cdi;
if(trace.orientation === 'h') {
size = xa.makeCalcdata(trace, 'x');
origPos = ya.makeCalcdata(trace, 'y');
pObj = alignPeriod(trace, ya, 'y', origPos);
hasPeriod = !!trace.yperiodalignment;
} else {
size = ya.makeCalcdata(trace, 'y');
origPos = xa.makeCalcdata(trace, 'x');
pObj = alignPeriod(trace, xa, 'x', origPos);
hasPeriod = !!trace.xperiodalignment;
}
pos = pObj.vals;
// create the "calculated data" to plot
var serieslen = Math.min(pos.length, size.length);
var cd = new Array(serieslen);
// Unlike other bar-like traces funnels do not support base attribute.
// bases for funnels are computed internally in a way that
// the mid-point of each bar are located on the axis line.
trace._base = [];
// set position and size
for(i = 0; i < serieslen; i++) {
// treat negative values as bad numbers
if(size[i] < 0) size[i] = BADNUM;
var connectToNext = false;
if(size[i] !== BADNUM) {
if(i + 1 < serieslen && size[i + 1] !== BADNUM) {
connectToNext = true;
}
}
cdi = cd[i] = {
p: pos[i],
s: size[i],
cNext: connectToNext
};
trace._base[i] = -0.5 * cdi.s;
if(hasPeriod) {
cd[i].orig_p = origPos[i]; // used by hover
cd[i].pEnd = pObj.ends[i];
cd[i].pStart = pObj.starts[i];
}
if(trace.ids) {
cdi.id = String(trace.ids[i]);
}
// calculate total values
if(i === 0) cd[0].vTotal = 0;
cd[0].vTotal += fixNum(cdi.s);
// ratio from initial value
cdi.begR = fixNum(cdi.s) / fixNum(cd[0].s);
}
var prevGoodNum;
for(i = 0; i < serieslen; i++) {
cdi = cd[i];
if(cdi.s === BADNUM) continue;
// ratio of total value
cdi.sumR = cdi.s / cd[0].vTotal;
// ratio of previous (good) value
cdi.difR = (prevGoodNum !== undefined) ? cdi.s / prevGoodNum : 1;
prevGoodNum = cdi.s;
}
arraysToCalcdata(cd, trace);
calcSelection(cd, trace);
return cd;
};
function fixNum(a) {
return (a === BADNUM) ? 0 : a;
}