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 getcomb(lo, hi) {
17 var comb = [], lb = [];
19 var ord = Math.pow(10, Math.floor(Math.log10(d)));
20 var scl = Math.floor(d / ord);
21 var inc, inc2, first, x, lb;
23 if (scl < 2) { inc = 0.1; inc2 = 0.5; }
24 else if (scl < 5) { inc = 0.2; inc2 = 1; }
25 else { inc = 0.5; inc2 = 2; }
28 first = (Math.floor(lo / inc) + 1) * inc;
29 for (x = 0; x < (d / inc) - 1.5; x++)
30 comb.push(first + inc * x);
31 first = (Math.floor(lo / inc2) + 1) * inc2;
32 for (x = 0; x < (d / inc2) - 1.5; x++)
33 lb.push(first + inc2 * x);
34 //dbg.innerHTML = "ord=" + ord + "<br>inc=" + inc + "<br>"
35 // + comb + "<br>" + lb;
40 return xzero + ((x - tmin) * tfact);
44 return wh - yzero - (y * hfact);
49 ctx.moveTo(px(tmin), py(0));
50 ctx.lineTo(px(tmax), py(0));
51 ctx.strokeStyle = "black";
56 var comb = getcomb(0, hmax);
60 for (i = 0; comb[0][i]; i++) {
61 ctx.moveTo(px(tmin) - 5, py(comb[0][i]));
62 ctx.lineTo(px(tmax), py(comb[0][i]));
64 ctx.strokeStyle = "lightgray";
68 ctx.moveTo(px(tmin), py(0));
69 ctx.lineTo(px(tmin), py(hmax));
70 ctx.strokeStyle = "black";
73 ctx.fillStyle = "black";
74 ctx.font = "bold 8px Courier";
75 ctx.fillText(0, px(tmin) - 8, py(0));
76 for (i = 0; comb[1][i]; i++) {
77 ctx.fillText(comb[1][i], px(tmin) - xzero + 2, py(comb[1][i]));
79 ctx.fillText("l/h", px(tmin) + 2, py(hmax) + 8);
82 /* @ updates global var `hmax` */
83 function differentiate(times) {
87 for (i = 0; i < times.length - 1; i++) {
88 dv = times[i+1][1] - times[i][1];
89 dt = times[i+1][0] - times[i][0];
90 if (dt != 0 && dv != 0) {
91 v = (dv / dt) * 360 ; /* Litres per hour */
92 if (hmax < v) hmax = v;
93 res.push([times[i][0], v]);
96 res.push([times[i][0], v]);
101 function drawplot(data, color) {
105 ctx.moveTo(px(data[0][0]), py(data[0][1]));
106 for (i = 1; i < data.length; i++) {
107 ctx.lineTo(px(data[i][0]), py(data[i - 1][1]));
108 ctx.lineTo(px(data[i][0]), py(data[i][1]));
110 ctx.strokeStyle = color;
114 function showloading() {
115 ctx.fillStyle = "green";
116 ctx.font = "bold 16px Courier";
117 ctx.fillText("loading", (ww / 2) - 40 , (wh / 2) + 8);
120 function clearplot() {
121 ctx.clearRect(0, 0, ww, wh);
125 if (cold_d.length && hot_d.length) {
126 tfact = (ww - xzero) / (tmax - tmin);
127 hfact = (wh - yzero) / hmax;
131 drawplot(cold_d, "blue");
132 drawplot(hot_d, "red");
138 function gotdata(data) {
139 document.getElementById("cold").innerHTML =
140 (data.current.cold / 100).toFixed(2);
141 document.getElementById("hot").innerHTML =
142 (data.current.hot / 100).toFixed(2);
143 tmin = data.range.lo;
144 tmax = data.range.hi;
145 /* differetiate() updates hmax */
147 cold_d = differentiate(data.cold);
148 hot_d = differentiate(data.hot);
149 //dbg.innerHTML = "hmax=" + hmax + " hfact=" + hfact + "<br>"
150 // + cold_d + "<br>" + hot_d;
154 function iso2qu(idate) {
155 return idate.replace("T", "+").replace("0Z", "");
158 function sendquery(lo, hi) {
159 var url = "query.cgi" + "?lo=" + iso2qu(lo) + "&" + iso2qu(hi);
161 //dbg.innerHTML = url;
162 xmlhttp.onreadystatechange = function() {
163 if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
164 // dbg.innerHTML = xmlhttp.responseText;
165 var myData = JSON.parse(xmlhttp.responseText);
169 xmlhttp.open("GET", url, true);
174 ww = window.innerWidth - 4;
175 if (ww > window.innerHeight) ww = window.innerHeight;
179 canvas.style.width = ww + "px";
180 canvas.style.height = wh + "px";
184 function initialize() {
185 var tdy = new Date();
188 tdy.setMilliseconds(0);
192 tmr = new Date(1*tdy + 86400000);
193 dbg = document.getElementById("debug");
194 canvas = document.getElementById("plot");
195 ctx = canvas.getContext("2d");
198 xmlhttp = new XMLHttpRequest();
199 sendquery(tdy.toISOString(), tmr.toISOString());
203 if(window.attachEvent) {
204 window.attachEvent('onload', initialize);
205 window.attachEvent('onresize', resize);
207 window.onload = initialize;
208 window.onresize = resize;
229 border: solid 1px black;
234 background-color: #d0e0ff;
238 background-color: #ffd0e0;
246 border: solid 1px black;
252 <title>Water Meters</title>
254 <h1>Water Meters</h1>
255 <div id="currentvals">
257 <div class="current" id="cold">cold</div>
258 <div class="current" id="hot">hot</div>
261 <canvas id="plot" width="640" height = "320"></canvas>