From dc5c3a393963ea4c949ebb7162ed78a693e351db Mon Sep 17 00:00:00 2001 From: Greg Look Date: Wed, 14 Oct 2015 13:39:27 -0700 Subject: [PATCH] Implement decibel and percentunit units. Add tests to exercise new units as well. --- public/app/components/kbn.js | 23 +++++++++++++++++------ public/test/specs/kbn-format-specs.js | 8 ++++++++ 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/public/app/components/kbn.js b/public/app/components/kbn.js index 4ac6d5712d9..208d3840fa1 100644 --- a/public/app/components/kbn.js +++ b/public/app/components/kbn.js @@ -245,10 +245,10 @@ function($, _) { // Formatter which always appends a fixed unit string to the value. No // scaling of the value is performed. - kbn.formatBuilders.fixedUnit = function(unit, separator) { + kbn.formatBuilders.fixedUnit = function(unit) { return function(size, decimals) { if (size === null) { return ""; } - return kbn.toFixed(size, decimals) + (separator || ' ') + unit; + return kbn.toFixed(size, decimals) + ' ' + unit; }; }; @@ -301,10 +301,20 @@ function($, _) { ///// VALUE FORMATS ///// // 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 kbn.valueFormats.bits = kbn.formatBuilders.binarySIPrefix('b'); @@ -442,6 +452,7 @@ function($, _) { {text: 'none' , value: 'none' }, {text: 'short', value: 'short' }, {text: 'scaled percentage (0-100)', value: 'percent' }, + {text: 'unit percentage (0.0-1.0)', value: 'percentunit'}, {text: 'ppm', value: 'ppm' }, {text: 'decibel', value: 'dB' }, ] diff --git a/public/test/specs/kbn-format-specs.js b/public/test/specs/kbn-format-specs.js index 366ab575491..d7f97187716 100644 --- a/public/test/specs/kbn-format-specs.js +++ b/public/test/specs/kbn-format-specs.js @@ -26,6 +26,14 @@ define([ describeValueFormat('none', 2.75e-10, 0, 10, '3e-10'); 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('ns', 25, 1, 0, '25 ns');