Просмотр исходного кода

Implement decibel and percentunit units.

Add tests to exercise new units as well.
Greg Look 10 лет назад
Родитель
Сommit
dc5c3a3939
2 измененных файлов с 25 добавлено и 6 удалено
  1. 17 6
      public/app/components/kbn.js
  2. 8 0
      public/test/specs/kbn-format-specs.js

+ 17 - 6
public/app/components/kbn.js

@@ -245,10 +245,10 @@ function($, _) {
 
 
   // Formatter which always appends a fixed unit string to the value. No
   // Formatter which always appends a fixed unit string to the value. No
   // scaling of the value is performed.
   // scaling of the value is performed.
-  kbn.formatBuilders.fixedUnit = function(unit, separator) {
+  kbn.formatBuilders.fixedUnit = function(unit) {
     return function(size, decimals) {
     return function(size, decimals) {
       if (size === null) { return ""; }
       if (size === null) { return ""; }
-      return kbn.toFixed(size, decimals) + (separator || ' ') + unit;
+      return kbn.toFixed(size, decimals) + ' ' + unit;
     };
     };
   };
   };
 
 
@@ -301,10 +301,20 @@ function($, _) {
   ///// VALUE FORMATS /////
   ///// VALUE FORMATS /////
 
 
   // Dimensionless Units
   // Dimensionless Units
-  kbn.valueFormats.none    = kbn.toFixed;
-  kbn.valueFormats.short   = kbn.formatBuilders.scaledUnits(1000, ['', ' K', ' Mil', ' Bil', ' Tri', ' Quadr', ' Quint', ' Sext', ' Sept']);
-  kbn.valueFormats.ppm     = kbn.formatBuilders.fixedUnit('ppm');
-  kbn.valueFormats.percent = kbn.formatBuilders.fixedUnit('%', '');
+  kbn.valueFormats.none  = kbn.toFixed;
+  kbn.valueFormats.short = kbn.formatBuilders.scaledUnits(1000, ['', ' K', ' Mil', ' Bil', ' Tri', ' Quadr', ' Quint', ' Sext', ' Sept']);
+  kbn.valueFormats.dB    = kbn.formatBuilders.fixedUnit('dB');
+  kbn.valueFormats.ppm   = kbn.formatBuilders.fixedUnit('ppm');
+
+  kbn.valueFormats.percent = function(size, decimals) {
+    if (size === null) { return ""; }
+    return kbn.toFixed(size, decimals) + '%';
+  };
+
+  kbn.valueFormats.percentunit = function(size, decimals) {
+    if (size === null) { return ""; }
+    return kbn.toFixed(100*size, decimals) + '%';
+  };
 
 
   // Data
   // Data
   kbn.valueFormats.bits   = kbn.formatBuilders.binarySIPrefix('b');
   kbn.valueFormats.bits   = kbn.formatBuilders.binarySIPrefix('b');
@@ -442,6 +452,7 @@ function($, _) {
           {text: 'none' ,                     value: 'none'       },
           {text: 'none' ,                     value: 'none'       },
           {text: 'short',                     value: 'short'      },
           {text: 'short',                     value: 'short'      },
           {text: 'scaled percentage (0-100)', value: 'percent'    },
           {text: 'scaled percentage (0-100)', value: 'percent'    },
+          {text: 'unit percentage (0.0-1.0)', value: 'percentunit'},
           {text: 'ppm',                       value: 'ppm'        },
           {text: 'ppm',                       value: 'ppm'        },
           {text: 'decibel',                   value: 'dB'         },
           {text: 'decibel',                   value: 'dB'         },
         ]
         ]

+ 8 - 0
public/test/specs/kbn-format-specs.js

@@ -26,6 +26,14 @@ define([
 
 
   describeValueFormat('none', 2.75e-10, 0, 10, '3e-10');
   describeValueFormat('none', 2.75e-10, 0, 10, '3e-10');
   describeValueFormat('none', 0, 0, 2, '0');
   describeValueFormat('none', 0, 0, 2, '0');
+  describeValueFormat('dB', 10, 1000, 2, '10.00 dB');
+
+  describeValueFormat('percent',  0, 0, 0, '0%');
+  describeValueFormat('percent', 53, 0, 1, '53.0%');
+  describeValueFormat('percentunit', 0.0, 0, 0, '0%');
+  describeValueFormat('percentunit', 0.278, 0, 1, '27.8%');
+  describeValueFormat('percentunit', 1.0, 0, 0, '100%');
+
   describeValueFormat('bytes', -1.57e+308, -1.57e+308, 2, 'NA');
   describeValueFormat('bytes', -1.57e+308, -1.57e+308, 2, 'NA');
 
 
   describeValueFormat('ns', 25, 1, 0, '25 ns');
   describeValueFormat('ns', 25, 1, 0, '25 ns');