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">
12 var tmin, tmax, tfact;
13 var xzero = 20, yzero = 20;
14 var cold_d = [], hot_d = [];
16 function showdate(utime) {
17 var dt = new Date(utime*1000);
18 return dt.toLocaleDateString() + " " + dt.toLocaleTimeString();
21 function getcomb(lo, hi) {
22 var comb = [], lb = [];
24 var ord = Math.pow(10, Math.floor(Math.log10(d)));
25 var scl = Math.floor(d / ord);
26 var inc, inc2, first, x, lb;
28 if (scl < 2) { inc = 0.1; inc2 = 0.5; }
29 else if (scl < 5) { inc = 0.2; inc2 = 1; }
30 else { inc = 0.5; inc2 = 2; }
33 first = (Math.floor(lo / inc) + 1) * inc;
34 for (x = 0; x < (d / inc) - 1.5; x++)
35 comb.push(first + inc * x);
36 first = (Math.floor(lo / inc2) + 1) * inc2;
37 for (x = 0; x < (d / inc2) - 1.5; x++)
38 lb.push(first + inc2 * x);
39 //dbg.innerHTML = "ord=" + ord + "<br>inc=" + inc + "<br>"
40 // + comb + "<br>" + lb;
45 return xzero + ((x - tmin) * tfact);
49 return wh - yzero - (y * hfact);
54 ctx.moveTo(px(tmin), py(0));
55 ctx.lineTo(px(tmax), py(0));
56 ctx.strokeStyle = "black";
61 var comb = getcomb(0, hmax);
65 for (i = 0; comb[0][i]; i++) {
66 ctx.moveTo(px(tmin) - 5, py(comb[0][i]));
67 ctx.lineTo(px(tmax), py(comb[0][i]));
69 ctx.strokeStyle = "lightgray";
73 ctx.moveTo(px(tmin), py(0));
74 ctx.lineTo(px(tmin), py(hmax));
75 ctx.strokeStyle = "black";
78 ctx.fillStyle = "black";
79 ctx.font = "bold 8px Courier";
80 ctx.fillText(0, px(tmin) - 8, py(0));
81 for (i = 0; comb[1][i]; i++) {
82 ctx.fillText(comb[1][i], px(tmin) - xzero + 2, py(comb[1][i]));
84 ctx.fillText("l/h", px(tmin) + 2, py(hmax) + 8);
87 /* @ updates global var `hmax` */
88 function differentiate(times) {
92 for (i = 0; i < times.length - 1; i++) {
93 dv = times[i+1][1] - times[i][1];
94 dt = times[i+1][0] - times[i][0];
95 if (dt != 0 && dv != 0) {
96 v = (dv / dt) * 360 ; /* Litres per hour */
97 if (hmax < v) hmax = v;
98 res.push([times[i][0], v]);
101 res.push([times[i][0], v]);
106 function drawplot(data, color) {
110 ctx.moveTo(px(data[0][0]), py(data[0][1]));
111 for (i = 1; i < data.length; i++) {
112 ctx.lineTo(px(data[i][0]), py(data[i - 1][1]));
113 ctx.lineTo(px(data[i][0]), py(data[i][1]));
115 ctx.strokeStyle = color;
119 function showloading() {
120 ctx.fillStyle = "green";
121 ctx.font = "bold 16px Courier";
122 ctx.fillText("loading", (ww / 2) - 40 , (wh / 2) + 8);
125 function clearplot() {
126 ctx.clearRect(0, 0, ww, wh);
130 if (cold_d.length && hot_d.length) {
131 tfact = (ww - xzero) / (tmax - tmin);
132 hfact = (wh - yzero) / hmax;
136 drawplot(cold_d, "blue");
137 drawplot(hot_d, "red");
143 function gotdata(data) {
144 document.getElementById("cold").innerHTML =
145 (data.current.cold / 100).toFixed(2);
146 document.getElementById("hot").innerHTML =
147 (data.current.hot / 100).toFixed(2);
148 tmin = data.range.lo;
149 tmax = data.range.hi;
150 dbg.innerHTML = "from " + tmin + " to " + tmax
151 + "<br>from " + showdate(tmin) + " to " + showdate(tmax);
152 /* differetiate() updates hmax */
154 cold_d = differentiate(data.cold);
155 hot_d = differentiate(data.hot);
156 //dbg.innerHTML = "hmax=" + hmax + " hfact=" + hfact + "<br>"
157 // + cold_d + "<br>" + hot_d;
161 function iso2qu(idate) {
162 return idate.replace("T", "+").replace("0Z", "");
165 function sendquery(lo, hi) {
166 var url = "query.cgi" + "?lo=" + iso2qu(lo) + "&hi=" + iso2qu(hi);
168 //dbg.innerHTML = url;
169 xmlhttp.onreadystatechange = function() {
170 if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
171 // dbg.innerHTML = xmlhttp.responseText;
172 var myData = JSON.parse(xmlhttp.responseText);
176 xmlhttp.open("GET", url, true);
181 ww = window.innerWidth - 4;
182 if (ww > window.innerHeight) ww = window.innerHeight;
186 canvas.style.width = ww + "px";
187 canvas.style.height = wh + "px";
191 function initialize() {
192 var tdy = new Date();
195 tdy.setMilliseconds(0);
199 tmr = new Date(1*tdy + 86400000);
200 dbg = document.getElementById("debug");
201 canvas = document.getElementById("plot");
202 ctx = canvas.getContext("2d");
205 xmlhttp = new XMLHttpRequest();
206 sendquery(tdy.toISOString(), tmr.toISOString());
210 if(window.attachEvent) {
211 window.attachEvent('onload', initialize);
212 window.attachEvent('onresize', resize);
214 window.onload = initialize;
215 window.onresize = resize;
236 border: solid 1px black;
241 background-color: #d0e0ff;
245 background-color: #ffd0e0;
253 border: solid 1px black;
259 <title>Water Meters</title>
261 <h1>Water Meters</h1>
262 <div id="currentvals">
264 <div class="current" id="cold">cold</div>
265 <div class="current" id="hot">hot</div>
268 <canvas id="plot" width="640" height = "320"></canvas>