]> www.average.org Git - pulsecounter.git/blobdiff - web/index.html
style; 'loading'
[pulsecounter.git] / web / index.html
index ea05899a87cb3a4a974dc730ef881d99acd74a17..9e459a20450ddcba8543270404b1fdbe25633d8f 100644 (file)
 <html xml:lang="en" lang="en" xmlns="http://www.w3.org/1999/xhtml">
 <head>
 <script>
-  var canvas;
-  var context;
-  function drawplot(times, color) {
-    var tmin = times[0][0];
-    var tfact = 640/(times[times.length-1][0] - tmin);
+  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 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.lineTo(px(tmax), py(0));
+    ctx.strokeStyle = "black";
+    ctx.stroke();
+  }
+  function yaxis() {
+    ctx.beginPath();
+    ctx.moveTo(px(tmin), py(0));
+    ctx.lineTo(px(tmin), py(hmax));
+    ctx.strokeStyle = "black";
+    ctx.stroke();
+  }
+  /* @ updates global var `hmax` */
+  function differentiate(times) {
+    var res = [];
+    var dv, dt, v;
+
+    for (i = 0; i < times.length - 1; i++) {
+      dv = times[i+1][1] - times[i][1];
+      dt = times[i+1][0] - times[i][0];
+      if (dt != 0 && dv != 0) {
+        v = (dv / dt) * 360 ; /* Litres per hour */
+        if (hmax < v) hmax = v;
+        res.push([times[i][0], v]);
+      }
+    }
+    res.push([times[i][0], v]);
+
+    return res;
+  }
+  function drawplot(data, color) {
     var i;
-    var height = [];
-    var hmax = 0;
-    var hfact;
 
-    for (i = 1; i < times.length; i++) {
-      height[i] = 1 / (times[i][0] - times[i-1][0]);
-      if (hmax < height[i]) hmax = height[i];
+    ctx.beginPath();
+    ctx.moveTo(px(data[0][0]), py(data[0][1]));
+    for (i = 1; i < data.length; i++) {
+      ctx.lineTo(px(data[i][0]), py(data[i - 1][1]));
+      ctx.lineTo(px(data[i][0]), py(data[i][1]));
     }
-    height[i+1] = height[i];
-    hfact = 480 / hmax;
-    context.beginPath();
-    context.moveTo(0, 480 - height[1]*hfact);
-    for (i = 1; i < times.length; i++) {
-      context.lineTo((times[i][0] - tmin)*tfact,480 - height[i]*hfact);
-      context.lineTo((times[i][0] - tmin)*tfact,480 - height[i+1]*hfact);
+    ctx.strokeStyle = color;
+    ctx.stroke();
+  }
+  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) {
+      clearplot();
+      drawplot(cold_d, "blue");
+      drawplot(hot_d, "red");
+      xaxis();
+      yaxis();
+    } else {
+      showloading();
     }
-    context.strokeStyle = color;
-    context.stroke();
   }
+
   function gotdata(data) {
+    document.getElementById("cold").innerHTML =
+      (data.current.cold / 100).toFixed(2);
+    document.getElementById("hot").innerHTML =
+      (data.current.hot / 100).toFixed(2);
+    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);
+    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";
+
+    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();
+  }
+
+  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");
-    context = canvas.getContext("2d");
-    drawplot(data.cold, "blue");
-    drawplot(data.hot, "red");
+    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>
+h1 {
+  text-align: center;
+}
+br {
+  clear: both;
+}
+div#currentvals {
+  width: 18em;
+  margin-left: auto;
+  margin-right: auto;
+  margin-bottom: 10px;
+  text-align: center;
+  font-size: 150%;
+}
+div.current {
+  position: relative;
+  padding: 0.2em;
+  border: solid 1px black;
+  margin: 0.2em;
+}
+div#cold {
+  float: left;
+  background-color: #d0e0ff;
+}
+div#hot {
+  float: right;
+  background-color: #ffd0e0;
+}
+canvas#plot {
+  padding: 0px;
+  margin: auto;
+  display: block;
+  width: 640px;
+  height: 320px;
+  border: solid 1px black;
+}
+body {
+  margin: 0px;
+}
+</style>
+<title>Water Meters</title>
 </head><body>
-<canvas id="plot" width="640" height = "480"
-        style="border:solid 1px #000000;"></canvas>
-<hr/>
+<h1>Water Meters</h1>
+<div id="currentvals">
+  Current Values
+  <div class="current" id="cold">cold</div>
+  <div class="current" id="hot">hot</div>
+</div>
+<br />
+<canvas id="plot" width="640" height = "320"></canvas>
+<br />
 <div id=debug>
 </div>
-<script>
-  var xmlhttp = new XMLHttpRequest();
-  var url = "query.cgi";
-
-  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, true);
-  xmlhttp.send();
-</script>
 </body>