annotate weather_server/static/graph.js @ 41:4af79d69b12e v0.2.0

Use units better suited for science.
author Paul Fisher <paul@pfish.zone>
date Tue, 01 Apr 2025 16:04:52 -0400
parents beedfa8eaa3f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
19
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
1 var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
28
f817fa785c93 Make graph only consider visible points when setting range.
Paul Fisher <paul@pfish.zone>
parents: 19
diff changeset
2 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
19
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
3 return new (P || (P = Promise))(function (resolve, reject) {
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
4 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
5 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
28
f817fa785c93 Make graph only consider visible points when setting range.
Paul Fisher <paul@pfish.zone>
parents: 19
diff changeset
6 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
7 step((generator = generator.apply(thisArg, _arguments || [])).next());
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
8 });
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
9 };
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
10 define("math", ["require", "exports"], function (require, exports) {
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
11 "use strict";
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
12 Object.defineProperty(exports, "__esModule", { value: true });
41
4af79d69b12e Use units better suited for science.
Paul Fisher <paul@pfish.zone>
parents: 33
diff changeset
13 exports.dewPointC = exports.cToK = exports.cToF = void 0;
19
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
14 function cToF(tempC) {
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
15 return tempC * 9 / 5 + 32;
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
16 }
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
17 exports.cToF = cToF;
41
4af79d69b12e Use units better suited for science.
Paul Fisher <paul@pfish.zone>
parents: 33
diff changeset
18 function cToK(tempC) {
4af79d69b12e Use units better suited for science.
Paul Fisher <paul@pfish.zone>
parents: 33
diff changeset
19 return tempC + 273.15;
4af79d69b12e Use units better suited for science.
Paul Fisher <paul@pfish.zone>
parents: 33
diff changeset
20 }
4af79d69b12e Use units better suited for science.
Paul Fisher <paul@pfish.zone>
parents: 33
diff changeset
21 exports.cToK = cToK;
19
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
22 const MAGNUS_B = 17.62;
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
23 const MAGNUS_C = 243.12;
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
24 function gammaFn(tempC, rhPct) {
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
25 return Math.log(rhPct / 100) + MAGNUS_B * tempC / (MAGNUS_C + tempC);
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
26 }
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
27 function dewPointC(tempC, rhPct) {
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
28 const gamma = gammaFn(tempC, rhPct);
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
29 return MAGNUS_C * gamma / (MAGNUS_B - gamma);
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
30 }
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
31 exports.dewPointC = dewPointC;
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
32 });
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
33 define("graph", ["require", "exports", "math"], function (require, exports, math_1) {
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
34 "use strict";
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
35 Object.defineProperty(exports, "__esModule", { value: true });
33
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
36 exports.setUp = void 0;
19
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
37 const HISTORY_SECONDS = 60 * 60 * 4;
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
38 const BUFFER_SECONDS = 300;
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
39 function setUp(root, tempElement, dewPointElement) {
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
40 return __awaiter(this, void 0, void 0, function* () {
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
41 const nowTS = new Date().getTime() / 1000;
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
42 const startTS = nowTS - HISTORY_SECONDS;
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
43 const query = new URL('?', location.href);
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
44 query.pathname = query.pathname + '/recent';
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
45 query.searchParams.set('seconds', String(HISTORY_SECONDS + BUFFER_SECONDS));
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
46 const results = yield fetch(query.href);
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
47 if (!results.ok)
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
48 return;
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
49 const data = yield results.json();
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
50 const readings = data.readings;
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
51 if (readings.length === 0)
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
52 return;
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
53 root.classList.remove('plain');
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
54 root.classList.add('fancy');
41
4af79d69b12e Use units better suited for science.
Paul Fisher <paul@pfish.zone>
parents: 33
diff changeset
55 const tempsF = readings.map(s => [s.sample_time, (0, math_1.cToK)(s.temp_c)]);
4af79d69b12e Use units better suited for science.
Paul Fisher <paul@pfish.zone>
parents: 33
diff changeset
56 const dewPointsF = readings.map(s => [s.sample_time, (0, math_1.cToK)((0, math_1.dewPointC)(s.temp_c, s.rh_pct))]);
19
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
57 setUpElement(tempElement, [startTS, nowTS], tempsF);
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
58 setUpElement(dewPointElement, [startTS, nowTS], dewPointsF);
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
59 });
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
60 }
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
61 exports.setUp = setUp;
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
62 function setUpElement(element, timeRange, data) {
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
63 if (data.length === 0)
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
64 return;
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
65 const chart = new Chart(element, timeRange, data);
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
66 chart.resize();
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
67 addEventListener('resize', () => chart.resize());
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
68 }
33
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
69 const Y_DEGREE_RANGE = 12;
19
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
70 const LINE_WIDTH_PX = 2;
33
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
71 const GRID_WIDTH_PX = 0.5;
19
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
72 const FONT_SIZE = '40px';
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
73 class Chart {
33
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
74 constructor(element, timeRange, data, timezone) {
19
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
75 this.element = element;
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
76 this.timeRange = timeRange;
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
77 this.data = data;
33
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
78 this.timezone = timezone;
19
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
79 this.canvas = document.createElement('canvas');
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
80 this.element.insertBefore(this.canvas, element.firstChild);
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
81 const unit = element.getElementsByClassName('unit')[0];
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
82 this.unit = unit && unit.textContent || '';
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
83 }
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
84 resize() {
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
85 const dpr = self.devicePixelRatio || 1;
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
86 const [w, h] = this.size();
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
87 const pxSize = [w * dpr, h * dpr];
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
88 this.canvas.width = pxSize[0];
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
89 this.canvas.height = pxSize[1];
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
90 const ctx = this.canvas.getContext('2d');
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
91 ctx.clearRect(0, 0, pxSize[0], pxSize[1]);
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
92 ctx.scale(dpr, dpr);
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
93 this.redraw(ctx);
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
94 }
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
95 redraw(ctx) {
33
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
96 ctx.strokeStyle = this.stroke();
19
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
97 ctx.lineJoin = 'round';
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
98 ctx.lineWidth = LINE_WIDTH_PX;
33
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
99 ctx.font = `bold ${FONT_SIZE} ${this.font()}`;
28
f817fa785c93 Make graph only consider visible points when setting range.
Paul Fisher <paul@pfish.zone>
parents: 19
diff changeset
100 const onScreenData = this.data.filter(([time, _]) => this.timeRange[0] <= time);
f817fa785c93 Make graph only consider visible points when setting range.
Paul Fisher <paul@pfish.zone>
parents: 19
diff changeset
101 const yRange = calculateYRange(onScreenData.map(d => d[1]));
19
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
102 const [fullW, fullH] = this.size();
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
103 const [xPad, yMargin] = this.measureMargin(ctx);
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
104 const graphSize = [fullW - xPad, fullH];
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
105 ctx.beginPath();
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
106 for (const pt of this.data) {
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
107 const projected = project(pt, graphSize, this.timeRange, yRange);
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
108 ctx.lineTo(...projected);
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
109 }
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
110 ctx.stroke();
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
111 ctx.beginPath();
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
112 const lastPt = this.data[this.data.length - 1];
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
113 const center = project(lastPt, graphSize, this.timeRange, yRange);
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
114 ctx.ellipse(center[0], center[1], 1.5 * LINE_WIDTH_PX, 1.5 * LINE_WIDTH_PX, 0, 0, 2 * Math.PI);
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
115 ctx.fillStyle = getComputedStyle(this.element).backgroundColor;
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
116 ctx.fill();
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
117 ctx.stroke();
33
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
118 this.drawGrid(ctx, graphSize, yRange);
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
119 ctx.fillStyle = this.stroke();
19
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
120 ctx.textAlign = 'left';
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
121 ctx.textBaseline = 'top';
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
122 ctx.fillText(`${niceNumber(lastPt[1])} ${this.unit}`, center[0] + 5 * LINE_WIDTH_PX, center[1] + yMargin);
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
123 }
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
124 measureMargin(ctx) {
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
125 const bbox = ctx.measureText(`−99 ${this.unit}`);
28
f817fa785c93 Make graph only consider visible points when setting range.
Paul Fisher <paul@pfish.zone>
parents: 19
diff changeset
126 const margin = 5 * LINE_WIDTH_PX +
f817fa785c93 Make graph only consider visible points when setting range.
Paul Fisher <paul@pfish.zone>
parents: 19
diff changeset
127 bbox.width +
f817fa785c93 Make graph only consider visible points when setting range.
Paul Fisher <paul@pfish.zone>
parents: 19
diff changeset
128 16;
19
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
129 return [margin, -31.5 / 2];
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
130 }
33
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
131 drawGrid(ctx, size, yRange) {
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
132 ctx.save();
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
133 ctx.lineCap = 'butt';
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
134 ctx.lineWidth = GRID_WIDTH_PX;
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
135 this.drawTime(ctx, size);
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
136 this.drawTemps(ctx, size, yRange);
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
137 ctx.restore();
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
138 }
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
139 drawTime(ctx, [w, h]) {
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
140 const minutes = minutesOf(this.timeRange, this.timezone);
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
141 for (const [ts, hhmm] of minutes) {
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
142 const mins = Number(hhmm.split(':')[1]);
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
143 let opacity;
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
144 if (mins === 0) {
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
145 opacity = 1;
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
146 }
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
147 else if (mins % 15 === 0) {
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
148 opacity = 0.5;
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
149 }
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
150 else {
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
151 continue;
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
152 }
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
153 const x = project1D(ts, w, this.timeRange);
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
154 ctx.save();
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
155 ctx.globalAlpha = opacity;
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
156 ctx.beginPath();
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
157 ctx.moveTo(x, 0);
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
158 ctx.lineTo(x, h);
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
159 ctx.stroke();
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
160 ctx.restore();
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
161 }
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
162 }
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
163 drawTemps(ctx, [w, h], [yLo, yHi]) {
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
164 const lo = Math.floor(yLo);
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
165 for (let deg = lo; deg < yHi; deg++) {
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
166 const ones = deg % 10;
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
167 let opacity;
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
168 if (ones === 0) {
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
169 opacity = 1;
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
170 }
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
171 else if (ones === 5) {
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
172 opacity = 0.5;
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
173 }
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
174 else {
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
175 opacity = 0.15;
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
176 }
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
177 const y = project1D(deg, h, [yHi, yLo]);
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
178 ctx.save();
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
179 ctx.globalAlpha = opacity;
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
180 ctx.beginPath();
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
181 ctx.moveTo(0, y);
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
182 ctx.lineTo(w, y);
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
183 ctx.stroke();
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
184 ctx.restore();
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
185 }
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
186 }
19
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
187 size() {
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
188 const cssSize = this.element.getBoundingClientRect();
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
189 return [cssSize.width, cssSize.height];
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
190 }
33
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
191 stroke() {
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
192 return getComputedStyle(this.element).color;
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
193 }
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
194 font() {
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
195 return getComputedStyle(this.element).fontFamily;
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
196 }
19
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
197 }
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
198 function niceNumber(n) {
33
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
199 return Math.floor(n).toLocaleString('en-us').replace('-', '−');
19
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
200 }
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
201 const EDGE_FRACTION = 0.125;
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
202 function calculateYRange(ys) {
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
203 const yMax = Math.max(...ys);
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
204 const yMin = Math.min(...ys);
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
205 const yMid = (yMin + yMax) / 2;
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
206 const lastY = ys[ys.length - 1];
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
207 const yLo = yMid - Y_DEGREE_RANGE / 2;
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
208 const yProportion = Math.max(Math.min((lastY - yLo) / Y_DEGREE_RANGE, 1 - EDGE_FRACTION), EDGE_FRACTION);
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
209 const rangeLo = lastY - yProportion * Y_DEGREE_RANGE;
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
210 return [rangeLo, rangeLo + Y_DEGREE_RANGE];
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
211 }
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
212 function project(coord, size, xRange, yRange) {
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
213 const [x, y] = coord;
33
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
214 const [yMax, yMin] = yRange;
19
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
215 const [xSize, ySize] = size;
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
216 return [
33
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
217 project1D(x, xSize, xRange),
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
218 project1D(y, ySize, [yMin, yMax]),
19
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
219 ];
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
220 }
33
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
221 function project1D(coord, size, range) {
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
222 const [min, max] = range;
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
223 const span = max - min;
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
224 return (coord - min) / span * size;
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
225 }
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
226 const MINUTE = 60;
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
227 function minutesOf([start, end], zone) {
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
228 const formatter = new Intl.DateTimeFormat('sv', { timeZone: zone, hour: '2-digit', minute: '2-digit' });
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
229 const startSeconds = MINUTE * Math.floor(start / MINUTE);
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
230 const result = [];
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
231 for (let t = startSeconds; t <= end; t += MINUTE) {
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
232 result.push([t, goodBitsOnly(formatter.format(new Date(t * 1000)))]);
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
233 }
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
234 return result;
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
235 }
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
236 function goodBitsOnly(s) {
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
237 return s.replace(/[^0-9 :-]/, '');
beedfa8eaa3f Add grid lines to the graph.
Paul Fisher <paul@pfish.zone>
parents: 28
diff changeset
238 }
19
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
239 });
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
240 //# sourceMappingURL=graph.js.map