Mercurial > personal > weather-server
annotate weather_server/static/graph.js @ 47:d200755e0930 v0.1.5
go back to fahrenheit
| author | Paul Fisher <paul@pfish.zone> |
|---|---|
| date | Sat, 03 May 2025 20:12:28 -0400 |
| parents | 4af79d69b12e |
| 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'); |
| 47 | 55 const tempsK = readings.map(s => [s.sample_time, (0, math_1.cToK)(s.temp_c)]); |
| 56 const dewPointsK = readings.map(s => [s.sample_time, (0, math_1.cToK)((0, math_1.dewPointC)(s.temp_c, s.rh_pct))]); | |
| 57 setUpElement(tempElement, [startTS, nowTS], tempsK); | |
| 58 setUpElement(dewPointElement, [startTS, nowTS], dewPointsK); | |
|
19
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; |
| 33 | 77 this.timezone = timezone; |
|
19
47987502bf4c
Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
78 this.canvas = document.createElement('canvas'); |
|
47987502bf4c
Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
79 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
|
80 const unit = element.getElementsByClassName('unit')[0]; |
|
47987502bf4c
Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
81 this.unit = unit && unit.textContent || ''; |
| 47 | 82 const conversionText = unit.dataset.fromK || 'k'; |
| 83 this.conversion = eval(`k => ${conversionText}`); | |
| 84 this.data = data.map(([ts, k]) => [ts, this.conversion(k)]); | |
|
19
47987502bf4c
Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
85 } |
|
47987502bf4c
Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
86 resize() { |
|
47987502bf4c
Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
87 const dpr = self.devicePixelRatio || 1; |
|
47987502bf4c
Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
88 const [w, h] = this.size(); |
|
47987502bf4c
Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
89 const pxSize = [w * dpr, h * dpr]; |
|
47987502bf4c
Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
90 this.canvas.width = pxSize[0]; |
|
47987502bf4c
Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
91 this.canvas.height = pxSize[1]; |
|
47987502bf4c
Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
92 const ctx = this.canvas.getContext('2d'); |
|
47987502bf4c
Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
93 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
|
94 ctx.scale(dpr, dpr); |
|
47987502bf4c
Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
95 this.redraw(ctx); |
|
47987502bf4c
Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
96 } |
|
47987502bf4c
Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
97 redraw(ctx) { |
| 33 | 98 ctx.strokeStyle = this.stroke(); |
|
19
47987502bf4c
Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
99 ctx.lineJoin = 'round'; |
|
47987502bf4c
Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
100 ctx.lineWidth = LINE_WIDTH_PX; |
| 33 | 101 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
|
102 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
|
103 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
|
104 const [fullW, fullH] = this.size(); |
|
47987502bf4c
Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
105 const [xPad, yMargin] = this.measureMargin(ctx); |
|
47987502bf4c
Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
106 const graphSize = [fullW - xPad, fullH]; |
|
47987502bf4c
Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
107 ctx.beginPath(); |
|
47987502bf4c
Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
108 for (const pt of this.data) { |
|
47987502bf4c
Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
109 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
|
110 ctx.lineTo(...projected); |
|
47987502bf4c
Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
111 } |
|
47987502bf4c
Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
112 ctx.stroke(); |
|
47987502bf4c
Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
113 ctx.beginPath(); |
|
47987502bf4c
Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
114 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
|
115 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
|
116 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
|
117 ctx.fillStyle = getComputedStyle(this.element).backgroundColor; |
|
47987502bf4c
Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
118 ctx.fill(); |
|
47987502bf4c
Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
119 ctx.stroke(); |
| 33 | 120 this.drawGrid(ctx, graphSize, yRange); |
| 121 ctx.fillStyle = this.stroke(); | |
|
19
47987502bf4c
Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
122 ctx.textAlign = 'left'; |
|
47987502bf4c
Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
123 ctx.textBaseline = 'top'; |
|
47987502bf4c
Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
124 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
|
125 } |
|
47987502bf4c
Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
126 measureMargin(ctx) { |
|
47987502bf4c
Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
127 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
|
128 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
|
129 bbox.width + |
|
f817fa785c93
Make graph only consider visible points when setting range.
Paul Fisher <paul@pfish.zone>
parents:
19
diff
changeset
|
130 16; |
|
19
47987502bf4c
Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
131 return [margin, -31.5 / 2]; |
|
47987502bf4c
Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
132 } |
| 33 | 133 drawGrid(ctx, size, yRange) { |
| 134 ctx.save(); | |
| 135 ctx.lineCap = 'butt'; | |
| 136 ctx.lineWidth = GRID_WIDTH_PX; | |
| 137 this.drawTime(ctx, size); | |
| 138 this.drawTemps(ctx, size, yRange); | |
| 139 ctx.restore(); | |
| 140 } | |
| 141 drawTime(ctx, [w, h]) { | |
| 142 const minutes = minutesOf(this.timeRange, this.timezone); | |
| 143 for (const [ts, hhmm] of minutes) { | |
| 144 const mins = Number(hhmm.split(':')[1]); | |
| 145 let opacity; | |
| 146 if (mins === 0) { | |
| 147 opacity = 1; | |
| 148 } | |
| 149 else if (mins % 15 === 0) { | |
| 150 opacity = 0.5; | |
| 151 } | |
| 152 else { | |
| 153 continue; | |
| 154 } | |
| 155 const x = project1D(ts, w, this.timeRange); | |
| 156 ctx.save(); | |
| 157 ctx.globalAlpha = opacity; | |
| 158 ctx.beginPath(); | |
| 159 ctx.moveTo(x, 0); | |
| 160 ctx.lineTo(x, h); | |
| 161 ctx.stroke(); | |
| 162 ctx.restore(); | |
| 163 } | |
| 164 } | |
| 165 drawTemps(ctx, [w, h], [yLo, yHi]) { | |
| 166 const lo = Math.floor(yLo); | |
| 167 for (let deg = lo; deg < yHi; deg++) { | |
| 168 const ones = deg % 10; | |
| 169 let opacity; | |
| 170 if (ones === 0) { | |
| 171 opacity = 1; | |
| 172 } | |
| 173 else if (ones === 5) { | |
| 174 opacity = 0.5; | |
| 175 } | |
| 176 else { | |
| 177 opacity = 0.15; | |
| 178 } | |
| 179 const y = project1D(deg, h, [yHi, yLo]); | |
| 180 ctx.save(); | |
| 181 ctx.globalAlpha = opacity; | |
| 182 ctx.beginPath(); | |
| 183 ctx.moveTo(0, y); | |
| 184 ctx.lineTo(w, y); | |
| 185 ctx.stroke(); | |
| 186 ctx.restore(); | |
| 187 } | |
| 188 } | |
|
19
47987502bf4c
Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
189 size() { |
|
47987502bf4c
Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
190 const cssSize = this.element.getBoundingClientRect(); |
|
47987502bf4c
Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
191 return [cssSize.width, cssSize.height]; |
|
47987502bf4c
Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
192 } |
| 33 | 193 stroke() { |
| 194 return getComputedStyle(this.element).color; | |
| 195 } | |
| 196 font() { | |
| 197 return getComputedStyle(this.element).fontFamily; | |
| 198 } | |
|
19
47987502bf4c
Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
199 } |
|
47987502bf4c
Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
200 function niceNumber(n) { |
| 33 | 201 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
|
202 } |
|
47987502bf4c
Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
203 const EDGE_FRACTION = 0.125; |
|
47987502bf4c
Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
204 function calculateYRange(ys) { |
|
47987502bf4c
Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
205 const yMax = Math.max(...ys); |
|
47987502bf4c
Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
206 const yMin = Math.min(...ys); |
|
47987502bf4c
Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
207 const yMid = (yMin + yMax) / 2; |
|
47987502bf4c
Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
208 const lastY = ys[ys.length - 1]; |
|
47987502bf4c
Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
209 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
|
210 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
|
211 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
|
212 return [rangeLo, rangeLo + Y_DEGREE_RANGE]; |
|
47987502bf4c
Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
213 } |
|
47987502bf4c
Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
214 function project(coord, size, xRange, yRange) { |
|
47987502bf4c
Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
215 const [x, y] = coord; |
| 33 | 216 const [yMax, yMin] = yRange; |
|
19
47987502bf4c
Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
217 const [xSize, ySize] = size; |
|
47987502bf4c
Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
218 return [ |
| 33 | 219 project1D(x, xSize, xRange), |
| 220 project1D(y, ySize, [yMin, yMax]), | |
|
19
47987502bf4c
Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
221 ]; |
|
47987502bf4c
Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
222 } |
| 33 | 223 function project1D(coord, size, range) { |
| 224 const [min, max] = range; | |
| 225 const span = max - min; | |
| 226 return (coord - min) / span * size; | |
| 227 } | |
| 228 const MINUTE = 60; | |
| 229 function minutesOf([start, end], zone) { | |
| 230 const formatter = new Intl.DateTimeFormat('sv', { timeZone: zone, hour: '2-digit', minute: '2-digit' }); | |
| 231 const startSeconds = MINUTE * Math.floor(start / MINUTE); | |
| 232 const result = []; | |
| 233 for (let t = startSeconds; t <= end; t += MINUTE) { | |
| 234 result.push([t, goodBitsOnly(formatter.format(new Date(t * 1000)))]); | |
| 235 } | |
| 236 return result; | |
| 237 } | |
| 238 function goodBitsOnly(s) { | |
| 239 return s.replace(/[^0-9 :-]/, ''); | |
| 240 } | |
|
19
47987502bf4c
Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
241 }); |
|
47987502bf4c
Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
242 //# sourceMappingURL=graph.js.map |
