diff --git a/public/app/components/kbn.js b/public/app/components/kbn.js index e0aa81dc485..ad2d4c3c422 100644 --- a/public/app/components/kbn.js +++ b/public/app/components/kbn.js @@ -298,6 +298,18 @@ function($, _) { return kbn.formatBuilders.scaledUnits(1024, units); }; + // Currency formatter for prefixing a symbol onto a number. Supports scaling + // up to the trillions. + kbn.formatBuilders.currency = function(symbol) { + var units = ['', 'K', 'M', 'B', 'T']; + var scaler = kbn.formatBuilders.scaledUnits(1000, units); + return function(size, decimals, scaledDecimals) { + if (size === null) { return ""; } + var scaled = scaler(size, decimals, scaledDecimals); + return symbol + scaled; + }; + }; + ///// VALUE FORMATS ///// // Dimensionless Units @@ -316,6 +328,10 @@ function($, _) { return kbn.toFixed(100*size, decimals) + '%'; }; + // Currencies + kbn.valueFormats.currencyUSD = kbn.formatBuilders.currency('$'); + kbn.valueFormats.currencyGBP = kbn.formatBuilders.currency('£'); + // Data kbn.valueFormats.bits = kbn.formatBuilders.binarySIPrefix('b'); kbn.valueFormats.bytes = kbn.formatBuilders.binarySIPrefix('B'); @@ -471,6 +487,13 @@ function($, _) { {text: 'decibel', value: 'dB' }, ] }, + { + text: 'currency', + submenu: [ + {text: 'Dollars ($)', value: 'currencyUSD'}, + {text: 'Pounds (£)', value: 'currencyGBP'}, + ] + }, { text: 'time', submenu: [ diff --git a/public/test/specs/kbn-format-specs.js b/public/test/specs/kbn-format-specs.js index feae3c261aa..f84544fff4f 100644 --- a/public/test/specs/kbn-format-specs.js +++ b/public/test/specs/kbn-format-specs.js @@ -53,6 +53,10 @@ define([ describeValueFormat('percentunit', 0.278, 0, 1, '27.8%'); describeValueFormat('percentunit', 1.0, 0, 0, '100%'); + describeValueFormat('currencyUSD', 7.42, 10000, 2, '$7.42'); + describeValueFormat('currencyUSD', 1532.82, 1000, 1, '$1.53K'); + describeValueFormat('currencyUSD', 18520408.7, 10000000, 0, '$19M'); + describeValueFormat('bytes', -1.57e+308, -1.57e+308, 2, 'NA'); describeValueFormat('ns', 25, 1, 0, '25 ns');