diff options
| author | Martin Kagamino Lehoux <martin@lehoux.net> | 2026-08-21 19:07:31 +0200 |
|---|---|---|
| committer | Martin Kagamino Lehoux <martin@lehoux.net> | 2026-08-21 19:07:31 +0200 |
| commit | b3a83b2fdd5b406d020398e7c3cf6f46149a2523 (patch) | |
| tree | 7086ae3602e219f7478dcaeca66c0e54e337c1f3 /web/frontend/analytics-logic.ts | |
| parent | 1e63f5c91f0e5ff7dfc582b67d262d5939cc34bc (diff) | |
Add global analytics scatter chart
Diffstat (limited to 'web/frontend/analytics-logic.ts')
| -rw-r--r-- | web/frontend/analytics-logic.ts | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/web/frontend/analytics-logic.ts b/web/frontend/analytics-logic.ts new file mode 100644 index 0000000..7a53944 --- /dev/null +++ b/web/frontend/analytics-logic.ts @@ -0,0 +1,35 @@ +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)}`); |
