aboutsummaryrefslogtreecommitdiff
path: root/web/frontend/analytics.ts
blob: 5dfad52cd25fc928c89bcafea9cc005b889e4c0d (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
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

import { formatCotacol, 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;
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 plot: Plot | null;
  private hoveredIndex: number;
  private readonly minSpeed: number;
  private readonly maxSpeed: number;
  private readonly minCotacol: number;
  private readonly maxCotacol: number;

  constructor(canvas: HTMLCanvasElement, points: AnalyticsPoint[], colors: AnalyticsColors) {
    this.canvas = canvas;
    this.points = points;
    this.colors = colors;
    this.output = document.getElementById("analytics-hover");
    const context = canvas.getContext("2d");
    if (!context) throw new Error("Analytics canvas is unavailable");
    this.context = context;
    this.plot = null;
    this.hoveredIndex = -1;
    let maxSpeed = 0;
    let maxCotacol = 0;
    for (const point of points) {
      maxSpeed = Math.max(maxSpeed, point.speedKmh);
      maxCotacol = Math.max(maxCotacol, point.cotacolPer100Km);
    }
    this.minSpeed = 0;
    this.minCotacol = 0;
    this.maxSpeed = maxSpeed * 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.minSpeed, this.maxSpeed);
    for (const tick of xTicks) {
      const x = this.xForSpeed(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(formatSpeed(tick), x, plot.bottom + 8);
    }
    this.context.fillStyle = this.colors.mutedText;
    this.context.textAlign = "right";
    this.context.fillText("km/h", 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.xForSpeed(point.speedKmh, 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.xForSpeed(point.speedKmh, 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 xForSpeed(speed: number, plot: Plot): number {
    const span = Math.max(this.maxSpeed - this.minSpeed, 1);
    return plot.left + ((speed - this.minSpeed) / 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.xForSpeed(point.speedKmh, 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.location.href = this.points[this.hoveredIndex].url;
    });
  }

  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];
        this.output.textContent = `${point.name} · ${formatSpeed(point.speedKmh)} km/h · ${formatCotacol(point.cotacolPer100Km)} Cotacol/100km · click for details`;
      }
    }
    this.draw();
  }
}

export const mountAnalytics = (): void => {
  const canvas = document.getElementById("analytics-chart");
  const pointsScript = document.getElementById("analytics-points");
  if (!canvas || !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 scatter = new AnalyticsScatter(canvas as HTMLCanvasElement, data, colors);
  window.addEventListener("resize", () => scatter.redraw());
  scatter.redraw();
};

mountAnalytics();