]> www.average.org Git - pulsecounter.git/blobdiff - web/index.html
refactor initialization
[pulsecounter.git] / web / index.html
index 06ee992b34c2c18a8c64547fc5dd03de38eacbb0..18b60e6762fbbfa577800f19b4c8b60cb3c46cf3 100644 (file)
 <html xml:lang="en" lang="en" xmlns="http://www.w3.org/1999/xhtml">
 <head>
 <script>
-  var canvas;
-  var context;
-  var ww;
-  var wh;
-  var hfact;
-  var tmin;
-  var tmax;
-  var tfact;
+  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 (x - tmin) * tfact;
+    return xzero + ((x - tmin) * tfact);
   }
   function py(y) {
-    return (wh - y * hfact);
+    return wh - yzero - (y * hfact);
   }
-  function drawplot(times, color) {
-    var i;
-    var height = [];
-    var hmax = 0;
+  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;
 
-    tfact = ww / (tmax - tmin);
-    for (i = 1; i < times.length; i++) {
-      height[i] = (times[i][1] - times[i-1][1]) / (times[i][0] - times[i-1][0]);
-      if (hmax < height[i]) hmax = height[i];
+    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;
+        if (hmax < v) hmax = v;
+        res.push([times[i][0], v]);
+      }
     }
-    height[i+1] = height[i];
-    hfact = wh / hmax;
+    res.push([times[i][0], v]);
 
-    context.beginPath();
-    context.moveTo(px(times[0][1]), py(height[1]));
-    for (i = 1; i < times.length; i++) {
-      context.lineTo(px(times[i][0]), py(height[i]));
-      context.lineTo(px(times[i][0]), py(height[i+1]));
-    }
-    context.lineTo(px(tmax), py(height[i+1]));
+    return res;
+  }
+  function drawplot(data, color) {
+    var i;
 
-    context.strokeStyle = color;
-    context.stroke();
+    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]));
+    }
+    ctx.strokeStyle = color;
+    ctx.stroke();
   }
+
   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");
-    context = canvas.getContext("2d");
+
+    ctx = canvas.getContext("2d");
     ww = canvas.width;
     wh = canvas.height;
+
     tmin = data.range.lo;
     tmax = data.range.hi;
-    document.getElementById("cold").innerHTML = data.current.cold / 100;
-    document.getElementById("hot").innerHTML = data.current.hot / 100;
-    drawplot(data.cold, "blue");
-    drawplot(data.hot, "red");
+    tfact = (ww - xzero) / (tmax - tmin);
+    /* differetiate() updates hmax */
+    hmax = 0;
+    cold_d = differentiate(data.cold);
+    hot_d = differentiate(data.hot);
+    hfact = (wh - yzero) / hmax;
+
+    //document.getElementById("debug").innerHTML = cold_d + "<br>" + hot_d;
+
+    drawplot(cold_d, "blue");
+    drawplot(hot_d, "red");
+    xaxis();
+    yaxis();
+  }
+
+  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 initialize() {
+    dbg = document.getElementById("debug");
+    xmlhttp = new XMLHttpRequest();
+    sendquery();
+  }
+
+  /* Set up */
+  if(window.attachEvent) {
+    window.attachEvent('onload', initialize);
+  } else {
+    window.onload = initialize;
   }
 </script>
 <style>
@@ -91,7 +157,7 @@ canvas#plot {
   margin-right: auto;
   display: block;
   width: 640px;
-  height: 480px;
+  height: 320px;
   border: solid 1px black;
 }
 </style>
@@ -104,23 +170,8 @@ canvas#plot {
   <div class="current" id="hot">hot</div>
 </div>
 <br />
-<canvas id="plot" width="640" height = "480"></canvas>
+<canvas id="plot" width="640" height = "320"></canvas>
 <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>