Mercurial > personal > weather-server
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 |
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 | 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 | 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 | 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 | 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 | 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 | 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 | 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 | 118 this.drawGrid(ctx, graphSize, yRange); |
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 | 131 drawGrid(ctx, size, yRange) { |
132 ctx.save(); | |
133 ctx.lineCap = 'butt'; | |
134 ctx.lineWidth = GRID_WIDTH_PX; | |
135 this.drawTime(ctx, size); | |
136 this.drawTemps(ctx, size, yRange); | |
137 ctx.restore(); | |
138 } | |
139 drawTime(ctx, [w, h]) { | |
140 const minutes = minutesOf(this.timeRange, this.timezone); | |
141 for (const [ts, hhmm] of minutes) { | |
142 const mins = Number(hhmm.split(':')[1]); | |
143 let opacity; | |
144 if (mins === 0) { | |
145 opacity = 1; | |
146 } | |
147 else if (mins % 15 === 0) { | |
148 opacity = 0.5; | |
149 } | |
150 else { | |
151 continue; | |
152 } | |
153 const x = project1D(ts, w, this.timeRange); | |
154 ctx.save(); | |
155 ctx.globalAlpha = opacity; | |
156 ctx.beginPath(); | |
157 ctx.moveTo(x, 0); | |
158 ctx.lineTo(x, h); | |
159 ctx.stroke(); | |
160 ctx.restore(); | |
161 } | |
162 } | |
163 drawTemps(ctx, [w, h], [yLo, yHi]) { | |
164 const lo = Math.floor(yLo); | |
165 for (let deg = lo; deg < yHi; deg++) { | |
166 const ones = deg % 10; | |
167 let opacity; | |
168 if (ones === 0) { | |
169 opacity = 1; | |
170 } | |
171 else if (ones === 5) { | |
172 opacity = 0.5; | |
173 } | |
174 else { | |
175 opacity = 0.15; | |
176 } | |
177 const y = project1D(deg, h, [yHi, yLo]); | |
178 ctx.save(); | |
179 ctx.globalAlpha = opacity; | |
180 ctx.beginPath(); | |
181 ctx.moveTo(0, y); | |
182 ctx.lineTo(w, y); | |
183 ctx.stroke(); | |
184 ctx.restore(); | |
185 } | |
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 | 191 stroke() { |
192 return getComputedStyle(this.element).color; | |
193 } | |
194 font() { | |
195 return getComputedStyle(this.element).fontFamily; | |
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 | 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 | 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 | 217 project1D(x, xSize, xRange), |
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 | 221 function project1D(coord, size, range) { |
222 const [min, max] = range; | |
223 const span = max - min; | |
224 return (coord - min) / span * size; | |
225 } | |
226 const MINUTE = 60; | |
227 function minutesOf([start, end], zone) { | |
228 const formatter = new Intl.DateTimeFormat('sv', { timeZone: zone, hour: '2-digit', minute: '2-digit' }); | |
229 const startSeconds = MINUTE * Math.floor(start / MINUTE); | |
230 const result = []; | |
231 for (let t = startSeconds; t <= end; t += MINUTE) { | |
232 result.push([t, goodBitsOnly(formatter.format(new Date(t * 1000)))]); | |
233 } | |
234 return result; | |
235 } | |
236 function goodBitsOnly(s) { | |
237 return s.replace(/[^0-9 :-]/, ''); | |
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 |