]> www.average.org Git - pulsecounter.git/blobdiff - web/index.html
adjust axis comb
[pulsecounter.git] / web / index.html
index 2041d70b022f2a21e8022f4fb4540d0eb4e8346e..bec6537b40c7ffd188a961a7d90d9a2d4e2a6eab 100644 (file)
   var xzero = 20, yzero = 20;
   var cold_d = [], hot_d = [];
 
+  function showdate(utime) {
+    var dt = new Date(utime*1000);
+    return dt.toLocaleDateString() + " " + dt.toLocaleTimeString();
+  }
+
   function getcomb(lo, hi) {
     var comb = [], lb = [];
     var d = hi - lo;
     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; }
+    if (scl < 2) { inc = 0.1; inc2 = 0.2; }
+    else if (scl < 5) { inc = 0.1; inc2 = 0.2; }
+    else { inc = 0.5; inc2 = 1; }
     inc *= ord;
     inc2 *= ord;
     first = (Math.floor(lo / inc) + 1) * inc;
-    for (x = 0; x < (d / inc) - 1.5; x++)
+    for (x = 0; x < (d / inc) - 1.2; x++)
       comb.push(first + inc * x);
     first = (Math.floor(lo / inc2) + 1) * inc2;
-    for (x = 0; x < (d / inc2) - 1.5; x++)
+    for (x = 0; x < (d / inc2) - 1.2; x++)
       lb.push(first + inc2 * x);
     //dbg.innerHTML = "ord=" + ord + "<br>inc=" + inc + "<br>"
     //              + comb + "<br>" + lb;
     ctx.lineTo(px(tmax), py(0));
     ctx.strokeStyle = "black";
     ctx.stroke();
+
+    ctx.fillStyle = "black";
+    ctx.font = "bold 8px Courier";
+    ctx.textAlign = "left";
+    ctx.fillText(showdate(tmin), px(tmin), py(0) + 16);
+    ctx.textAlign = "right";
+    ctx.fillText(showdate(tmax), px(tmax), py(0) + 16);
   }
 
   function yaxis() {
 
     ctx.fillStyle = "black";
     ctx.font = "bold 8px Courier";
-    ctx.fillText(0, px(tmin) - 8, py(0));
+    ctx.textAlign = "right";
+    ctx.fillText(0, px(tmin) - 2, py(0));
     for (i = 0; comb[1][i]; i++) {
-      ctx.fillText(comb[1][i], px(tmin) - xzero + 2, py(comb[1][i]));
+      ctx.fillText(comb[1][i].toFixed(1), px(tmin) - 2, py(comb[1][i]));
     }
+    ctx.textAlign = "left";
     ctx.fillText("l/h", px(tmin) + 2, py(hmax) + 8);
   }
 
         res.push([times[i][0], v]);
       }
     }
-    res.push([times[i][0], v]);
+    if (i) res.push([times[i][0], v]);
 
     return res;
   }
   function showloading() {
     ctx.fillStyle = "green";
     ctx.font = "bold 16px Courier";
-    ctx.fillText("loading", (ww / 2) - 40 , (wh / 2) + 8);
+    ctx.textAlign="center";
+    ctx.fillText("...loading...", (ww / 2) , (wh / 2) + 8);
+  }
+
+  function showempty() {
+    ctx.fillStyle = "red";
+    ctx.font = "bold 24px Courier";
+    ctx.textAlign="center";
+    ctx.fillText("No data for the requested time interval",
+                 (ww / 2) , (wh / 2) + 8);
   }
 
   function clearplot() {
   }
 
   function redraw() {
-    if (cold_d.length && hot_d.length) {
+    clearplot();
+    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();
+      showempty();
     }
   }
 
       (data.current.hot / 100).toFixed(2);
     tmin = data.range.lo;
     tmax = data.range.hi;
+    //dbg.innerHTML = "from " + tmin + " to " + tmax
+    //              + "<br>from " + showdate(tmin) + " to " + showdate(tmax);
     /* differetiate() updates hmax */
     hmax = 0;
     cold_d = differentiate(data.cold);
   }
 
   function sendquery(lo, hi) {
-    var url = "query.cgi" + "?lo=" + iso2qu(lo) + "&" + iso2qu(hi);
+    var url = "query.cgi" + "?lo=" + iso2qu(lo) + "&hi=" + iso2qu(hi);
 
     //dbg.innerHTML = url;
     xmlhttp.onreadystatechange = function() {
       }
     }
     xmlhttp.open("GET", url, true);
+    clearplot();
+    showloading();
     xmlhttp.send();
   }
 
     redraw();
   }
 
