forked from recharts/recharts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getEquidistantTicks.ts
63 lines (52 loc) · 1.81 KB
/
getEquidistantTicks.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { isVisible } from '../util/TickUtils';
import { CartesianTickItem } from '../util/types';
import { getEveryNthWithCondition } from '../util/getEveryNthWithCondition';
import { Sign } from './getTicks';
export function getEquidistantTicks(
sign: Sign,
boundaries: { start: number; end: number },
getTickSize: (tick: CartesianTickItem, index: number) => number,
ticks: CartesianTickItem[],
minTickGap: number,
): CartesianTickItem[] {
const result = (ticks || []).slice();
const { start: initialStart, end } = boundaries;
let index = 0;
// Premature optimisation idea 1: Estimate a lower bound, and start from there.
// For now, start from every tick
let stepsize = 1;
let start = initialStart;
while (stepsize <= result.length) {
// Given stepsize, evaluate whether every stepsize-th tick can be shown.
// If it can not, then increase the stepsize by 1, and try again.
const entry = ticks?.[index];
// Break condition - If we have evaluate all the ticks, then we are done.
if (entry === undefined) {
return getEveryNthWithCondition(ticks, stepsize);
}
// Check if the element collides with the next element
const i = index;
let size: number | undefined;
const getSize = () => {
if (size === undefined) {
size = getTickSize(entry, i);
}
return size;
};
const tickCoord = entry.coordinate;
// We will always show the first tick.
const isShow = index === 0 || isVisible(sign, tickCoord, getSize, start, end);
if (!isShow) {
// Start all over with a larger stepsize
index = 0;
start = initialStart;
stepsize += 1;
}
if (isShow) {
// If it can be shown, update the start
start = tickCoord + sign * (getSize() / 2 + minTickGap);
index += stepsize;
}
}
return [];
}