UNPKG

1.75 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3/**
4 * 计算几分位 https://github.com/simple-statistics/simple-statistics/blob/master/src/quantile_sorted.js
5 * @param x 数组
6 * @param p 百分比
7 */
8function quantileSorted(x, p) {
9 var idx = x.length * p;
10 /*if (x.length === 0) { // 当前场景这些条件不可能命中
11 throw new Error('quantile requires at least one value.');
12 } else if (p < 0 || p > 1) {
13 throw new Error('quantiles must be between 0 and 1');
14 } else */
15 if (p === 1) {
16 // If p is 1, directly return the last element
17 return x[x.length - 1];
18 }
19 else if (p === 0) {
20 // If p is 0, directly return the first element
21 return x[0];
22 }
23 else if (idx % 1 !== 0) {
24 // If p is not integer, return the next element in array
25 return x[Math.ceil(idx) - 1];
26 }
27 else if (x.length % 2 === 0) {
28 // If the list has even-length, we'll take the average of this number
29 // and the next value, if there is one
30 return (x[idx - 1] + x[idx]) / 2;
31 }
32 else {
33 // Finally, in the simple case of an integer value
34 // with an odd-length list, return the x value at the index.
35 return x[idx];
36 }
37}
38function calculateTicks(cfg) {
39 var tickCount = cfg.tickCount, values = cfg.values;
40 if (!values || !values.length) {
41 return [];
42 }
43 var sorted = values.slice().sort(function (a, b) {
44 return a - b;
45 });
46 var ticks = [];
47 for (var i = 0; i < tickCount; i++) {
48 var p = i / (tickCount - 1);
49 ticks.push(quantileSorted(sorted, p));
50 }
51 return ticks;
52}
53exports.default = calculateTicks;
54//# sourceMappingURL=quantile.js.map
\No newline at end of file