diff --git a/src/ui/public/stringify/__tests__/_color.js b/src/ui/public/stringify/__tests__/_color.js index 05e3021dc902a..d3c9c89beae69 100644 --- a/src/ui/public/stringify/__tests__/_color.js +++ b/src/ui/public/stringify/__tests__/_color.js @@ -1,6 +1,7 @@ import expect from 'expect.js'; import ngMock from 'ng_mock'; import RegistryFieldFormatsProvider from 'ui/registry/field_formats'; + describe('Color Format', function () { let fieldFormats; let ColorFormat; @@ -63,4 +64,42 @@ describe('Color Format', function () { expect(converter('a', 'html')).to.eql('a'); }); }); + + + it('should take into account numerical format', function () { + let colorer = new ColorFormat({ + colors: [{ + range: '100:150', + text: 'blue', + background: 'yellow' + }] + //, + // pattern: '0,0.[000]' + }); + expect(colorer.convert(99.5555, 'html')).to.eql('99.556'); + expect(colorer.convert(100.5555, 'html')).to.eql('100.556'); + expect(colorer.convert(149.5555, 'html')).to.eql('149.556'); + expect(colorer.convert(151.5555, 'html')).to.eql('151.556'); + + }); + + it('should take into account custom numerical format', function () { + let colorer = new ColorFormat({ + colors: [{ + range: '100:150', + text: 'blue', + background: 'yellow' + }], + pattern: '0,0.[00]' + }); + expect(colorer.convert(99.5555, 'html')).to.eql('99.56'); + expect(colorer.convert(100.5555, 'html')).to.eql('100.56'); + expect(colorer.convert(149.5555, 'html')).to.eql('149.56'); + expect(colorer.convert(151.5555, 'html')).to.eql('151.56'); + + }); + + + + }); diff --git a/src/ui/public/stringify/editors/color.html b/src/ui/public/stringify/editors/color.html index 23ab3bd92fb02..35176535eba6f 100644 --- a/src/ui/public/stringify/editors/color.html +++ b/src/ui/public/stringify/editors/color.html @@ -1,4 +1,6 @@
+ +
-
diff --git a/src/ui/public/stringify/types/_numeral.js b/src/ui/public/stringify/types/_numeral.js index 9bbab9715436e..c5025c5507daf 100644 --- a/src/ui/public/stringify/types/_numeral.js +++ b/src/ui/public/stringify/types/_numeral.js @@ -3,6 +3,7 @@ import 'ui/field_format_editor/numeral/numeral'; import IndexPatternsFieldFormatProvider from 'ui/index_patterns/_field_format/field_format'; import BoundToConfigObjProvider from 'ui/bound_to_config_obj'; export default function AbstractNumeralFormatProvider(Private) { + const FieldFormat = Private(IndexPatternsFieldFormatProvider); const BoundToConfigObj = Private(BoundToConfigObjProvider); const numeral = require('numeral')(); @@ -26,6 +27,7 @@ export default function AbstractNumeralFormatProvider(Private) { Numeral.factory = function (opts) { + _.class(Class).inherits(Numeral); function Class(params) { Class.Super.call(this, params); diff --git a/src/ui/public/stringify/types/color.js b/src/ui/public/stringify/types/color.js index 221bc9acaec48..badb4cd272c80 100644 --- a/src/ui/public/stringify/types/color.js +++ b/src/ui/public/stringify/types/color.js @@ -1,11 +1,13 @@ import 'ui/stringify/editors/color.less'; import _ from 'lodash'; -import IndexPatternsFieldFormatProvider from 'ui/index_patterns/_field_format/field_format'; import colorTemplate from 'ui/stringify/editors/color.html'; -export default function ColorFormatProvider(Private) { +import StringifyTypesNumeralProvider from 'ui/stringify/types/_numeral'; +import BoundToConfigObjProvider from 'ui/bound_to_config_obj'; - const FieldFormat = Private(IndexPatternsFieldFormatProvider); - const convertTemplate = _.template('<%- val %>'); +export default function ColorFormatProvider(Private) { + const Numeral = Private(StringifyTypesNumeralProvider); + const BoundToConfigObj = Private(BoundToConfigObjProvider); + const DEFAULT_COLOR = { range: `${Number.NEGATIVE_INFINITY}:${Number.POSITIVE_INFINITY}`, regex: '', @@ -13,7 +15,7 @@ export default function ColorFormatProvider(Private) { background: '#ffffff' }; - _.class(_Color).inherits(FieldFormat); + _.class(_Color).inherits(Numeral); function _Color(params) { _Color.Super.call(this, params); } @@ -42,11 +44,10 @@ export default function ColorFormatProvider(Private) { } }; - - _Color.paramDefaults = { - fieldType: null, // populated by editor, see controller below - colors: [_.cloneDeep(DEFAULT_COLOR)] - }; + _Color.paramDefaults = new BoundToConfigObj({ + colors: [_.cloneDeep(DEFAULT_COLOR)], + pattern: '=format:color:defaultPattern' + }); _Color.prototype.findColorRuleForVal = function (val) { switch (this.param('fieldType')) { @@ -69,13 +70,20 @@ export default function ColorFormatProvider(Private) { _Color.prototype._convert = { html(val) { - const color = this.findColorRuleForVal(val); - if (!color) return _.asPrettyString(val); + const color = _.findLast(this.param('colors'), ({ range }) => { + if (!range) return; + const [start, end] = range.split(':'); + return val >= Number(start) && val <= Number(end); + }); + + const formattedValue = Numeral.prototype._convert.call(this, val); + if (!color) { + return formattedValue; + } - let style = ''; - if (color.text) style += `color: ${color.text};`; - if (color.background) style += `background-color: ${color.background};`; - return convertTemplate({ val, style }); + const styleColor = color.text ? `color: ${color.text};` : ''; + const styleBackgroundColor = color.background ? `background-color: ${color.background};` : ''; + return `${formattedValue}`; } }; diff --git a/src/ui/settings/defaults.js b/src/ui/settings/defaults.js index 1a6845989281a..e831223c652d7 100644 --- a/src/ui/settings/defaults.js +++ b/src/ui/settings/defaults.js @@ -186,6 +186,11 @@ export default function defaultSettingsProvider() { value: '0,0.[000]', description: 'Default numeral format for the "number" format' }, + 'format:color:defaultPattern': { + type: 'string', + value: '0,0.[000]', + description: 'Default numeral format for the "color" format' + }, 'format:bytes:defaultPattern': { type: 'string', value: '0,0.[000]b',