1 <?xml version="1.0" encoding="utf-8"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4 <html xml:lang="en" lang="en" xmlns="http://www.w3.org/1999/xhtml">
11 var tmin, tmax, tfact;
12 var xzero = 40, yzero = 48;
13 var cold_d = [], hot_d = [];
15 function showdate(utime) {
16 var dt = new Date(utime*1000);
17 return dt.toLocaleDateString() + " " + dt.toLocaleTimeString();
21 return xzero + ((x - tmin) * tfact);
24 function py(y) { return wh - yzero - (y * hfact);
27 const dow = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
28 const mn = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
29 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
31 function getcombx(lo, hi) {
32 var comb = [], lb = [];
33 var trange = tmax - tmin;
34 var inc, inc2, base, t, dt;
35 var label = function(t) { return "<<" + t.toFixed(1) + ">>"; }
36 function tohour(t) { var dt = new Date(t*1000); return dt.getHours(); }
37 function todow(t) { var dt = new Date(t*1000); return dow[dt.getDay()]; }
38 function todom(t) { var dt = new Date(t*1000); return dt.getDate(); }
39 function tomonth(t) { var dt = new Date(t*1000); return mn[dt.getMonth()]; }
41 if (trange < 172800) { /* two days -> one hour */
46 else if (trange < 864000) { /* 10 days -> six hours */
51 else if (trange < 2678400) { /* 31 days -> 1 day */
56 else { /* ~ one month */
57 /* TODO: make this a separate case with loop over months rather than
58 fixed number of seconds. */
64 dt = new Date(tmin*1000);
65 base = (Math.floor(tmin / inc) + 1) * inc + 60 * dt.getTimezoneOffset();
66 for (t = base; t < tmax; t += inc)
68 base = (Math.floor(tmin / inc2) + 1) * inc2 + 60 * dt.getTimezoneOffset();
69 for (t = base; t < tmax; t += inc2)
70 lb.push([t, label(t)]);
71 //dbg.innerHTML = "ord=" + ord + "<br>inc=" + inc + "<br>"
72 // + comb + "<br>" + lb;
77 var comb = getcombx(0, hmax);
81 for (i = 0; comb[0][i]; i++) {
82 ctx.moveTo(px(comb[0][i]), py(0) + 5);
83 ctx.lineTo(px(comb[0][i]), py(0));
85 ctx.strokeStyle = "gray";
89 ctx.moveTo(px(tmin), py(0));
90 ctx.lineTo(px(tmax), py(0));
91 ctx.strokeStyle = "black";
94 ctx.fillStyle = "black";
95 ctx.font = "bold 16px Courier";
96 ctx.textAlign = "center";
98 for (i = 0; comb[1][i]; i++) {
99 ctx.fillText(comb[1][i][1], px(comb[1][i][0]), py(0) + 20);
100 ctx.moveTo(px(comb[1][i][0]), py(0) + 5);
101 ctx.lineTo(px(comb[1][i][0]), py(0));
103 ctx.strokeStyle = "black";
106 ctx.fillStyle = "black";
107 ctx.textAlign = "left";
108 ctx.fillText(showdate(tmin), px(tmin), py(0) + 40);
109 ctx.textAlign = "right";
110 ctx.fillText(showdate(tmax), px(tmax), py(0) + 40);
113 function getcomby(lo, hi) {
114 var comb = [], lb = [];
116 var ord = Math.pow(10, Math.floor(Math.log10(d)));
117 var scl = Math.floor(d / ord);
118 var inc, inc2, first, x, lb;
120 if (scl < 2) { inc = 0.1; inc2 = 0.2; }
121 else if (scl < 5) { inc = 0.1; inc2 = 0.5; }
122 else { inc = 0.5; inc2 = 1; }
125 first = (Math.floor(lo / inc) + 1) * inc;
126 for (x = 0; x < (d / inc) - 1.2; x++)
127 comb.push(first + inc * x);
128 first = (Math.floor(lo / inc2) + 1) * inc2;
129 for (x = 0; x < (d / inc2) - 1.2; x++)
130 lb.push(first + inc2 * x);
131 //dbg.innerHTML = "ord=" + ord + "<br>inc=" + inc + "<br>"
132 // + comb + "<br>" + lb;
137 var comb = getcomby(0, hmax);
141 for (i = 0; comb[0][i]; i++) {
142 ctx.moveTo(px(tmin) - 5, py(comb[0][i]));
143 ctx.lineTo(px(tmax), py(comb[0][i]));
145 ctx.strokeStyle = "lightgray";
149 ctx.moveTo(px(tmin), py(0));
150 ctx.lineTo(px(tmin), py(hmax));
151 ctx.strokeStyle = "black";
154 ctx.fillStyle = "black";
155 ctx.font = "bold 16px Courier";
156 ctx.textAlign = "right";
157 ctx.fillText(0, px(tmin) - 6, py(0));
159 for (i = 0; comb[1][i]; i++) {
160 ctx.fillText(comb[1][i].toFixed(1), px(tmin) - 6, py(comb[1][i]));
161 ctx.moveTo(px(tmin) - 5, py(comb[1][i]));
162 ctx.lineTo(px(tmax), py(comb[1][i]));
164 ctx.strokeStyle = "gray";
167 ctx.textAlign = "left";
168 ctx.fillText("l/min", px(tmin) + 4, py(hmax) + 12);
171 /* @ updates global var `hmax` */
172 function differentiate(times) {
176 for (i = 0; i < times.length - 1; i++) {
177 dv = times[i+1][1] - times[i][1];
178 dt = times[i+1][0] - times[i][0];
179 if (dt != 0 && dv != 0) {
180 v = (dv / dt) * 600 ; /* Litres per min */
181 if (hmax < v) hmax = v;
182 res.push([times[i][0], v]);
185 if (i) res.push([times[i][0], v]);
190 function drawplot(data, color) {
194 ctx.moveTo(px(data[0][0]), py(data[0][1]));
195 for (i = 1; i < data.length; i++) {
196 ctx.lineTo(px(data[i][0]), py(data[i - 1][1]));
197 ctx.lineTo(px(data[i][0]), py(data[i][1]));
199 ctx.strokeStyle = color;
203 function showloading() {
204 ctx.fillStyle = "green";
205 ctx.font = "bold 16px Courier";
206 ctx.textAlign="center";
207 ctx.fillText("...loading...", (ww / 2) , (wh / 2) + 8);
210 function showempty() {
211 ctx.fillStyle = "red";
212 ctx.font = "bold 24px Courier";
213 ctx.textAlign="center";
214 ctx.fillText("No data for the requested time interval",
215 (ww / 2) , (wh / 2) + 8);
218 function clearplot() {
219 ctx.clearRect(0, 0, ww, wh);
224 if (cold_d.length || hot_d.length) {
225 tfact = (ww - xzero) / (tmax - tmin);
226 hfact = (wh - yzero) / hmax;
229 drawplot(cold_d, "blue");
230 drawplot(hot_d, "red");
236 function gotdata(data) {
237 document.getElementById("cold").innerHTML =
238 (data.current.cold / 100).toFixed(2);
239 document.getElementById("hot").innerHTML =
240 (data.current.hot / 100).toFixed(2);
241 tmin = data.range.lo;
242 tmax = data.range.hi;
243 //dbg.innerHTML = "from " + tmin + " to " + tmax
244 // + "<br>from " + showdate(tmin) + " to " + showdate(tmax);
245 /* differetiate() updates hmax */
247 cold_d = differentiate(data.cold);
248 hot_d = differentiate(data.hot);
249 //dbg.innerHTML = "hmax=" + hmax + " hfact=" + hfact + "<br>"
250 // + cold_d + "<br>" + hot_d;
254 function sendreq(qstr) {
255 var url = "query.cgi" + qstr;
256 var xmlhttp = new XMLHttpRequest();
258 //dbg.innerHTML = url;
259 xmlhttp.onreadystatechange = function() {
260 if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
261 // dbg.innerHTML = xmlhttp.responseText;
262 var myData = JSON.parse(xmlhttp.responseText);
266 xmlhttp.open("GET", url, true);
272 function iso2qu(idate) {
273 return idate.replace("T", "+").replace("0Z", "");
276 function sendquery(lo, hi) {
277 return sendreq("?lo=" + iso2qu(lo) + "&hi=" + iso2qu(hi));
281 ww = window.innerWidth - 4;
282 if (ww > window.innerHeight) ww = window.innerHeight;
286 canvas.style.width = ww + "px";
287 canvas.style.height = wh + "px";
291 function daystart(date) {
292 date.setMilliseconds(0);
299 function prevweek() {
300 var tdy = daystart(new Date());
301 var dow = tdy.getDay();
304 wstart = new Date(1*tdy - 86400000 * (dow + 7));
305 wend = new Date(1*wstart + 86400000 * 7);
306 sendquery(wstart.toISOString(), wend.toISOString());
309 function thisweek() {
310 var tdy = daystart(new Date());
311 var dow = tdy.getDay();
314 wstart = new Date(1*tdy - 86400000 * dow);
315 wend = new Date(1*wstart + 86400000 * 7);
316 sendquery(wstart.toISOString(), wend.toISOString());
319 function beforeyesterday() {
320 var tdy = daystart(new Date());
321 var ytd = new Date(1*tdy - 86400000);
322 var byd = new Date(1*ytd - 86400000);
323 sendquery(byd.toISOString(), ytd.toISOString());
326 function yesterday() {
327 var tdy = daystart(new Date());
328 var ytd = new Date(1*tdy - 86400000);
329 sendquery(ytd.toISOString(), tdy.toISOString());
333 var tdy = daystart(new Date());
334 var tmr = new Date(1*tdy + 86400000);
335 sendquery(tdy.toISOString(), tmr.toISOString());
338 function initialize() {
339 var qstr = window.location.search;
341 dbg = document.getElementById("debug");
342 canvas = document.getElementById("plot");
343 ctx = canvas.getContext("2d");
345 if (qstr) sendreq(qstr);
348 document.getElementById("today").onclick = today;
349 document.getElementById("yesterday").onclick = yesterday;
350 document.getElementById("beforeyesterday").onclick = beforeyesterday;
351 document.getElementById("thisweek").onclick = thisweek;
352 document.getElementById("prevweek").onclick = prevweek;
356 window.onload = initialize;
357 window.onresize = resize;
361 font-family: PipeDream;
362 src: url('PIPED.TTF') format('truetype');
363 /* Free to use font from http://www.mlink.net/~paterson/jpfonts.htm */
368 font-family: PipeDream;
371 background-color: lightgray;
387 border: solid 1px black;
392 background-color: #d0e0ff;
396 background-color: #ffd0e0;
404 /* border: solid 1px black; */
413 display: inline-block;
417 border: solid 1px black;
419 background-color: lightgray;
427 transform: translate(0, -50%);
433 <title>Water Meters</title>
435 <h1>WATER METERS</h1>
436 <div id="currentvals">
438 <div class="current" id="cold">cold</div>
439 <div class="current" id="hot">hot</div>
442 <canvas id="plot" width="640" height = "320"></canvas>
445 <div class="query" id="prevweek"><div class="label">PREVIOUS WEEK</div></div>
446 <div class="query" id="beforeyesterday"><div class="label">DAY
447 BEFORE YESTERDAY</div></div>
448 <div class="query" id="yesterday"><div class="label">YESTERDAY</div></div>
449 <div class="query" id="today"><div class="label">TODAY</div></div>
450 <div class="query" id="thisweek"><div class="label">THIS WEEK</div></div>
453 <div id="debug"></div>