UNPKG

3.77 kBJavaScriptView Raw
1export default function d3Linear(cfg) {
2 var min = cfg.min, max = cfg.max, nice = cfg.nice, tickCount = cfg.tickCount;
3 var linear = new D3Linear();
4 linear.domain([min, max]);
5 if (nice) {
6 linear.nice(tickCount);
7 }
8 return linear.ticks(tickCount);
9}
10var DEFAULT_COUNT = 5;
11var e10 = Math.sqrt(50);
12var e5 = Math.sqrt(10);
13var e2 = Math.sqrt(2);
14// https://github.com/d3/d3-scale
15var D3Linear = /** @class */ (function () {
16 function D3Linear() {
17 this._domain = [0, 1];
18 }
19 D3Linear.prototype.domain = function (domain) {
20 if (domain) {
21 this._domain = Array.from(domain, Number);
22 return this;
23 }
24 return this._domain.slice();
25 };
26 D3Linear.prototype.nice = function (count) {
27 var _a, _b;
28 if (count === void 0) { count = DEFAULT_COUNT; }
29 var d = this._domain.slice();
30 var i0 = 0;
31 var i1 = this._domain.length - 1;
32 var start = this._domain[i0];
33 var stop = this._domain[i1];
34 var step;
35 if (stop < start) {
36 _a = [stop, start], start = _a[0], stop = _a[1];
37 _b = [i1, i0], i0 = _b[0], i1 = _b[1];
38 }
39 step = tickIncrement(start, stop, count);
40 if (step > 0) {
41 start = Math.floor(start / step) * step;
42 stop = Math.ceil(stop / step) * step;
43 step = tickIncrement(start, stop, count);
44 }
45 else if (step < 0) {
46 start = Math.ceil(start * step) / step;
47 stop = Math.floor(stop * step) / step;
48 step = tickIncrement(start, stop, count);
49 }
50 if (step > 0) {
51 d[i0] = Math.floor(start / step) * step;
52 d[i1] = Math.ceil(stop / step) * step;
53 this.domain(d);
54 }
55 else if (step < 0) {
56 d[i0] = Math.ceil(start * step) / step;
57 d[i1] = Math.floor(stop * step) / step;
58 this.domain(d);
59 }
60 return this;
61 };
62 D3Linear.prototype.ticks = function (count) {
63 if (count === void 0) { count = DEFAULT_COUNT; }
64 return d3ArrayTicks(this._domain[0], this._domain[this._domain.length - 1], count || DEFAULT_COUNT);
65 };
66 return D3Linear;
67}());
68export { D3Linear };
69function d3ArrayTicks(start, stop, count) {
70 var reverse;
71 var i = -1;
72 var n;
73 var ticks;
74 var step;
75 (stop = +stop), (start = +start), (count = +count);
76 if (start === stop && count > 0) {
77 return [start];
78 }
79 // tslint:disable-next-line
80 if ((reverse = stop < start)) {
81 (n = start), (start = stop), (stop = n);
82 }
83 // tslint:disable-next-line
84 if ((step = tickIncrement(start, stop, count)) === 0 || !isFinite(step)) {
85 return [];
86 }
87 if (step > 0) {
88 start = Math.ceil(start / step);
89 stop = Math.floor(stop / step);
90 ticks = new Array((n = Math.ceil(stop - start + 1)));
91 while (++i < n) {
92 ticks[i] = (start + i) * step;
93 }
94 }
95 else {
96 start = Math.floor(start * step);
97 stop = Math.ceil(stop * step);
98 ticks = new Array((n = Math.ceil(start - stop + 1)));
99 while (++i < n) {
100 ticks[i] = (start - i) / step;
101 }
102 }
103 if (reverse) {
104 ticks.reverse();
105 }
106 return ticks;
107}
108function tickIncrement(start, stop, count) {
109 var step = (stop - start) / Math.max(0, count);
110 var power = Math.floor(Math.log(step) / Math.LN10);
111 var error = step / Math.pow(10, power);
112 return power >= 0
113 ? (error >= e10 ? 10 : error >= e5 ? 5 : error >= e2 ? 2 : 1) * Math.pow(10, power)
114 : -Math.pow(10, -power) / (error >= e10 ? 10 : error >= e5 ? 5 : error >= e2 ? 2 : 1);
115}
116//# sourceMappingURL=d3-linear.js.map
\No newline at end of file