| [e4c61dd] | 1 | const e10 = Math.sqrt(50),
|
|---|
| 2 | e5 = Math.sqrt(10),
|
|---|
| 3 | e2 = Math.sqrt(2);
|
|---|
| 4 |
|
|---|
| 5 | function tickSpec(start, stop, count) {
|
|---|
| 6 | const step = (stop - start) / Math.max(0, count),
|
|---|
| 7 | power = Math.floor(Math.log10(step)),
|
|---|
| 8 | error = step / Math.pow(10, power),
|
|---|
| 9 | factor = error >= e10 ? 10 : error >= e5 ? 5 : error >= e2 ? 2 : 1;
|
|---|
| 10 | let i1, i2, inc;
|
|---|
| 11 | if (power < 0) {
|
|---|
| 12 | inc = Math.pow(10, -power) / factor;
|
|---|
| 13 | i1 = Math.round(start * inc);
|
|---|
| 14 | i2 = Math.round(stop * inc);
|
|---|
| 15 | if (i1 / inc < start) ++i1;
|
|---|
| 16 | if (i2 / inc > stop) --i2;
|
|---|
| 17 | inc = -inc;
|
|---|
| 18 | } else {
|
|---|
| 19 | inc = Math.pow(10, power) * factor;
|
|---|
| 20 | i1 = Math.round(start / inc);
|
|---|
| 21 | i2 = Math.round(stop / inc);
|
|---|
| 22 | if (i1 * inc < start) ++i1;
|
|---|
| 23 | if (i2 * inc > stop) --i2;
|
|---|
| 24 | }
|
|---|
| 25 | if (i2 < i1 && 0.5 <= count && count < 2) return tickSpec(start, stop, count * 2);
|
|---|
| 26 | return [i1, i2, inc];
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | export default function ticks(start, stop, count) {
|
|---|
| 30 | stop = +stop, start = +start, count = +count;
|
|---|
| 31 | if (!(count > 0)) return [];
|
|---|
| 32 | if (start === stop) return [start];
|
|---|
| 33 | const reverse = stop < start, [i1, i2, inc] = reverse ? tickSpec(stop, start, count) : tickSpec(start, stop, count);
|
|---|
| 34 | if (!(i2 >= i1)) return [];
|
|---|
| 35 | const n = i2 - i1 + 1, ticks = new Array(n);
|
|---|
| 36 | if (reverse) {
|
|---|
| 37 | if (inc < 0) for (let i = 0; i < n; ++i) ticks[i] = (i2 - i) / -inc;
|
|---|
| 38 | else for (let i = 0; i < n; ++i) ticks[i] = (i2 - i) * inc;
|
|---|
| 39 | } else {
|
|---|
| 40 | if (inc < 0) for (let i = 0; i < n; ++i) ticks[i] = (i1 + i) / -inc;
|
|---|
| 41 | else for (let i = 0; i < n; ++i) ticks[i] = (i1 + i) * inc;
|
|---|
| 42 | }
|
|---|
| 43 | return ticks;
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | export function tickIncrement(start, stop, count) {
|
|---|
| 47 | stop = +stop, start = +start, count = +count;
|
|---|
| 48 | return tickSpec(start, stop, count)[2];
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | export function tickStep(start, stop, count) {
|
|---|
| 52 | stop = +stop, start = +start, count = +count;
|
|---|
| 53 | const reverse = stop < start, inc = reverse ? tickIncrement(stop, start, count) : tickIncrement(start, stop, count);
|
|---|
| 54 | return (reverse ? -1 : 1) * (inc < 0 ? 1 / -inc : inc);
|
|---|
| 55 | }
|
|---|