annotate weather_server/static/script.js @ 14:dd77a7ee02c1

Create tiny AMD loader and set up tsc for it.
author Paul Fisher <paul@pfish.zone>
date Wed, 09 Oct 2019 23:14:16 -0400
parents 4eaa9d69c4e2
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
12
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
1 'use strict';
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
2
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
3 /**
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
4 * Converts Celsius to Fahrenheit.
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
5 * @param {number} tempC
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
6 * @return {number} The temperature in Fahrenheit.
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
7 */
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
8 function cToF(tempC) {
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
9 return tempC * 9 / 5 + 32;
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
10 }
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
11
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
12 const MAGNUS_B = 17.62;
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
13 const MAGNUS_C = 243.12;
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
14
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
15 /**
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
16 * The gamma function to calculate dew point.
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
17 *
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
18 * @param {number} tempC The temperature, in degrees Celsius.
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
19 * @param {number} rhPct The relative humidity, in percent.
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
20 * @return {number} The value of the gamma function.
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
21 */
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
22 function gammaFn(tempC, rhPct) {
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
23 return Math.log(rhPct / 100) + MAGNUS_B * tempC / (MAGNUS_C + tempC);
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
24 }
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
25
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
26 /**
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
27 * Calculates the dew point.
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
28 *
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
29 * @param {number} tempC The temperature, in degrees Celsius.
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
30 * @param {number} rhPct The relative humidity, in percent.
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
31 * @return {number} The dew point, in degrees Celsius.
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
32 */
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
33 function dewPointC(tempC, rhPct) {
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
34 const gamma = gammaFn(tempC, rhPct);
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
35 return MAGNUS_C * gamma / (MAGNUS_B - gamma);
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
36 }
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
37
13
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
38 const HISTORY_SECONDS = 86400;
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
39
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
40 /**
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
41 * Sets up everything.
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
42 * @param {HTMLElement} tempElement The element where temperature data is.
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
43 * @param {HTMLElement} dewPointElement The element where the dew point is.
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
44 */
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
45 async function setUp(tempElement, dewPointElement) {
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
46 const nowTS = new Date().getTime() / 1000;
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
47 const startTS = nowTS - HISTORY_SECONDS;
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
48 const query = new URL(location.href);
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
49 query.pathname = query.pathname + '/recent';
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
50 query.search = '';
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
51 query.searchParams.set('seconds', String(HISTORY_SECONDS));
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
52 const results = await fetch(query.href);
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
53 if (!results.ok) return;
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
54 const data = await results.json();
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
55 if (data.length === 0) return;
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
56
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
57 const tempsF = data.map(s => [s.sample_time, cToF(s.temp_c)]);
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
58 const dewPointsF = data.map(
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
59 s => [s.sample_time, cToF(dewPointC(s.temp_c, s.rh_pct))]);
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
60 setUpElement(tempElement, [startTS, nowTS], tempsF);
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
61 setUpElement(dewPointElement, [startTS, nowTS], dewPointsF);
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
62 }
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
63
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
64 /**
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
65 * Sets up charting for this element.
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
66 * @param {HTMLElement} element The element to put a graph in.
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
67 * @param {[number, number]} timeRange The `[start, end]` of the time range.
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
68 * @param {[number, number][]} data The data to chart.
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
69 */
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
70 function setUpElement(element, timeRange, data) {
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
71 element.insertBefore(document.createElement('canvas'), element.firstChild);
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
72 element.classList.remove('plain');
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
73 element.classList.add('fancy');
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
74 const doDraw = () => redrawCanvas(element, data, timeRange);
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
75 doDraw();
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
76 addEventListener('resize', doDraw);
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
77 }
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
78
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
79 /**
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
80 *
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
81 * @param {HTMLElement} element The parent element to put the `<canvas>` in.
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
82 * @param {[number, number][]} data The data to chart.
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
83 * @param {[number, number]} xRange The `[start, end]` of the X range to plot.
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
84 */
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
85 function redrawCanvas(element, data, xRange) {
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
86 let canvas = element.getElementsByTagName('canvas')[0];
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
87 if (!canvas) {
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
88 canvas = document.createElement('canvas');
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
89 element.insertBefore(canvas, element.firstChild);
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
90 }
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
91 const dpr = window.devicePixelRatio || 1;
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
92 const cssSize = element.getBoundingClientRect();
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
93 const pxSize = [cssSize.width * dpr, cssSize.height * dpr];
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
94 canvas.width = pxSize[0];
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
95 canvas.height = pxSize[1];
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
96 const ctx = canvas.getContext('2d');
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
97 ctx.clearRect(0, 0, pxSize[0], pxSize[1]);
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
98 const computed = getComputedStyle(element);
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
99 ctx.strokeStyle = computed.color;
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
100 drawChart(ctx, data, xRange, pxSize);
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
101 }
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
102
12
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
103 /** The height of the chart in degrees. */
13
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
104 const CHART_RANGE_DEGREES = 15;
12
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
105
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
106 /**
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
107 * Charts some data.
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
108 *
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
109 * @param {CanvasRenderingContext2D} ctx The context to draw in.
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
110 * @param {[number, number][]} data The data to chart, as `[x, y]` pairs.
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
111 * @param {[number, number]} xRange The bounds of the X axis to draw.
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
112 * @param {[number, number]} size The `[width, height]` of the context.
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
113 */
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
114 function drawChart(ctx, data, xRange, size) {
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
115 const yRange = calculateYRange(data.map(d => d[1]));
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
116
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
117 ctx.lineWidth = 1.5;
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
118
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
119 ctx.beginPath();
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
120 const first = project(data[0], size, xRange, yRange);
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
121 ctx.moveTo(...first);
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
122 for (const pt of data) {
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
123 const projected = project(pt, size, xRange, yRange);
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
124 ctx.lineTo(...projected);
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
125 }
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
126 ctx.stroke();
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
127 }
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
128
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
129 /**
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
130 * Determines what the Y range of the chart should be.
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
131 * @param {number[]} ys The Y values of the chart.
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
132 * @return {[number, number]} The lowest and highest values of the range.
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
133 */
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
134 function calculateYRange(ys) {
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
135 const yMax = Math.max(...ys);
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
136 const yMin = Math.min(...ys);
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
137 const yMid = (yMin + yMax) / 2;
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
138 const lastY = ys[ys.length - 1];
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
139
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
140 // We want the last value to be, at most, at the top or bottom 1/4 line
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
141 // of the chart.
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
142
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
143 // If the middle of the range is already close enough, just use that.
13
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
144 if (CHART_RANGE_DEGREES / 4 <= Math.abs(yMid - lastY)) {
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
145 return [yMid - CHART_RANGE_DEGREES / 2, yMid + CHART_RANGE_DEGREES / 2];
12
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
146 }
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
147 // Otherwise, clamp the chart range.
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
148 if (lastY < yMid) {
13
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
149 return [lastY - CHART_RANGE_DEGREES / 4, lastY + 3 * CHART_RANGE_DEGREES / 4];
12
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
150 }
13
4eaa9d69c4e2 Extremely basic version of the graph drawer.
Paul Fisher <paul@pfish.zone>
parents: 12
diff changeset
151 return [lastY - 3 * CHART_RANGE_DEGREES / 4, lastY + CHART_RANGE_DEGREES / 4];
12
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
152 }
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
153
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
154 /**
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
155 * Projects a Cartesian coordinate into Canvas space.
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
156 *
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
157 * @param {[number, number]} coord The `[x, y]` coordinate to project.
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
158 * @param {[number, number]} size The `[width, height]` of the context.
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
159 * @param {[number, number]} xRange The range of X values in the context.
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
160 * @param {[number, number]} yRange The range of Y values in the context.
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
161 * @return {[number, number]} The `[x, y]` coordinate in Canvas space.
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
162 */
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
163 function project(coord, size, xRange, yRange) {
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
164 const [x, y] = coord;
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
165 const [xMin, xMax] = xRange;
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
166 const xSpan = xMax - xMin;
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
167 const [yMin, yMax] = yRange;
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
168 const ySpan = yMax - yMin;
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
169 const [xSize, ySize] = size;
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
170 return [
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
171 (x - xMin) / xSpan * xSize,
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
172 (yMax- y) / ySpan * ySize,
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
173 ]
9e6289598d8c Add script to draw charts with initial functions.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
174 }