1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.findTickInterval = void 0;
|
4 | const time_interval_1 = require("./time-interval");
|
5 | const utc_interval_1 = require("./utc-interval");
|
6 | const bisect_1 = require("./bisect");
|
7 | const ticks_1 = require("./ticks");
|
8 | function chooseTickIntervals(utc) {
|
9 | const intervalMap = utc ? utc_interval_1.utcIntervalMap : time_interval_1.localIntervalMap;
|
10 | const { year, month, week, day, hour, minute, second, millisecond } = intervalMap;
|
11 | const tickIntervals = [
|
12 | [second, 1],
|
13 | [second, 5],
|
14 | [second, 15],
|
15 | [second, 30],
|
16 | [minute, 1],
|
17 | [minute, 5],
|
18 | [minute, 15],
|
19 | [minute, 30],
|
20 | [hour, 1],
|
21 | [hour, 3],
|
22 | [hour, 6],
|
23 | [hour, 12],
|
24 | [day, 1],
|
25 | [day, 2],
|
26 | [week, 1],
|
27 | [month, 1],
|
28 | [month, 3],
|
29 | [year, 1],
|
30 | ];
|
31 | return {
|
32 | tickIntervals,
|
33 | year,
|
34 | millisecond,
|
35 | };
|
36 | }
|
37 | function findTickInterval(start, stop, count, interval, utc) {
|
38 | const lo = +start;
|
39 | const hi = +stop;
|
40 | const { tickIntervals, year, millisecond } = chooseTickIntervals(utc);
|
41 | const getter = ([interval, count]) => interval.duration * count;
|
42 | const targetCount = interval ? (hi - lo) / interval : count || 5;
|
43 | const targetInterval = interval || (hi - lo) / targetCount;
|
44 | const len = tickIntervals.length;
|
45 | const i = (0, bisect_1.bisect)(tickIntervals, targetInterval, 0, len, getter);
|
46 | let matchInterval;
|
47 | if (i === len) {
|
48 | const step = (0, ticks_1.tickStep)(lo / year.duration, hi / year.duration, targetCount);
|
49 | matchInterval = [year, step];
|
50 | }
|
51 | else if (i) {
|
52 | const closeToLow = targetInterval / getter(tickIntervals[i - 1]) < getter(tickIntervals[i]) / targetInterval;
|
53 | const [timeInterval, targetStep] = closeToLow ? tickIntervals[i - 1] : tickIntervals[i];
|
54 | const step = interval ? Math.ceil(interval / timeInterval.duration) : targetStep;
|
55 | matchInterval = [timeInterval, step];
|
56 | }
|
57 | else {
|
58 | const step = Math.max((0, ticks_1.tickStep)(lo, hi, targetCount), 1);
|
59 | matchInterval = [millisecond, step];
|
60 | }
|
61 | return matchInterval;
|
62 | }
|
63 | exports.findTickInterval = findTickInterval;
|
64 |
|
\ | No newline at end of file |