| [a762898] | 1 | import { isVisible } from '../util/TickUtils';
|
|---|
| 2 | import { getEveryNth } from '../util/getEveryNth';
|
|---|
| 3 | export function getEquidistantTicks(sign, boundaries, getTickSize, ticks, minTickGap) {
|
|---|
| 4 | // If the ticks are readonly, then the slice might not be necessary
|
|---|
| 5 | var result = (ticks || []).slice();
|
|---|
| 6 | var {
|
|---|
| 7 | start: initialStart,
|
|---|
| 8 | end
|
|---|
| 9 | } = boundaries;
|
|---|
| 10 | var index = 0;
|
|---|
| 11 | // Premature optimisation idea 1: Estimate a lower bound, and start from there.
|
|---|
| 12 | // For now, start from every tick
|
|---|
| 13 | var stepsize = 1;
|
|---|
| 14 | var start = initialStart;
|
|---|
| 15 | var _loop = function _loop() {
|
|---|
| 16 | // Given stepsize, evaluate whether every stepsize-th tick can be shown.
|
|---|
| 17 | // If it can not, then increase the stepsize by 1, and try again.
|
|---|
| 18 |
|
|---|
| 19 | var entry = ticks === null || ticks === void 0 ? void 0 : ticks[index];
|
|---|
| 20 |
|
|---|
| 21 | // Break condition - If we have evaluated all the ticks, then we are done.
|
|---|
| 22 | if (entry === undefined) {
|
|---|
| 23 | return {
|
|---|
| 24 | v: getEveryNth(ticks, stepsize)
|
|---|
| 25 | };
|
|---|
| 26 | }
|
|---|
| 27 |
|
|---|
| 28 | // Check if the element collides with the next element
|
|---|
| 29 | var i = index;
|
|---|
| 30 | var size;
|
|---|
| 31 | var getSize = () => {
|
|---|
| 32 | if (size === undefined) {
|
|---|
| 33 | size = getTickSize(entry, i);
|
|---|
| 34 | }
|
|---|
| 35 | return size;
|
|---|
| 36 | };
|
|---|
| 37 | var tickCoord = entry.coordinate;
|
|---|
| 38 | // We will always show the first tick.
|
|---|
| 39 | var isShow = index === 0 || isVisible(sign, tickCoord, getSize, start, end);
|
|---|
| 40 | if (!isShow) {
|
|---|
| 41 | // Start all over with a larger stepsize
|
|---|
| 42 | index = 0;
|
|---|
| 43 | start = initialStart;
|
|---|
| 44 | stepsize += 1;
|
|---|
| 45 | }
|
|---|
| 46 | if (isShow) {
|
|---|
| 47 | // If it can be shown, update the start
|
|---|
| 48 | start = tickCoord + sign * (getSize() / 2 + minTickGap);
|
|---|
| 49 | index += stepsize;
|
|---|
| 50 | }
|
|---|
| 51 | },
|
|---|
| 52 | _ret;
|
|---|
| 53 | while (stepsize <= result.length) {
|
|---|
| 54 | _ret = _loop();
|
|---|
| 55 | if (_ret) return _ret.v;
|
|---|
| 56 | }
|
|---|
| 57 | return [];
|
|---|
| 58 | }
|
|---|
| 59 | export function getEquidistantPreserveEndTicks(sign, boundaries, getTickSize, ticks, minTickGap) {
|
|---|
| 60 | // If the ticks are readonly, then the slice might not be necessary
|
|---|
| 61 | // Reworked logic for getEquidistantPreserveEndTicks
|
|---|
| 62 | var result = (ticks || []).slice();
|
|---|
| 63 | var len = result.length;
|
|---|
| 64 | if (len === 0) {
|
|---|
| 65 | return [];
|
|---|
| 66 | }
|
|---|
| 67 | var {
|
|---|
| 68 | start: initialStart,
|
|---|
| 69 | end
|
|---|
| 70 | } = boundaries;
|
|---|
| 71 |
|
|---|
| 72 | // Start with stepsize = 1 (every tick) up to the maximum possible stepsize (len)
|
|---|
| 73 | for (var stepsize = 1; stepsize <= len; stepsize++) {
|
|---|
| 74 | // 1. Calculate the offset so the last tick (index len - 1) is always included in the sequence.
|
|---|
| 75 | var offset = (len - 1) % stepsize;
|
|---|
| 76 | var start = initialStart; // `start` tracks the coordinate of the last successfully drawn tick + gap
|
|---|
| 77 | var ok = true;
|
|---|
| 78 |
|
|---|
| 79 | // 2. Iterate through the end-anchored sequence: offset, offset + stepsize, ..., len - 1
|
|---|
| 80 | var _loop2 = function _loop2() {
|
|---|
| 81 | var entry = ticks[index];
|
|---|
| 82 | if (entry == null) {
|
|---|
| 83 | return 0; // continue
|
|---|
| 84 | }
|
|---|
| 85 | var i = index;
|
|---|
| 86 | var size;
|
|---|
| 87 |
|
|---|
| 88 | // Use a function to get size, as in the original code
|
|---|
| 89 | var getSize = () => {
|
|---|
| 90 | if (size === undefined) {
|
|---|
| 91 | size = getTickSize(entry, i);
|
|---|
| 92 | }
|
|---|
| 93 | return size;
|
|---|
| 94 | };
|
|---|
| 95 | var tickCoord = entry.coordinate;
|
|---|
| 96 |
|
|---|
| 97 | // 3. Apply visibility logic (including the first tick special case)
|
|---|
| 98 | // The reviewer says *not* to unconditionally bypass checks for the last tick.
|
|---|
| 99 | var isShow = index === offset || isVisible(sign, tickCoord, getSize, start, end);
|
|---|
| 100 | if (!isShow) {
|
|---|
| 101 | // If any tick in this end-anchored sequence fails visibility/collision,
|
|---|
| 102 | // reject this stepsize and move to the next iteration (larger stepsize).
|
|---|
| 103 | ok = false;
|
|---|
| 104 | return 1; // break
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | // 4. If showable, update the 'start' coordinate for the next collision check
|
|---|
| 108 | if (isShow) {
|
|---|
| 109 | start = tickCoord + sign * (getSize() / 2 + minTickGap);
|
|---|
| 110 | }
|
|---|
| 111 | },
|
|---|
| 112 | _ret2;
|
|---|
| 113 | for (var index = offset; index < len; index += stepsize) {
|
|---|
| 114 | _ret2 = _loop2();
|
|---|
| 115 | if (_ret2 === 0) continue;
|
|---|
| 116 | if (_ret2 === 1) break;
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | // 5. If the entire sequence for this stepsize passed the visibility check, return the result
|
|---|
| 120 | if (ok) {
|
|---|
| 121 | // Build the final result array explicitly using the validated stepsize and offset.
|
|---|
| 122 | var finalTicks = [];
|
|---|
| 123 | for (var _index = offset; _index < len; _index += stepsize) {
|
|---|
| 124 | var tick = ticks[_index];
|
|---|
| 125 | if (tick != null) {
|
|---|
| 126 | finalTicks.push(tick);
|
|---|
| 127 | }
|
|---|
| 128 | }
|
|---|
| 129 | return finalTicks;
|
|---|
| 130 | }
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | // If no stepsize works (this shouldn't happen unless minTickGap is huge), return an empty array.
|
|---|
| 134 | return [];
|
|---|
| 135 | } |
|---|