<html xml:lang="en" lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<script>
- var canvas;
- var ctx;
- var ww;
- var wh;
- var hmax = 0;
- var hfact;
- var tmin;
- var tmax;
- var tfact;
- var xzero = 20;
- var yzero = 20;
+ var dbg;
+ var xmlhttp;
+ var canvas, ctx;
+ var ww, wh;
+ var hmax, hfact;
+ var tmin, tmax, tfact;
+ var xzero = 20, yzero = 20;
+ var cold_d = [], hot_d = [];
+
+ function getcomb(lo, hi) {
+ var comb = [], lb = [];
+ var d = hi - lo;
+ var ord = Math.pow(10, Math.floor(Math.log10(d)));
+ var scl = Math.floor(d / ord);
+ var inc, inc2, first, x, lb;
+
+ if (scl < 2) { inc = 0.1; inc2 = 0.5; }
+ else if (scl < 5) { inc = 0.2; inc2 = 1; }
+ else { inc = 0.5; inc2 = 2; }
+ inc *= ord;
+ inc2 *= ord;
+ first = (Math.floor(lo / inc) + 1) * inc;
+ for (x = 0; x < (d / inc) - 1.5; x++)
+ comb.push(first + inc * x);
+ first = (Math.floor(lo / inc2) + 1) * inc2;
+ for (x = 0; x < (d / inc2) - 1.5; x++)
+ lb.push(first + inc2 * x);
+ //dbg.innerHTML = "ord=" + ord + "<br>inc=" + inc + "<br>"
+ // + comb + "<br>" + lb;
+ return [comb, lb];
+ }
+
function px(x) {
return xzero + ((x - tmin) * tfact);
}
+
function py(y) {
return wh - yzero - (y * hfact);
}
+
function xaxis() {
ctx.beginPath();
ctx.moveTo(px(tmin), py(0));
ctx.strokeStyle = "black";
ctx.stroke();
}
+
function yaxis() {
+ var comb = getcomb(0, hmax);
+ var i;
+
+ ctx.beginPath();
+ for (i = 0; comb[0][i]; i++) {
+ ctx.moveTo(px(tmin) - 5, py(comb[0][i]));
+ ctx.lineTo(px(tmax), py(comb[0][i]));
+ }
+ ctx.strokeStyle = "lightgray";
+ ctx.stroke();
+
ctx.beginPath();
ctx.moveTo(px(tmin), py(0));
ctx.lineTo(px(tmin), py(hmax));
ctx.strokeStyle = "black";
ctx.stroke();
+
+ ctx.fillStyle = "black";
+ ctx.font = "bold 8px Courier";
+ ctx.fillText(0, px(tmin) - 8, py(0));
+ for (i = 0; comb[1][i]; i++) {
+ ctx.fillText(comb[1][i], px(tmin) - xzero + 2, py(comb[1][i]));
+ }
+ ctx.fillText("l/h", px(tmin) + 2, py(hmax) + 8);
}
- function differentiate(times) { /* updates glbal hmax */
+
+ /* @ updates global var `hmax` */
+ function differentiate(times) {
var res = [];
var dv, dt, v;
dv = times[i+1][1] - times[i][1];
dt = times[i+1][0] - times[i][0];
if (dt != 0 && dv != 0) {
- v = dv / dt;
+ v = (dv / dt) * 360 ; /* Litres per hour */
if (hmax < v) hmax = v;
res.push([times[i][0], v]);
}
return res;
}
+
function drawplot(data, color) {
var i;
ctx.strokeStyle = color;
ctx.stroke();
}
- function gotdata(data) {
- var cold_d = [], hot_d = [];
+ function showloading() {
+ ctx.fillStyle = "green";
+ ctx.font = "bold 16px Courier";
+ ctx.fillText("loading", (ww / 2) - 40 , (wh / 2) + 8);
+ }
+
+ function clearplot() {
+ ctx.clearRect(0, 0, ww, wh);
+ }
+
+ function redraw() {
+ if (cold_d.length && hot_d.length) {
+ tfact = (ww - xzero) / (tmax - tmin);
+ hfact = (wh - yzero) / hmax;
+ clearplot();
+ xaxis();
+ yaxis();
+ drawplot(cold_d, "blue");
+ drawplot(hot_d, "red");
+ } else {
+ showloading();
+ }
+ }
+
+ function gotdata(data) {
document.getElementById("cold").innerHTML =
(data.current.cold / 100).toFixed(2);
document.getElementById("hot").innerHTML =
(data.current.hot / 100).toFixed(2);
- canvas = document.getElementById("plot");
-
- ctx = canvas.getContext("2d");
- ww = canvas.width;
- wh = canvas.height;
-
tmin = data.range.lo;
tmax = data.range.hi;
- tfact = (ww - xzero) / (tmax - tmin);
+ /* differetiate() updates hmax */
+ hmax = 0;
cold_d = differentiate(data.cold);
hot_d = differentiate(data.hot);
- /* differetiate() updates hmax */
- hfact = (wh - yzero) / hmax;
+ //dbg.innerHTML = "hmax=" + hmax + " hfact=" + hfact + "<br>"
+ // + cold_d + "<br>" + hot_d;
+ redraw();
+ }
+
+ function sendquery(lo, hi) {
+ var url = "query.cgi";
- document.getElementById("debug").innerHTML.append("<br>" ++ cold_d + "<br>" + hot_d);
+ if (lo && hi) url += "?lo=" + lo + "&" + hi;
+ else url = "query.cgi?lo=2015-12-19+00:00:00&hi=2015-12-20+00:00:00"; //FIX
+ xmlhttp.onreadystatechange = function() {
+ if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
+ // dbg.innerHTML = xmlhttp.responseText;
+ var myData = JSON.parse(xmlhttp.responseText);
+ gotdata(myData);
+ }
+ }
+ xmlhttp.open("GET", url, true);
+ xmlhttp.send();
+ }
- drawplot(cold_d, "blue");
- drawplot(hot_d, "red");
- xaxis();
- yaxis();
+ function resize() {
+ ww = window.innerWidth - 4;
+ if (ww > window.innerHeight) ww = window.innerHeight;
+ wh = ww / 2;
+ canvas.width = ww;
+ canvas.height = wh;
+ canvas.style.width = ww + "px";
+ canvas.style.height = wh + "px";
+ redraw();
+ }
+
+ function initialize() {
+ dbg = document.getElementById("debug");
+ canvas = document.getElementById("plot");
+ ctx = canvas.getContext("2d");
+ xmlhttp = new XMLHttpRequest();
+ resize();
+ sendquery();
+ }
+
+ /* Set up */
+ if(window.attachEvent) {
+ window.attachEvent('onload', initialize);
+ window.attachEvent('onresize', resize);
+ } else {
+ window.onload = initialize;
+ window.onresize = resize;
}
</script>
<style>
clear: both;
}
div#currentvals {
- width: 14em;
+ width: 18em;
margin-left: auto;
margin-right: auto;
margin-bottom: 10px;
background-color: #ffd0e0;
}
canvas#plot {
- padding-left: 0;
- padding-right: 0;
- margin-left: auto;
- margin-right: auto;
+ padding: 0px;
+ margin: auto;
display: block;
width: 640px;
height: 320px;
border: solid 1px black;
}
+body {
+ margin: 0px;
+}
</style>
<title>Water Meters</title>
</head><body>
<br />
<div id=debug>
</div>
-<script>
- var xmlhttp = new XMLHttpRequest();
- var url = "query.cgi";
- var qstr = "?lo=2015-12-19+00:00:00&hi=2015-12-20+00:00:00";
-
- xmlhttp.onreadystatechange = function() {
- if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
- document.getElementById("debug").innerHTML = xmlhttp.responseText;
- var myData = JSON.parse(xmlhttp.responseText);
- gotdata(myData);
- }
- }
- xmlhttp.open("GET", url+qstr, true);
- xmlhttp.send();
-</script>
</body>