stuff
[matches/MCTX3420.git] / testing / MCTXWeb / public_html / static / mctx.graph.js
index e79b6a8..fd475ad 100644 (file)
@@ -11,6 +11,8 @@ mctx.graph.api.sensors = mctx.api + "sensors";
 mctx.graph.api.actuators = mctx.api + "actuators";
 mctx.sensors = {};
 mctx.actuators = {};
+
+mctx.graph.devices = {};
 mctx.graph.dependent = null;
 mctx.graph.independent = null;
 mctx.graph.timer = null;
@@ -23,6 +25,15 @@ mctx.graph.chart = null;
  * Appends each value pair to the result
  * @returns result
  */
+/**
+ * Helper - Calculate pairs of (dependent, independent) values
+ * Given input as (time, value) pairs for dependent and independent
+ * Appends each value pair to the result
+ * @param {array[][]} dependent Dependent data to be correlated with independent
+ * @param {array[][]} independent Independent data
+ * @param {array[][]} result Storage location
+ * @returns {dataMerge.result}
+ */
 function dataMerge(dependent, independent, result) {
        var j = 0;
        for (var i = 0; i < dependent.length-1; ++i) {
@@ -56,12 +67,13 @@ $.fn.deployDevices = function(input_type, check_first, group) {
   var apply = function(dict, prefix) {
     $.each(dict, function(key, val) {
       var attributes = {
-          'type' : input_type, 'value' : key, 'alt' : val,
+          'type' : input_type, 'value' : key, 'alt' : val.name,
           'class' : prefix, 'name' : group, 
-          'id' : prefix + '_' + val //Unique id (name mangling)
+          'id' : prefix + '_' + val.name //Unique id (name mangling)
       };
+      
       var entry = $("<input/>", attributes);
-      var label = $("<label/>", {'for' : prefix + '_' + val, 'text' : val}); 
+      var label = $("<label/>", {'for' : prefix + '_' + val.name, 'text' : val.name}); 
       entry.prop("checked", check_first);
       check_first = false;
       container.append(entry).append(label);
@@ -97,19 +109,19 @@ $.fn.setDevices = function() {
   });
 };
 
-function setGraphStatus(on, failText) {
+function setGraphStatus(on, failText, keep) {
   if (on) {
     mctx.graph.running = true;
     $("#status-text").html("&nbsp;");
-    $("#graph-run").text("Pause");
+    $("#graph-run").prop("value", "Pause");
   } else {
     mctx.graph.running = false;
     if (failText) {
       $("#status-text").text(failText).addClass("fail");
-    } else {
+    } else if (!keep) {
       $("#status-text").text("Graph stopped").removeClass("fail");
     }
-    $("#graph-run").text("Run");
+    $("#graph-run").prop("value", "Run");
   }
 }
 
@@ -146,6 +158,11 @@ function graphUpdater() {
         responses.push($.ajax({url : urls[val.urltype], data : parameters})
         .done(function(json) {
           //alert("Hi from " + json.name);
+          if (!$("#status-text").checkStatus(json)) {
+            setGraphStatus(false, null, true); //Don't reset text, checkstatus just set it.
+            return;
+          }
+          
           var dev = val.data;
           for (var i = 0; i < json.data.length; ++i) {
             if (dev.length <= 0 || json.data[i][0] > dev[dev.length-1][0]) {
@@ -160,29 +177,38 @@ function graphUpdater() {
 
     //... When the response is received, then() will happen (I think?)
     $.when.apply(this, responses).then(function () {
-      var plot_data = [];
-      yaxis.each(function() {
-        //alert("Add " + $(this).val() + " to plot");
-        if (xaxis.attr("alt") === "time") {
-          //alert("Against time");
-          plot_data.push(devices[$(this).attr("alt")].data);
+      if (mctx.graph.running) {
+        var plot_data = [];
+        
+        yaxis.each(function() {
+          var series = {};
+          series.label = $(this).attr("alt");
+          
+          //alert("Add " + $(this).val() + " to plot");
+          if (xaxis.attr("alt") === "time") {
+            //alert("Against time");
+            series.data = devices[$(this).attr("alt")].data;
+          } else {
+            var result = []
+            dataMerge(devices[xaxis.attr("alt")].data, 
+                      devices[$(this).attr("alt")].data, result);
+            series.data = result;
+          }
+          plot_data.push(series);
+        });
+        
+        if (mctx.graph.chart !== null) {
+          mctx.graph.chart.setData(plot_data);
+          mctx.graph.chart.setupGrid(); 
+          mctx.graph.chart.draw();
         } else {
-          var result = []
-          dataMerge(devices[xaxis.attr("alt")].data, 
-                    devices[$(this).attr("alt")].data, result);
-          plot_data.push(result);
+          var options = {
+            legend : {
+              container : "#graph-legend"
+            }
+          };
+          mctx.graph.chart = $.plot("#graph", plot_data, options);
         }
-      });
-      
-      //$.plot("#graph", plot_data);
-      if (mctx.graph.chart !== null) {
-        mctx.graph.chart.setData(plot_data);
-        mctx.graph.chart.setupGrid(); 
-        mctx.graph.chart.draw();
-      } else {
-        mctx.graph.chart = $.plot("#graph", plot_data);
-      }
-      if (mctx.graph.running) {
         mctx.graph.timer = setTimeout(updater, 1000);
       }
     }, function () {

UCC git Repository :: git.ucc.asn.au