source: node_modules/recharts/lib/cartesian/getEquidistantTicks.js@ a762898

Last change on this file since a762898 was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago

Added visualizations

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