blob: e8a15596c14bff4e99a94595261b26ddd539514f (
plain) (
blame)
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
|
export const niceStep = (range: number, target: number): number => {
if (range <= 0) return 1;
const ideal = range / target;
const magnitude = 10 ** Math.floor(Math.log10(ideal));
let best = magnitude;
let bestDistance = Number.POSITIVE_INFINITY;
for (const factor of [1, 2, 5, 10]) {
const candidate = factor * magnitude;
const distance = Math.abs(candidate - ideal);
if (distance < bestDistance) {
bestDistance = distance;
best = candidate;
}
}
return best;
};
export const ticks = (min: number, max: number, target = 5): number[] => {
const step = niceStep(max - min, target);
const values: number[] = [];
for (let value = Math.floor(min / step) * step; value <= max + 1e-9; value += step) {
if (value >= min - 1e-9) values.push(roundStep(value));
}
return values;
};
const roundStep = (value: number): number => {
if (!Number.isFinite(value) || value === 0) return 0;
const decimals = Math.max(0, -Math.floor(Math.log10(value) + 1e-9));
return Number(value.toFixed(decimals));
};
export const formatSpeed = (speed: number): string => `${speed.toFixed(1)}`;
export const formatCotacol = (cotacol: number): string => (cotacol >= 100 ? `${Math.round(cotacol)}` : `${cotacol.toFixed(1)}`);
export const formatHours = (hours: number): string => {
if (hours < 1) return `${Math.round(hours * 60)}m`;
const h = Math.floor(hours);
const m = Math.round((hours - h) * 60);
if (m === 0) return `${h}h`;
return `${h}h${String(m).padStart(2, "0")}`;
};
|