UNPKG

1.66 kBJavaScriptView Raw
1function prettyNumber(n) {
2 return n < 1e-15 ? n : parseFloat(n.toFixed(15));
3}
4export default function pretty(min, max, n) {
5 if (n === void 0) { n = 5; }
6 if (min === max) {
7 return {
8 max: max,
9 min: min,
10 ticks: [min],
11 };
12 }
13 /*
14 R pretty:
15 https://svn.r-project.org/R/trunk/src/appl/pretty.c
16 https://www.rdocumentation.org/packages/base/versions/3.5.2/topics/pretty
17 */
18 var h = 1.5; // high.u.bias
19 var h5 = 0.5 + 1.5 * h; // u5.bias
20 // 反正我也不会调参,跳过所有判断步骤
21 var d = max - min;
22 var c = d / n;
23 // 当d非常小的时候触发,但似乎没什么用
24 // const min_n = Math.floor(n / 3);
25 // const shrink_sml = Math.pow(2, 5);
26 // if (Math.log10(d) < -2) {
27 // c = (_.max([ Math.abs(max), Math.abs(min) ]) * shrink_sml) / min_n;
28 // }
29 var base = Math.pow(10, Math.floor(Math.log10(c)));
30 var unit = base;
31 if (2 * base - c < h * (c - unit)) {
32 unit = 2 * base;
33 if (5 * base - c < h5 * (c - unit)) {
34 unit = 5 * base;
35 if (10 * base - c < h * (c - unit)) {
36 unit = 10 * base;
37 }
38 }
39 }
40 var nu = Math.ceil(max / unit);
41 var ns = Math.floor(min / unit);
42 var hi = Math.max(nu * unit, max);
43 var lo = Math.min(ns * unit, min);
44 var size = Math.floor((hi - lo) / unit) + 1;
45 var ticks = new Array(size);
46 for (var i = 0; i < size; i++) {
47 ticks[i] = prettyNumber(lo + i * unit);
48 }
49 return {
50 min: lo,
51 max: hi,
52 ticks: ticks,
53 };
54}
55//# sourceMappingURL=pretty.js.map
\No newline at end of file