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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
|
import { formatCotacol, formatHours, formatSpeed, ticks } from "./analytics-logic.js";
import type { AnalyticsPoint } from "./types.js";
interface Plot {
left: number;
right: number;
top: number;
bottom: number;
}
interface AnalyticsColors {
surface: string;
grid: string;
subtle: string;
forest: string;
accent: string;
mutedText: string;
}
const POINT_RADIUS = 4;
const HOVER_RADIUS = 7;
const HIT_RADIUS = 12;
interface ScatterOptions {
outputId: string;
xAccessor: (point: AnalyticsPoint) => number;
xFormatter: (value: number) => string;
xUnit: string;
}
export class AnalyticsScatter {
private readonly canvas: HTMLCanvasElement;
private readonly points: AnalyticsPoint[];
private readonly colors: AnalyticsColors;
private readonly output: HTMLElement | null;
private readonly context: CanvasRenderingContext2D;
private readonly xAccessor: (point: AnalyticsPoint) => number;
private readonly xFormatter: (value: number) => string;
private readonly xUnit: string;
private plot: Plot | null;
private hoveredIndex: number;
private readonly minX: number;
private readonly maxX: number;
private readonly minCotacol: number;
private readonly maxCotacol: number;
constructor(canvas: HTMLCanvasElement, points: AnalyticsPoint[], colors: AnalyticsColors, options: ScatterOptions) {
this.canvas = canvas;
this.points = points;
this.colors = colors;
this.output = document.getElementById(options.outputId);
this.xAccessor = options.xAccessor;
this.xFormatter = options.xFormatter;
this.xUnit = options.xUnit;
const context = canvas.getContext("2d");
if (!context) throw new Error("Analytics canvas is unavailable");
this.context = context;
this.plot = null;
this.hoveredIndex = -1;
let maxX = 0;
let maxCotacol = 0;
for (const point of points) {
maxX = Math.max(maxX, this.xAccessor(point));
maxCotacol = Math.max(maxCotacol, point.cotacolPer100Km);
}
this.minX = 0;
this.minCotacol = 0;
this.maxX = maxX * 1.08 || 1;
this.maxCotacol = maxCotacol * 1.08 || 1;
this.bindEvents();
}
redraw(): void {
this.draw();
}
private draw(): void {
const rect = this.canvas.getBoundingClientRect();
if (rect.width === 0 || rect.height === 0) return;
const ratio = window.devicePixelRatio || 1;
this.canvas.width = Math.floor(rect.width * ratio);
this.canvas.height = Math.floor(rect.height * ratio);
this.context.setTransform(ratio, 0, 0, ratio, 0, 0);
const plot: Plot = { left: 44, right: rect.width - 16, top: 16, bottom: rect.height - 30 };
this.plot = plot;
this.context.clearRect(0, 0, rect.width, rect.height);
this.context.fillStyle = this.colors.surface;
this.context.fillRect(0, 0, rect.width, rect.height);
this.drawGrid(plot);
this.drawPoints(plot);
if (this.hoveredIndex >= 0) this.drawHover(plot);
}
private drawGrid(plot: Plot): void {
this.context.font = "11px system-ui, sans-serif";
const xTicks = ticks(this.minX, this.maxX);
for (const tick of xTicks) {
const x = this.xForValue(tick, plot);
this.context.strokeStyle = this.colors.grid;
this.context.lineWidth = 1;
this.context.beginPath();
this.context.moveTo(x, plot.top);
this.context.lineTo(x, plot.bottom);
this.context.stroke();
this.context.fillStyle = this.colors.subtle;
this.context.textAlign = "center";
this.context.textBaseline = "top";
this.context.fillText(this.xFormatter(tick), x, plot.bottom + 8);
}
this.context.fillStyle = this.colors.mutedText;
this.context.textAlign = "right";
this.context.fillText(this.xUnit, plot.right, plot.bottom + 8);
const yTicks = ticks(this.minCotacol, this.maxCotacol);
for (const tick of yTicks) {
const y = this.yForCotacol(tick, plot);
this.context.strokeStyle = this.colors.grid;
this.context.lineWidth = 1;
this.context.beginPath();
this.context.moveTo(plot.left, y);
this.context.lineTo(plot.right, y);
this.context.stroke();
this.context.fillStyle = this.colors.subtle;
this.context.textAlign = "right";
this.context.textBaseline = "middle";
this.context.fillText(formatCotacol(tick), plot.left - 8, y);
}
}
private drawPoints(plot: Plot): void {
for (let index = 0; index < this.points.length; index++) {
const point = this.points[index];
this.context.fillStyle = this.colors.accent;
this.context.beginPath();
this.context.arc(
this.xForValue(this.xAccessor(point), plot),
this.yForCotacol(point.cotacolPer100Km, plot),
POINT_RADIUS,
0,
2 * Math.PI,
);
this.context.fill();
}
}
private drawHover(plot: Plot): void {
const point = this.points[this.hoveredIndex];
const x = this.xForValue(this.xAccessor(point), plot);
const y = this.yForCotacol(point.cotacolPer100Km, plot);
this.context.strokeStyle = this.colors.forest;
this.context.lineWidth = 2;
this.context.beginPath();
this.context.arc(x, y, HOVER_RADIUS, 0, 2 * Math.PI);
this.context.stroke();
}
private xForValue(value: number, plot: Plot): number {
const span = Math.max(this.maxX - this.minX, 1);
return plot.left + ((value - this.minX) / span) * (plot.right - plot.left);
}
private yForCotacol(cotacol: number, plot: Plot): number {
const span = Math.max(this.maxCotacol - this.minCotacol, 1);
return plot.bottom - ((cotacol - this.minCotacol) / span) * (plot.bottom - plot.top);
}
private nearestPoint(x: number, y: number): number {
if (!this.plot) return -1;
let nearest = -1;
let best = Infinity;
for (let index = 0; index < this.points.length; index++) {
const point = this.points[index];
const dx = this.xForValue(this.xAccessor(point), this.plot) - x;
const dy = this.yForCotacol(point.cotacolPer100Km, this.plot) - y;
const distance = Math.hypot(dx, dy);
if (distance < best) {
best = distance;
nearest = index;
}
}
return best <= HIT_RADIUS ? nearest : -1;
}
private bindEvents(): void {
this.canvas.addEventListener("pointermove", (event: PointerEvent) => {
const rect = this.canvas.getBoundingClientRect();
const index = this.nearestPoint(event.clientX - rect.left, event.clientY - rect.top);
this.canvas.style.cursor = index >= 0 ? "pointer" : "default";
this.setHovered(index);
});
this.canvas.addEventListener("pointerleave", () => {
this.canvas.style.cursor = "default";
this.setHovered(-1);
});
this.canvas.addEventListener("pointercancel", () => this.setHovered(-1));
this.canvas.addEventListener("click", () => {
if (this.hoveredIndex < 0) return;
window.open(this.points[this.hoveredIndex].url, "_blank", "noopener");
});
}
private setHovered(index: number): void {
if (index === this.hoveredIndex) return;
this.hoveredIndex = index;
if (this.output) {
if (index < 0) {
this.output.textContent = "Hover a point to inspect a ride.";
} else {
const point = this.points[index];
const xLabel = this.xUnit ? `${this.xFormatter(this.xAccessor(point))} ${this.xUnit}` : this.xFormatter(this.xAccessor(point));
this.output.textContent = `${point.name} · ${xLabel} · ${formatCotacol(point.cotacolPer100Km)} Cotacol/100km · click for details`;
}
}
this.draw();
}
}
export const mountAnalytics = (): void => {
const pointsScript = document.getElementById("analytics-points");
if (!pointsScript) return;
const data = JSON.parse(pointsScript.textContent ?? "") as AnalyticsPoint[];
if (data.length === 0) return;
const colorProbe = document.createElement("span");
colorProbe.hidden = true;
document.body.append(colorProbe);
const resolveColor = (name: string): string => {
colorProbe.style.color = `var(${name})`;
return getComputedStyle(colorProbe).color;
};
const colors: AnalyticsColors = {
surface: resolveColor("--color-plot-surface"),
grid: resolveColor("--color-plot-grid"),
subtle: resolveColor("--color-subtle"),
forest: resolveColor("--color-forest"),
accent: resolveColor("--color-accent"),
mutedText: resolveColor("--color-muted"),
};
colorProbe.remove();
const speedCanvas = document.getElementById("analytics-chart") as HTMLCanvasElement | null;
if (speedCanvas) {
const scatter = new AnalyticsScatter(speedCanvas, data, colors, {
outputId: "analytics-hover",
xAccessor: (point) => point.speedKmh,
xFormatter: formatSpeed,
xUnit: "km/h",
});
window.addEventListener("resize", () => scatter.redraw());
scatter.redraw();
}
const durationCanvas = document.getElementById("analytics-duration-chart") as HTMLCanvasElement | null;
if (durationCanvas) {
const scatter = new AnalyticsScatter(durationCanvas, data, colors, {
outputId: "analytics-duration-hover",
xAccessor: (point) => point.movingHours,
xFormatter: formatHours,
xUnit: "",
});
window.addEventListener("resize", () => scatter.redraw());
scatter.redraw();
}
};
mountAnalytics();
|