share/collection.js: Improve number formatting.
[collection4.git] / share / collection.js
index 3ee5ccc..1a62f6e 100644 (file)
@@ -28,36 +28,48 @@ var c4 =
 
 function value_to_string (value) /* {{{ */
 {
-  var abs_value = Math.abs (value);
+  var abs_value;
+  var v2s = function (value)
+  {
+    var tmp = Math.round (100.0 * value) / 100.0;
+    return ("" + tmp);
+  }
+
+  if (value == null)
+    return ('NaN');
+  else if (value == 0)
+    return ('0');
+
+  abs_value = Math.abs (value);
 
   if ((abs_value < 10000) && (abs_value >= 0.1))
-    return ("" + value);
+    return (v2s (value));
   else if (abs_value > 1)
   {
     if (abs_value < 10000000)
-      return ("" + (value / 1000) + "k");
+      return (v2s (value / 1000) + "k");
     else if (abs_value < 10000000000)
-      return ("" + (value / 1000000) + "M");
+      return (v2s (value / 1000000) + "M");
     else if (abs_value < 10000000000000)
-      return ("" + (value / 1000000000) + "G");
+      return (v2s (value / 1000000000) + "G");
     else
-      return ("" + (value / 1000000000000) + "T");
+      return (v2s (value / 1000000000000) + "T");
   }
   else
   {
     if (abs_value >= 0.001)
-      return ("" + (value * 1000) + "m");
+      return (v2s (value * 1000) + "m");
     else if (abs_value >= 0.000001)
-      return ("" + (value * 1000000) + "u");
+      return (v2s (value * 1000000) + "u");
     else
-      return ("" + (value * 1000000000) + "n");
+      return (v2s (value * 1000000000) + "n");
   }
 } /* }}} function value_to_string */
 
-function instance_get_params (graph) /* {{{ */
+function instance_get_params (inst) /* {{{ */
 {
-  var graph_selector = graph.graph_selector;
-  var inst_selector = graph.instance_selector;
+  var graph_selector = inst.graph_selector;
+  var inst_selector = inst.instance_selector;
   var selector = {};
 
   if (graph_selector.host == inst_selector.host)
@@ -126,11 +138,11 @@ function ident_clone (ident) /* {{{ */
   return (ret);
 } /* }}} ident_clone */
 
-function graph_get_defs (graph) /* {{{ */
+function inst_get_defs (inst) /* {{{ */
 {
-  if (!graph.def)
+  if (!inst.def)
   {
-    var params = ident_clone (graph.graph_selector);
+    var params = instance_get_params (inst);
     params.action = "graph_def_json";
 
     $.ajax({
@@ -143,14 +155,14 @@ function graph_get_defs (graph) /* {{{ */
         if (!data)
           return;
 
-        graph.def = data;
+        inst.def = data;
       }});
   }
 
-  if (graph.def)
-    return (graph.def);
+  if (inst.def)
+    return (inst.def);
   return;
-} /* }}} graph_get_defs */
+} /* }}} inst_get_defs */
 
 function ident_matches (selector, ident) /* {{{ */
 {
@@ -218,7 +230,6 @@ function def_draw_one (def, data, chart_opts) /* {{{ */
   var chart_series = new Object ();
 
   chart_series.type = 'line';
-  chart_series.name = def.legend || def.data_source;
   chart_series.pointInterval = data.interval * 1000;
   chart_series.pointStart = data.first_value_time * 1000;
   chart_series.data = data.data;
@@ -226,6 +237,11 @@ function def_draw_one (def, data, chart_opts) /* {{{ */
   chart_series.shadow = false;
   chart_series.marker = { enabled: false };
 
+  if (def.legend)
+    chart_series.name = def.legend;
+  else if (def.ds_name)
+    chart_series.name = def.ds_name;
+
   if (def.area)
     chart_series.type = 'area';
 
@@ -307,7 +323,7 @@ function json_graph_update (index) /* {{{ */
   if (!inst)
     return;
 
-  def = graph_get_defs (inst);
+  def = inst_get_defs (inst);
   if (!def)
     return;