-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathcalc.js
198 lines (164 loc) · 5.11 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/**
* 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 isNumeric = require('fast-isnumeric');
var Lib = require('../../lib');
var Axes = require('../../plots/cartesian/axes');
// outlier definition based on http://www.physics.csbsju.edu/stats/box2.html
module.exports = function calc(gd, trace) {
var fullLayout = gd._fullLayout;
var xa = Axes.getFromId(gd, trace.xaxis || 'x');
var ya = Axes.getFromId(gd, trace.yaxis || 'y');
var orientation = trace.orientation;
var cd = [];
var numKey = '_numBoxes';
var i;
var valAxis, valLetter;
var posAxis, posLetter;
if(orientation === 'h') {
valAxis = xa;
valLetter = 'x';
posAxis = ya;
posLetter = 'y';
} else {
valAxis = ya;
valLetter = 'y';
posAxis = xa;
posLetter = 'x';
}
var val = valAxis.makeCalcdata(trace, valLetter);
var pos = getPos(trace, posLetter, posAxis, val, fullLayout[numKey]);
var dv = Lib.distinctVals(pos);
var posDistinct = dv.vals;
var dPos = dv.minDiff / 2;
var posBins = makeBins(posDistinct, dPos);
var vLen = val.length;
var pLen = posDistinct.length;
var ptsPerBin = initNestedArray(pLen);
// bin pts info per position bins
for(i = 0; i < vLen; i++) {
var v = val[i];
if(!isNumeric(v)) continue;
var n = Lib.findBin(pos[i], posBins);
if(n >= 0 && n < pLen) {
var pt = {v: v, i: i};
arraysToCalcdata(pt, trace, i);
ptsPerBin[n].push(pt);
}
}
// build calcdata trace items, one item per distinct position
for(i = 0; i < pLen; i++) {
if(ptsPerBin[i].length > 0) {
var pts = ptsPerBin[i].sort(sortByVal);
var boxVals = pts.map(extractVal);
var bvLen = boxVals.length;
var cdi = {
pos: posDistinct[i],
pts: pts
};
cdi.min = boxVals[0];
cdi.max = boxVals[bvLen - 1];
cdi.mean = Lib.mean(boxVals, bvLen);
cdi.sd = Lib.stdev(boxVals, bvLen, cdi.mean);
// first quartile
cdi.q1 = Lib.interp(boxVals, 0.25);
// median
cdi.med = Lib.interp(boxVals, 0.5);
// third quartile
cdi.q3 = Lib.interp(boxVals, 0.75);
// lower and upper fences - last point inside
// 1.5 interquartile ranges from quartiles
cdi.lf = Math.min(
cdi.q1,
boxVals[Math.min(
Lib.findBin(2.5 * cdi.q1 - 1.5 * cdi.q3, boxVals, true) + 1,
bvLen - 1
)]
);
cdi.uf = Math.max(
cdi.q3,
boxVals[Math.max(
Lib.findBin(2.5 * cdi.q3 - 1.5 * cdi.q1, boxVals),
0
)]
);
// lower and upper outliers - 3 IQR out (don't clip to max/min,
// this is only for discriminating suspected & far outliers)
cdi.lo = 4 * cdi.q1 - 3 * cdi.q3;
cdi.uo = 4 * cdi.q3 - 3 * cdi.q1;
cd.push(cdi);
}
}
Axes.expand(valAxis, val, {padded: true});
if(cd.length > 0) {
cd[0].t = {
num: fullLayout[numKey],
dPos: dPos
};
fullLayout[numKey]++;
return cd;
} else {
return [{t: {empty: true}}];
}
};
// In vertical (horizontal) box plots:
// if no x (y) data, use x0 (y0), or name
// so if you want one box
// per trace, set x0 (y0) to the x (y) value or category for this trace
// (or set x (y) to a constant array matching y (x))
function getPos(trace, posLetter, posAxis, val, num) {
if(posLetter in trace) {
return posAxis.makeCalcdata(trace, posLetter);
}
var pos0;
if(posLetter + '0' in trace) {
pos0 = trace[posLetter + '0'];
} else if('name' in trace && (
posAxis.type === 'category' || (
isNumeric(trace.name) &&
['linear', 'log'].indexOf(posAxis.type) !== -1
) || (
Lib.isDateTime(trace.name) &&
posAxis.type === 'date'
)
)) {
pos0 = trace.name;
} else {
pos0 = num;
}
var pos0c = posAxis.d2c(pos0, 0, trace[posLetter + 'calendar']);
return val.map(function() { return pos0c; });
}
function makeBins(x, dx) {
var len = x.length;
var bins = new Array(len + 1);
for(var i = 0; i < len; i++) {
bins[i] = x[i] - dx;
}
bins[len] = x[len - 1] + dx;
return bins;
}
function initNestedArray(len) {
var arr = new Array(len);
for(var i = 0; i < len; i++) {
arr[i] = [];
}
return arr;
}
function arraysToCalcdata(pt, trace, i) {
var trace2calc = {
text: 'tx'
};
for(var k in trace2calc) {
if(Array.isArray(trace[k])) {
pt[trace2calc[k]] = trace[k][i];
}
}
}
function sortByVal(a, b) { return a.v - b.v; }
function extractVal(o) { return o.v; }