]> www.average.org Git - pulsecounter.git/commitdiff
make xmlhttprequest for data
authorEugene Crosser <crosser@average.org>
Mon, 21 Dec 2015 21:20:02 +0000 (00:20 +0300)
committerEugene Crosser <crosser@average.org>
Mon, 21 Dec 2015 21:20:02 +0000 (00:20 +0300)
web/index.html [new file with mode: 0644]
web/query.cgi

diff --git a/web/index.html b/web/index.html
new file mode 100644 (file)
index 0000000..ea05899
--- /dev/null
@@ -0,0 +1,59 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<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 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];
+    }
+    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);
+    }
+    context.strokeStyle = color;
+    context.stroke();
+  }
+  function gotdata(data) {
+    canvas = document.getElementById("plot");
+    context = canvas.getContext("2d");
+    drawplot(data.cold, "blue");
+    drawplot(data.hot, "red");
+  }
+</script>
+</head><body>
+<canvas id="plot" width="640" height = "480"
+        style="border:solid 1px #000000;"></canvas>
+<hr/>
+<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>
index 872023716d1cd12ecd43af9e6bcca7e76b2eed8f..690eb9aaac6953abd8403706c9c2a842f163b277 100755 (executable)
@@ -71,7 +71,7 @@ cgiMain = do
           ++ ", \"hi\": " ++ show (ohi :: Int)
           ++ "}, \"current\": {\"cold\": " ++ show (floor (ccold :: Double))
           ++ ", \"hot\": " ++ show (floor (chot :: Double))
-          ++ "}}, \"cold\": [" ++ showjson cold
+          ++ "}, \"cold\": [" ++ showjson cold
           ++ "], \"hot\": [" ++ showjson hot ++ "]}\n"
 
 showjson :: [(Int, Double)] -> String