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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
import type { AnalyticsPoint } from "./types.js";
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")}`;
};
export type ColorBy = "none" | "year" | "rideType" | "bikeType";
export interface RideGroup {
label: string;
count: number;
colorIndex: number;
}
export interface GroupedPoint {
point: AnalyticsPoint;
colorIndex: number;
}
const UNKNOWN_LABEL = "Unknown";
const rideTypeLabels: Record<string, string> = {
"": UNKNOWN_LABEL,
training: "Training",
indoor: "Indoor",
race: "Race",
transport: "Transport",
};
const bikeTypeLabels: Record<string, string> = {
"": UNKNOWN_LABEL,
road: "Road",
mtb: "MTB",
};
const rideTypeOrder = ["training", "indoor", "race", "transport"];
const bikeTypeOrder = ["road", "mtb"];
export const analyticsRideTypeLabel = (value: string): string => rideTypeLabels[value] ?? value;
export const analyticsBikeTypeLabel = (value: string): string => bikeTypeLabels[value] ?? value;
const rideTypeColorIndex = (value: string): number => {
const index = rideTypeOrder.indexOf(value);
return index < 0 ? rideTypeOrder.length : index;
};
const bikeTypeColorIndex = (value: string): number => {
const index = bikeTypeOrder.indexOf(value);
return index < 0 ? bikeTypeOrder.length : index;
};
const yearColorIndex = (value: number, years: number[]): number => years.indexOf(value);
export const groupRidePoints = (points: AnalyticsPoint[], colorBy: ColorBy): RideGroup[] => {
if (colorBy === "none") return [];
const counts = new Map<string, number>();
for (const point of points) {
const key = groupKey(colorBy, point);
counts.set(key, (counts.get(key) ?? 0) + 1);
}
return groupKeys(colorBy, counts).map((key, index) => ({
label: groupLabel(colorBy, key),
count: counts.get(key) ?? 0,
colorIndex: colorIndexForGroupKey(colorBy, key, index),
}));
};
const colorIndexForGroupKey = (colorBy: ColorBy, key: string, compactIndex: number): number => {
if (colorBy === "rideType") return rideTypeColorIndex(key);
if (colorBy === "bikeType") return bikeTypeColorIndex(key);
return compactIndex;
};
const groupKey = (colorBy: ColorBy, point: AnalyticsPoint): string => {
if (colorBy === "year") return String(point.year);
if (colorBy === "rideType") return point.rideType;
if (colorBy === "bikeType") return point.bikeType;
return "";
};
const groupLabel = (colorBy: ColorBy, key: string): string => {
if (colorBy === "year") return key;
if (colorBy === "rideType") return analyticsRideTypeLabel(key);
if (colorBy === "bikeType") return analyticsBikeTypeLabel(key);
return key;
};
const groupKeys = (colorBy: ColorBy, counts: Map<string, number>): string[] => {
const keys = [...counts.keys()];
if (colorBy === "rideType") {
return [...rideTypeOrder.filter((key) => counts.has(key)), ...(counts.has("") ? [""] : [])];
}
if (colorBy === "bikeType") {
return [...bikeTypeOrder.filter((key) => counts.has(key)), ...(counts.has("") ? [""] : [])];
}
if (colorBy === "year") {
return keys.sort((a, b) => Number(a) - Number(b));
}
return keys;
};
export const colorIndexForPoint = (point: AnalyticsPoint, colorBy: ColorBy): number => {
if (colorBy === "year") return 0;
if (colorBy === "rideType") return rideTypeColorIndex(point.rideType);
if (colorBy === "bikeType") return bikeTypeColorIndex(point.bikeType);
return 0;
};
export const groupedPoints = (points: AnalyticsPoint[], colorBy: ColorBy): GroupedPoint[] => {
if (colorBy === "year") {
const years = [...new Set(points.map((point) => point.year))].sort((a, b) => a - b);
return points.map((point) => ({ point, colorIndex: yearColorIndex(point.year, years) }));
}
return points.map((point) => ({ point, colorIndex: colorIndexForPoint(point, colorBy) }));
};
|