UNPKG

1.84 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.rPretty = void 0;
4const pretty_number_1 = require("../utils/pretty-number");
5/**
6 * 创建分割点
7 * @param min 左区间
8 * @param max 右区间
9 * @param n 分割点个数
10 * @returns 计算后的 ticks
11 * @see R pretty https://svn.r-project.org/R/trunk/src/appl/pretty.c
12 * @see R pretty https://www.rdocumentation.org/packages/base/versions/3.5.2/topics/pretty
13 */
14const rPretty = (min, max, m = 5) => {
15 if (min === max) {
16 return [min];
17 }
18 const n = m < 0 ? 0 : Math.round(m);
19 if (n === 0)
20 return [];
21 // high.u.bias
22 const h = 1.5;
23 // u5.bias
24 const h5 = 0.5 + 1.5 * h;
25 // 反正我也不会调参,跳过所有判断步骤
26 const d = max - min;
27 const c = d / n;
28 // 当d非常小的时候触发,但似乎没什么用
29 // const min_n = Math.floor(n / 3);
30 // const shrink_sml = Math.pow(2, 5);
31 // if (Math.log10(d) < -2) {
32 // c = (_.max([ Math.abs(max), Math.abs(min) ]) * shrink_sml) / min_n;
33 // }
34 const base = 10 ** Math.floor(Math.log10(c));
35 let unit = base;
36 if (2 * base - c < h * (c - unit)) {
37 unit = 2 * base;
38 if (5 * base - c < h5 * (c - unit)) {
39 unit = 5 * base;
40 if (10 * base - c < h * (c - unit)) {
41 unit = 10 * base;
42 }
43 }
44 }
45 const nu = Math.ceil(max / unit);
46 const ns = Math.floor(min / unit);
47 const hi = Math.max(nu * unit, max);
48 const lo = Math.min(ns * unit, min);
49 const size = Math.floor((hi - lo) / unit) + 1;
50 const ticks = new Array(size);
51 for (let i = 0; i < size; i += 1) {
52 ticks[i] = (0, pretty_number_1.prettyNumber)(lo + i * unit);
53 }
54 return ticks;
55};
56exports.rPretty = rPretty;
57//# sourceMappingURL=r-pretty.js.map
\No newline at end of file