annotate weather_server/static/graph.js @ 25:a4147ecb18b3

ignore all .vscode nonsense.
author Paul Fisher <paul@pfish.zone>
date Sun, 10 Nov 2019 23:43:37 -0500
parents 47987502bf4c
children f817fa785c93
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) {
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
2 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
3 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
4 function rejected(value) { try { step(generator["throw"](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 step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
6 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
7 });
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 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
10 "use strict";
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
11 Object.defineProperty(exports, "__esModule", { value: true });
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
12 /** Converts Celsius to Fahrenheit. */
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
13 function cToF(tempC) {
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
14 return tempC * 9 / 5 + 32;
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
15 }
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
16 exports.cToF = cToF;
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
17 const MAGNUS_B = 17.62;
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
18 const MAGNUS_C = 243.12;
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
19 /** The gamma function to calculate dew point. */
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
20 function gammaFn(tempC, rhPct) {
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
21 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
22 }
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
23 /** Calculates the dew point. */
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
24 function dewPointC(tempC, rhPct) {
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
25 const gamma = gammaFn(tempC, rhPct);
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
26 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
27 }
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
28 exports.dewPointC = dewPointC;
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
29 });
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
30 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
31 "use strict";
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
32 Object.defineProperty(exports, "__esModule", { value: true });
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
33 /** The amount of time we will draw on the graph. */
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
34 const HISTORY_SECONDS = 60 * 60 * 4;
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
35 /** The amount of buffer room we will request before HISTORY_SECONDS. */
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
36 const BUFFER_SECONDS = 300;
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
37 /**
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
38 * Sets up everything.
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
39 * @param tempElement The element where temperature data is.
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
40 * @param dewPointElement The element where the dew point is.
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
41 */
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
42 function setUp(root, tempElement, dewPointElement) {
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
43 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
44 const nowTS = new Date().getTime() / 1000;
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
45 const startTS = nowTS - HISTORY_SECONDS;
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
46 const query = new URL('?', location.href);
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
47 query.pathname = query.pathname + '/recent';
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
48 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
49 const results = yield fetch(query.href);
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
50 if (!results.ok)
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
51 return;
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
52 const data = yield results.json();
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
53 const readings = data.readings;
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
54 if (readings.length === 0)
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
55 return;
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
56 root.classList.remove('plain');
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
57 root.classList.add('fancy');
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
58 const tempsF = readings.map(s => [s.sample_time, math_1.cToF(s.temp_c)]);
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
59 const dewPointsF = readings.map(s => [s.sample_time, math_1.cToF(math_1.dewPointC(s.temp_c, s.rh_pct))]);
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
60 setUpElement(tempElement, [startTS, nowTS], tempsF);
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
61 setUpElement(dewPointElement, [startTS, nowTS], dewPointsF);
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
62 });
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
63 }
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
64 exports.setUp = setUp;
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
65 /**
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
66 * Sets up charting for this element.
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
67 * @param element The element to put a graph in.
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
68 * @param timeRange The `[start, end]` of the time range.
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
69 * @param data The data to chart.
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
70 */
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
71 function setUpElement(element, timeRange, data) {
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
72 if (data.length === 0)
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
73 return;
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
74 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
75 chart.resize();
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
76 addEventListener('resize', () => chart.resize());
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
77 }
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
78 /** The number of degrees that the graph shows vertically. */
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
79 const Y_DEGREE_RANGE = 10;
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
80 const LINE_WIDTH_PX = 2;
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
81 const FONT_SIZE = '40px';
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
82 class Chart {
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 * Creates a new chart.
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
85 * @param element The parent element to create `<canvas>`-based chart in.
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
86 * @param timeRange `[start, end]` of the range to chart as Unix timestamps.
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
87 * @param data The data to chart.
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
88 */
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
89 constructor(element, timeRange, data) {
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
90 this.element = element;
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
91 this.timeRange = timeRange;
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
92 this.data = data;
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
93 this.canvas = document.createElement('canvas');
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
94 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
95 const unit = element.getElementsByClassName('unit')[0];
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
96 this.unit = unit && unit.textContent || '';
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
97 }
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
98 resize() {
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
99 const dpr = self.devicePixelRatio || 1;
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
100 const [w, h] = this.size();
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
101 const pxSize = [w * dpr, h * dpr];
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
102 this.canvas.width = pxSize[0];
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
103 this.canvas.height = pxSize[1];
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
104 const ctx = this.canvas.getContext('2d');
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
105 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
106 ctx.scale(dpr, dpr);
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
107 this.redraw(ctx);
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
108 }
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
109 redraw(ctx) {
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
110 const stroke = getComputedStyle(this.element).color;
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
111 const family = getComputedStyle(this.element).fontFamily;
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
112 ctx.strokeStyle = stroke;
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
113 ctx.lineJoin = 'round';
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
114 ctx.lineWidth = LINE_WIDTH_PX;
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
115 ctx.font = `bold ${FONT_SIZE} ${family}`;
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
116 const yRange = calculateYRange(this.data.map(d => d[1]));
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
117 const [fullW, fullH] = this.size();
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
118 const [xPad, yMargin] = this.measureMargin(ctx);
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
119 const graphSize = [fullW - xPad, fullH];
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
120 ctx.beginPath();
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
121 for (const pt of this.data) {
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
122 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
123 ctx.lineTo(...projected);
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
124 }
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
125 ctx.stroke();
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
126 ctx.beginPath();
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
127 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
128 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
129 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
130 ctx.fillStyle = getComputedStyle(this.element).backgroundColor;
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
131 ctx.fill();
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
132 ctx.stroke();
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
133 ctx.fillStyle = stroke;
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
134 ctx.textAlign = 'left';
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
135 ctx.textBaseline = 'top';
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
136 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
137 }
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
138 measureMargin(ctx) {
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
139 const bbox = ctx.measureText(`−99 ${this.unit}`);
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
140 const margin = 5 * LINE_WIDTH_PX + // margin to text
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
141 bbox.width + // max (?) width of text
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
142 16; // Pixel margin to wall.
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
143 return [margin, -31.5 / 2];
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
144 }
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
145 size() {
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
146 const cssSize = this.element.getBoundingClientRect();
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
147 return [cssSize.width, cssSize.height];
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
148 }
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
149 }
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
150 function niceNumber(n) {
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
151 return Math.round(n).toLocaleString('en-us').replace('-', '−');
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
152 }
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
153 /** The closest that the last point will be allowed to get to the edge. */
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
154 const EDGE_FRACTION = 0.125;
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
155 /**
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
156 * Determines what the Y range of the chart should be.
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
157 * @param ys The Y values of the chart.
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
158 * @return The lowest and highest values of the range.
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
159 */
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
160 function calculateYRange(ys) {
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
161 const yMax = Math.max(...ys);
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
162 const yMin = Math.min(...ys);
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
163 const yMid = (yMin + yMax) / 2;
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
164 const lastY = ys[ys.length - 1];
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
165 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
166 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
167 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
168 return [rangeLo, rangeLo + Y_DEGREE_RANGE];
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
169 }
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
170 /**
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
171 * Projects a Cartesian coordinate into Canvas space.
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
172 *
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
173 * @param coord The `[x, y]` coordinate to project.
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
174 * @param size The `[width, height]` of the context.
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
175 * @param xRange The range of X values in the context.
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
176 * @param yRange The range of Y values in the context.
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
177 * @return The `[x, y]` coordinate in Canvas space.
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
178 */
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
179 function project(coord, size, xRange, yRange) {
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
180 const [x, y] = coord;
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
181 const [xMin, xMax] = xRange;
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
182 const xSpan = xMax - xMin;
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
183 const [yMin, yMax] = yRange;
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
184 const ySpan = yMax - yMin;
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
185 const [xSize, ySize] = size;
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
186 return [
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
187 (x - xMin) / xSpan * xSize,
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
188 (yMax - y) / ySpan * ySize,
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
189 ];
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
190 }
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
191 });
47987502bf4c Add graph, make it public, and bump the version.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
192 //# sourceMappingURL=graph.js.map