+  function daystart(date) {
+    date.setMilliseconds(0);
+    date.setSeconds(0);
+    date.setMinutes(0);
+    date.setHours(0);
+    return date;
+  }
+
+  function prevweek() {
+    var tdy = daystart(new Date());
+    var dow = tdy.getDay();
+    var wstart, wend;
+
+    wstart = new Date(1*tdy - 86400000 * (dow + 7));
+    wend = new Date(1*wstart + 86400000 * 7);
+    xmlhttp = new XMLHttpRequest();
+    sendquery(wstart.toISOString(), wend.toISOString());
+  }
+
+  function thisweek() {
+    var tdy = daystart(new Date());
+    var dow = tdy.getDay();
+    var wstart, wend;
+
+    wstart = new Date(1*tdy - 86400000 * dow);
+    wend = new Date(1*wstart + 86400000 * 7);
+    xmlhttp = new XMLHttpRequest();
+    sendquery(wstart.toISOString(), wend.toISOString());
+  }
+
+  function beforeyesterday() {
+    var tdy = daystart(new Date());
+    var ytd = new Date(1*tdy - 86400000);
+    var byd = new Date(1*ytd - 86400000);
+    xmlhttp = new XMLHttpRequest();
+    sendquery(byd.toISOString(), ytd.toISOString());
+  }
+
+  function yesterday() {
+    var tdy = daystart(new Date());
+    var ytd = new Date(1*tdy - 86400000);
+    xmlhttp = new XMLHttpRequest();
+    sendquery(ytd.toISOString(), tdy.toISOString());
+  }
+
+  function today() {
+    var tdy = daystart(new Date());
+    var tmr = new Date(1*tdy + 86400000);
+    xmlhttp = new XMLHttpRequest();
+    sendquery(tdy.toISOString(), tmr.toISOString());
+  }
+
   function initialize() {
-    var tdy = new Date();
-    var tmr;
-
-    tdy.setMilliseconds(0);
-    tdy.setSeconds(0);
-    tdy.setMinutes(0);
-    tdy.setHours(0);
-    tmr = new Date(1*tdy + 86400000);
     dbg = document.getElementById("debug");
     canvas = document.getElementById("plot");
     ctx = canvas.getContext("2d");
     resize();
+    today();
     
-    xmlhttp = new XMLHttpRequest();
-    sendquery(tdy.toISOString(), tmr.toISOString());
+    document.getElementById("today").onclick = today;
+    document.getElementById("yesterday").onclick = yesterday;
+    document.getElementById("beforeyesterday").onclick = beforeyesterday;
+    document.getElementById("thisweek").onclick = thisweek;
+    document.getElementById("prevweek").onclick = prevweek;
   }
 
   /* Set up */
   }
 </script>
 <style>
+@font-face {
+  font-family: PipeDream;
+  src: url('PIPED.TTF') format('truetype');
+  /* Free to use font from http://www.mlink.net/~paterson/jpfonts.htm */
+}
 h1 {
+  margin-top: 5px;
   text-align: center;
+  font-family: PipeDream;
+  font-size: 64px;
+  font-weight: normal;
+  background-color: lightgray;
 }
 br {
   clear: both;
@@ -243,7 +328,26 @@ canvas#plot {
   display: block;
   width: 640px;
   height: 320px;
+  /*border: solid 1px black;*/
+}
+div#queries {
+  margin-left: auto;
+  margin-right: auto;
+  margin-bottom: 10px;
+  text-align: center;
+}
+div.query {
+  display: inline-block;
+  width: 8em;
+  height: 8em;
   border: solid 1px black;
+  background-color: lightgray;
+  cursor: pointer;
+  vertical-align: middle;
+}
+span.query {
+  display: inline-block;
+  vertical-align: middle;   /* does not work for some reason */
 }
 body {
   margin: 0px;
@@ -251,7 +355,7 @@ body {
 </style>
 <title>Water Meters</title>
 </head><body>
-<h1>Water Meters</h1>
+<h1>WATER METERS</h1>
 <div id="currentvals">
   Current Values
   <div class="current" id="cold">cold</div>
@@ -260,6 +364,14 @@ body {
 <br />
 <canvas id="plot" width="640" height = "320"></canvas>
 <br />
-<div id=debug>
+<div id="queries">
+ <div class="query" id="prevweek"><span class="query">PREVIOUS WEEK</span></div>
+ <div class="query" id="beforeyesterday"><span class="query">DAY
+   BEFORE YESTERDAY</span></div>
+ <div class="query" id="yesterday"><span class="query">YESTERDAY</span></div>
+ <div class="query" id="today"><span class="query">TODAY</span></div>
+ <div class="query" id="thisweek"><span class="query">THIS WEEK</span></div>
 </div>
+<br />
+<div id="debug"></div>
 </body>