From c0ab87796673cfd48db3faa95ed06117b12a201c Mon Sep 17 00:00:00 2001 From: Peter Holmberg Date: Thu, 21 Mar 2019 13:45:50 +0100 Subject: [PATCH 1/8] adding function --- .../grafana-ui/src/utils/displayValue.test.ts | 6 +-- packages/grafana-ui/src/utils/displayValue.ts | 44 ++++++++++++++++++- 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/packages/grafana-ui/src/utils/displayValue.test.ts b/packages/grafana-ui/src/utils/displayValue.test.ts index bf0e7a89bba..8e7a4a421a2 100644 --- a/packages/grafana-ui/src/utils/displayValue.test.ts +++ b/packages/grafana-ui/src/utils/displayValue.test.ts @@ -1,5 +1,5 @@ import { getDisplayProcessor, getColorFromThreshold, DisplayProcessor, DisplayValue } from './displayValue'; -import { MappingType, ValueMapping } from '../types/panel'; +import { MappingType, ValueMapping } from '../types'; function assertSame(input: any, processors: DisplayProcessor[], match: DisplayValue) { processors.forEach(processor => { @@ -128,7 +128,7 @@ describe('Format value', () => { const result = instance(value); - expect(result.text).toEqual('6.0'); + expect(result.text).toEqual('6'); }); it('should return formatted value if there are no matching value mappings', () => { @@ -141,7 +141,7 @@ describe('Format value', () => { const result = instance(value); - expect(result.text).toEqual('10.0'); + expect(result.text).toEqual('10'); }); it('should return mapped value if there are matching value mappings', () => { diff --git a/packages/grafana-ui/src/utils/displayValue.ts b/packages/grafana-ui/src/utils/displayValue.ts index 75191f34434..04c78476595 100644 --- a/packages/grafana-ui/src/utils/displayValue.ts +++ b/packages/grafana-ui/src/utils/displayValue.ts @@ -15,7 +15,6 @@ export interface DisplayValue { export interface DisplayValueOptions { unit?: string; decimals?: DecimalCount; - scaledDecimals?: DecimalCount; dateFormat?: string; // If set try to convert numbers to date color?: string; @@ -37,6 +36,7 @@ export type DisplayProcessor = (value: any) => DisplayValue; export function getDisplayProcessor(options?: DisplayValueOptions): DisplayProcessor { if (options && !_.isEmpty(options)) { const formatFunc = getValueFormat(options.unit || 'none'); + return (value: any) => { const { prefix, suffix, mappings, thresholds, theme } = options; let color = options.color; @@ -47,12 +47,15 @@ export function getDisplayProcessor(options?: DisplayValueOptions): DisplayProce let shouldFormat = true; if (mappings && mappings.length > 0) { const mappedValue = getMappedValue(mappings, value); + if (mappedValue) { text = mappedValue.text; const v = toNumber(text); + if (!isNaN(v)) { numeric = v; } + shouldFormat = false; } } @@ -67,7 +70,8 @@ export function getDisplayProcessor(options?: DisplayValueOptions): DisplayProce if (!isNaN(numeric)) { if (shouldFormat && !_.isBoolean(value)) { - text = formatFunc(numeric, options.decimals, options.scaledDecimals, options.isUtc); + const decimalInfo = getDecimalsForValue(value); + text = formatFunc(numeric, decimalInfo.decimals, decimalInfo.scaledDecimals, options.isUtc); } if (thresholds && thresholds.length > 0) { color = getColorFromThreshold(numeric, thresholds, theme); @@ -143,3 +147,39 @@ export function getColorFromThreshold(value: number, thresholds: Threshold[], th // Use the first threshold as the default color return getColorFromHexRgbOrName(thresholds[0].color, themeType); } + +export function getDecimalsForValue(value: number): { decimals: number; scaledDecimals: number } { + const delta = value / 2; + let dec = -Math.floor(Math.log(delta) / Math.LN10); + + const magn = Math.pow(10, -dec); + const norm = delta / magn; // norm is between 1.0 and 10.0 + let size; + + if (norm < 1.5) { + size = 1; + } else if (norm < 3) { + size = 2; + // special case for 2.5, requires an extra decimal + if (norm > 2.25) { + size = 2.5; + ++dec; + } + } else if (norm < 7.5) { + size = 5; + } else { + size = 10; + } + + size *= magn; + + // reduce starting decimals if not needed + if (Math.floor(value) === value) { + dec = 0; + } + + const decimals = Math.max(0, dec); + const scaledDecimals = decimals - Math.floor(Math.log(size) / Math.LN10) + 2; + + return { decimals, scaledDecimals }; +} From 205a5baa56b2e4db2ea2ff071ea5529df71a6937 Mon Sep 17 00:00:00 2001 From: Peter Holmberg Date: Thu, 21 Mar 2019 13:49:12 +0100 Subject: [PATCH 2/8] Sorting imports --- packages/grafana-ui/src/utils/displayValue.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/grafana-ui/src/utils/displayValue.ts b/packages/grafana-ui/src/utils/displayValue.ts index 04c78476595..2d424805d2c 100644 --- a/packages/grafana-ui/src/utils/displayValue.ts +++ b/packages/grafana-ui/src/utils/displayValue.ts @@ -1,10 +1,14 @@ -import { ValueMapping, Threshold } from '../types'; +// Libraries import _ from 'lodash'; +import moment from 'moment'; + +// Utils import { getValueFormat, DecimalCount } from './valueFormats/valueFormats'; import { getMappedValue } from './valueMappings'; -import { GrafanaTheme, GrafanaThemeType } from '../types'; import { getColorFromHexRgbOrName } from './namedColorsPalette'; -import moment from 'moment'; + +// Types +import { GrafanaTheme, GrafanaThemeType, ValueMapping, Threshold } from '../types'; export interface DisplayValue { text: string; // Show in the UI From 528f085c96766960a6035da92405835b0ec24970 Mon Sep 17 00:00:00 2001 From: Peter Holmberg Date: Thu, 21 Mar 2019 14:03:53 +0100 Subject: [PATCH 3/8] updating usages in singlestat --- public/app/plugins/panel/singlestat/module.ts | 46 ++----------------- 1 file changed, 3 insertions(+), 43 deletions(-) diff --git a/public/app/plugins/panel/singlestat/module.ts b/public/app/plugins/panel/singlestat/module.ts index 5b75de44949..b1b85107898 100644 --- a/public/app/plugins/panel/singlestat/module.ts +++ b/public/app/plugins/panel/singlestat/module.ts @@ -3,6 +3,7 @@ import $ from 'jquery'; import 'vendor/flot/jquery.flot'; import 'vendor/flot/jquery.flot.gauge'; import 'app/features/panel/panellinks/link_srv'; +import { getDecimalsForValue } from '@grafana/ui'; import kbn from 'app/core/utils/kbn'; import config from 'app/core/config'; @@ -190,7 +191,7 @@ class SingleStatCtrl extends MetricsPanelCtrl { data.value = 0; data.valueRounded = 0; } else { - const decimalInfo = this.getDecimalsForValue(data.value); + const decimalInfo = getDecimalsForValue(data.value); const formatFunc = getValueFormat(this.panel.format); data.valueFormatted = formatFunc( @@ -243,47 +244,6 @@ class SingleStatCtrl extends MetricsPanelCtrl { this.render(); } - getDecimalsForValue(value) { - if (_.isNumber(this.panel.decimals)) { - return { decimals: this.panel.decimals, scaledDecimals: null }; - } - - const delta = value / 2; - let dec = -Math.floor(Math.log(delta) / Math.LN10); - - const magn = Math.pow(10, -dec); - const norm = delta / magn; // norm is between 1.0 and 10.0 - let size; - - if (norm < 1.5) { - size = 1; - } else if (norm < 3) { - size = 2; - // special case for 2.5, requires an extra decimal - if (norm > 2.25) { - size = 2.5; - ++dec; - } - } else if (norm < 7.5) { - size = 5; - } else { - size = 10; - } - - size *= magn; - - // reduce starting decimals if not needed - if (Math.floor(value) === value) { - dec = 0; - } - - const result: any = {}; - result.decimals = Math.max(0, dec); - result.scaledDecimals = result.decimals - Math.floor(Math.log(size) / Math.LN10) + 2; - - return result; - } - setValues(data) { data.flotpairs = []; @@ -319,7 +279,7 @@ class SingleStatCtrl extends MetricsPanelCtrl { data.value = this.series[0].stats[this.panel.valueName]; data.flotpairs = this.series[0].flotpairs; - const decimalInfo = this.getDecimalsForValue(data.value); + const decimalInfo = getDecimalsForValue(data.value); data.valueFormatted = formatFunc( data.value, From 6e05b9f41c7f7757514f7d5462f03ccfec711abb Mon Sep 17 00:00:00 2001 From: Peter Holmberg Date: Thu, 21 Mar 2019 14:05:45 +0100 Subject: [PATCH 4/8] adding test --- .../grafana-ui/src/utils/displayValue.test.ts | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/packages/grafana-ui/src/utils/displayValue.test.ts b/packages/grafana-ui/src/utils/displayValue.test.ts index 8e7a4a421a2..c52aee6f87b 100644 --- a/packages/grafana-ui/src/utils/displayValue.test.ts +++ b/packages/grafana-ui/src/utils/displayValue.test.ts @@ -1,4 +1,10 @@ -import { getDisplayProcessor, getColorFromThreshold, DisplayProcessor, DisplayValue } from './displayValue'; +import { + getDisplayProcessor, + getColorFromThreshold, + DisplayProcessor, + DisplayValue, + getDecimalsForValue, +} from './displayValue'; import { MappingType, ValueMapping } from '../types'; function assertSame(input: any, processors: DisplayProcessor[], match: DisplayValue) { @@ -155,3 +161,18 @@ describe('Format value', () => { expect(instance(value).text).toEqual('1-20'); }); }); + +describe('getDecimalsForValue()', () => { + it('should calculate reasonable decimals precision for given value', () => { + expect(getDecimalsForValue(1.01)).toEqual({ decimals: 1, scaledDecimals: 4 }); + expect(getDecimalsForValue(9.01)).toEqual({ decimals: 0, scaledDecimals: 2 }); + expect(getDecimalsForValue(1.1)).toEqual({ decimals: 1, scaledDecimals: 4 }); + expect(getDecimalsForValue(2)).toEqual({ decimals: 0, scaledDecimals: 2 }); + expect(getDecimalsForValue(20)).toEqual({ decimals: 0, scaledDecimals: 1 }); + expect(getDecimalsForValue(200)).toEqual({ decimals: 0, scaledDecimals: 0 }); + expect(getDecimalsForValue(2000)).toEqual({ decimals: 0, scaledDecimals: 0 }); + expect(getDecimalsForValue(20000)).toEqual({ decimals: 0, scaledDecimals: -2 }); + expect(getDecimalsForValue(200000)).toEqual({ decimals: 0, scaledDecimals: -3 }); + expect(getDecimalsForValue(200000000)).toEqual({ decimals: 0, scaledDecimals: -6 }); + }); +}); From 08a4c9f6fa99fa7186ea3aa67c78f6f01c2d6ee9 Mon Sep 17 00:00:00 2001 From: Peter Holmberg Date: Fri, 22 Mar 2019 10:42:17 +0100 Subject: [PATCH 5/8] move typings to types, --- .../src/components/BarGauge/BarGauge.tsx | 4 +- .../grafana-ui/src/components/Gauge/Gauge.tsx | 5 +- packages/grafana-ui/src/types/displayValue.ts | 34 +++++++++++++ packages/grafana-ui/src/types/index.ts | 1 + .../grafana-ui/src/utils/displayValue.test.ts | 21 ++++---- packages/grafana-ui/src/utils/displayValue.ts | 50 ++++++++----------- 6 files changed, 70 insertions(+), 45 deletions(-) create mode 100644 packages/grafana-ui/src/types/displayValue.ts diff --git a/packages/grafana-ui/src/components/BarGauge/BarGauge.tsx b/packages/grafana-ui/src/components/BarGauge/BarGauge.tsx index 99d2d061a11..a5a2190b8ca 100644 --- a/packages/grafana-ui/src/components/BarGauge/BarGauge.tsx +++ b/packages/grafana-ui/src/components/BarGauge/BarGauge.tsx @@ -3,10 +3,10 @@ import React, { PureComponent, CSSProperties, ReactNode } from 'react'; import tinycolor from 'tinycolor2'; // Utils -import { getColorFromHexRgbOrName, getThresholdForValue, DisplayValue } from '../../utils'; +import { getColorFromHexRgbOrName, getThresholdForValue } from '../../utils'; // Types -import { Themeable, TimeSeriesValue, Threshold, VizOrientation } from '../../types'; +import { DisplayValue, Themeable, TimeSeriesValue, Threshold, VizOrientation } from '../../types'; const BAR_SIZE_RATIO = 0.8; diff --git a/packages/grafana-ui/src/components/Gauge/Gauge.tsx b/packages/grafana-ui/src/components/Gauge/Gauge.tsx index f812cc971bc..8118fe82f19 100644 --- a/packages/grafana-ui/src/components/Gauge/Gauge.tsx +++ b/packages/grafana-ui/src/components/Gauge/Gauge.tsx @@ -1,10 +1,9 @@ import React, { PureComponent } from 'react'; import $ from 'jquery'; -import { Threshold, GrafanaThemeType } from '../../types'; import { getColorFromHexRgbOrName } from '../../utils'; -import { Themeable } from '../../index'; -import { DisplayValue } from '../../utils/displayValue'; + +import { DisplayValue, Threshold, GrafanaThemeType, Themeable } from '../../types'; export interface Props extends Themeable { height: number; diff --git a/packages/grafana-ui/src/types/displayValue.ts b/packages/grafana-ui/src/types/displayValue.ts new file mode 100644 index 00000000000..3d8f2d23ddb --- /dev/null +++ b/packages/grafana-ui/src/types/displayValue.ts @@ -0,0 +1,34 @@ +import { DecimalCount } from '../utils'; +import { ValueMapping } from './panel'; +import { Threshold } from './threshold'; +import { GrafanaTheme } from './theme'; + +export interface DisplayValue { + text: string; // Show in the UI + numeric: number; // Use isNaN to check if it is a real number + color?: string; // color based on configs or Threshold +} + +export interface DisplayValueOptions { + unit?: string; + decimals?: DecimalCount; + dateFormat?: string; // If set try to convert numbers to date + + color?: string; + mappings?: ValueMapping[]; + thresholds?: Threshold[]; + prefix?: string; + suffix?: string; + + // Alternative to empty string + noValue?: string; + + // Context + isUtc?: boolean; + theme?: GrafanaTheme; // Will pick 'dark' if not defined +} + +export interface DecimalInfo { + decimals: number; + scaledDecimals: number; +} diff --git a/packages/grafana-ui/src/types/index.ts b/packages/grafana-ui/src/types/index.ts index c0aede431d0..1aec63c8690 100644 --- a/packages/grafana-ui/src/types/index.ts +++ b/packages/grafana-ui/src/types/index.ts @@ -6,3 +6,4 @@ export * from './datasource'; export * from './theme'; export * from './threshold'; export * from './input'; +export * from './displayValue'; diff --git a/packages/grafana-ui/src/utils/displayValue.test.ts b/packages/grafana-ui/src/utils/displayValue.test.ts index c52aee6f87b..39f5ea4de3c 100644 --- a/packages/grafana-ui/src/utils/displayValue.test.ts +++ b/packages/grafana-ui/src/utils/displayValue.test.ts @@ -1,11 +1,5 @@ -import { - getDisplayProcessor, - getColorFromThreshold, - DisplayProcessor, - DisplayValue, - getDecimalsForValue, -} from './displayValue'; -import { MappingType, ValueMapping } from '../types'; +import { getDisplayProcessor, getColorFromThreshold, DisplayProcessor, getDecimalsForValue } from './displayValue'; +import { DisplayValue, MappingType, ValueMapping } from '../types'; function assertSame(input: any, processors: DisplayProcessor[], match: DisplayValue) { processors.forEach(processor => { @@ -134,7 +128,7 @@ describe('Format value', () => { const result = instance(value); - expect(result.text).toEqual('6'); + expect(result.text).toEqual('6.0'); }); it('should return formatted value if there are no matching value mappings', () => { @@ -147,7 +141,14 @@ describe('Format value', () => { const result = instance(value); - expect(result.text).toEqual('10'); + expect(result.text).toEqual('10.0'); + }); + + it('should set auto decimals, 1 significant', () => { + const value = '1.23'; + const instance = getDisplayProcessor({ decimals: null }); + + expect(instance(value).text).toEqual('1.2'); }); it('should return mapped value if there are matching value mappings', () => { diff --git a/packages/grafana-ui/src/utils/displayValue.ts b/packages/grafana-ui/src/utils/displayValue.ts index 2d424805d2c..7c83d85d882 100644 --- a/packages/grafana-ui/src/utils/displayValue.ts +++ b/packages/grafana-ui/src/utils/displayValue.ts @@ -3,37 +3,12 @@ import _ from 'lodash'; import moment from 'moment'; // Utils -import { getValueFormat, DecimalCount } from './valueFormats/valueFormats'; +import { getValueFormat } from './valueFormats/valueFormats'; import { getMappedValue } from './valueMappings'; import { getColorFromHexRgbOrName } from './namedColorsPalette'; // Types -import { GrafanaTheme, GrafanaThemeType, ValueMapping, Threshold } from '../types'; - -export interface DisplayValue { - text: string; // Show in the UI - numeric: number; // Use isNaN to check if it is a real number - color?: string; // color based on configs or Threshold -} - -export interface DisplayValueOptions { - unit?: string; - decimals?: DecimalCount; - dateFormat?: string; // If set try to convert numbers to date - - color?: string; - mappings?: ValueMapping[]; - thresholds?: Threshold[]; - prefix?: string; - suffix?: string; - - // Alternative to empty string - noValue?: string; - - // Context - isUtc?: boolean; - theme?: GrafanaTheme; // Will pick 'dark' if not defined -} +import { DecimalInfo, DisplayValue, DisplayValueOptions, GrafanaTheme, GrafanaThemeType, Threshold } from '../types'; export type DisplayProcessor = (value: any) => DisplayValue; @@ -74,8 +49,23 @@ export function getDisplayProcessor(options?: DisplayValueOptions): DisplayProce if (!isNaN(numeric)) { if (shouldFormat && !_.isBoolean(value)) { - const decimalInfo = getDecimalsForValue(value); - text = formatFunc(numeric, decimalInfo.decimals, decimalInfo.scaledDecimals, options.isUtc); + let decimals; + let scaledDecimals = 0; + + if (!options.decimals) { + const decimalInfo = getDecimalsForValue(value); + + decimals = decimalInfo.decimals; + scaledDecimals = decimalInfo.scaledDecimals; + } else { + decimals = options.decimals; + } + + console.log('coin coin', value); + console.log(decimals); + console.log(scaledDecimals); + + text = formatFunc(numeric, decimals, scaledDecimals, options.isUtc); } if (thresholds && thresholds.length > 0) { color = getColorFromThreshold(numeric, thresholds, theme); @@ -152,7 +142,7 @@ export function getColorFromThreshold(value: number, thresholds: Threshold[], th return getColorFromHexRgbOrName(thresholds[0].color, themeType); } -export function getDecimalsForValue(value: number): { decimals: number; scaledDecimals: number } { +export function getDecimalsForValue(value: number): DecimalInfo { const delta = value / 2; let dec = -Math.floor(Math.log(delta) / Math.LN10); From 94d129e4795b6a786c8041173ce0d1f6beeb7ad1 Mon Sep 17 00:00:00 2001 From: Peter Holmberg Date: Fri, 22 Mar 2019 13:07:39 +0100 Subject: [PATCH 6/8] add one more test --- packages/grafana-ui/src/utils/displayValue.test.ts | 7 +++++++ packages/grafana-ui/src/utils/displayValue.ts | 4 ---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/packages/grafana-ui/src/utils/displayValue.test.ts b/packages/grafana-ui/src/utils/displayValue.test.ts index 39f5ea4de3c..1849daa76d7 100644 --- a/packages/grafana-ui/src/utils/displayValue.test.ts +++ b/packages/grafana-ui/src/utils/displayValue.test.ts @@ -151,6 +151,13 @@ describe('Format value', () => { expect(instance(value).text).toEqual('1.2'); }); + it('should set auto decimals, 2 significant', () => { + const value = '0.0245'; + const instance = getDisplayProcessor({ decimals: null }); + + expect(instance(value).text).toEqual('0.02'); + }); + it('should return mapped value if there are matching value mappings', () => { const valueMappings: ValueMapping[] = [ { id: 0, operator: '', text: '1-20', type: MappingType.RangeToText, from: '1', to: '20' }, diff --git a/packages/grafana-ui/src/utils/displayValue.ts b/packages/grafana-ui/src/utils/displayValue.ts index 7c83d85d882..5c6e9a7d8f4 100644 --- a/packages/grafana-ui/src/utils/displayValue.ts +++ b/packages/grafana-ui/src/utils/displayValue.ts @@ -61,10 +61,6 @@ export function getDisplayProcessor(options?: DisplayValueOptions): DisplayProce decimals = options.decimals; } - console.log('coin coin', value); - console.log(decimals); - console.log(scaledDecimals); - text = formatFunc(numeric, decimals, scaledDecimals, options.isUtc); } if (thresholds && thresholds.length > 0) { From 5a5aed10a1968721e040951882550de2e684b49f Mon Sep 17 00:00:00 2001 From: Peter Holmberg Date: Fri, 22 Mar 2019 13:35:40 +0100 Subject: [PATCH 7/8] adding check for decimals --- public/app/plugins/panel/singlestat/module.ts | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/public/app/plugins/panel/singlestat/module.ts b/public/app/plugins/panel/singlestat/module.ts index b1b85107898..bfe9db835c4 100644 --- a/public/app/plugins/panel/singlestat/module.ts +++ b/public/app/plugins/panel/singlestat/module.ts @@ -279,15 +279,17 @@ class SingleStatCtrl extends MetricsPanelCtrl { data.value = this.series[0].stats[this.panel.valueName]; data.flotpairs = this.series[0].flotpairs; - const decimalInfo = getDecimalsForValue(data.value); + let decimals = this.panel.decimals; + let scaledDecimals = 0; - data.valueFormatted = formatFunc( - data.value, - decimalInfo.decimals, - decimalInfo.scaledDecimals, - this.dashboard.isTimezoneUtc() - ); - data.valueRounded = kbn.roundValue(data.value, decimalInfo.decimals); + if (!this.panel.decimals) { + const decimalInfo = getDecimalsForValue(data.value); + decimals = decimalInfo.decimals; + scaledDecimals = decimalInfo.scaledDecimals; + } + + data.valueFormatted = formatFunc(data.value, decimals, scaledDecimals, this.dashboard.isTimezoneUtc()); + data.valueRounded = kbn.roundValue(data.value, decimals); } // Add $__name variable for using in prefix or postfix From dba64a4e88a0629b6ee09563f293d5862d7974e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Fri, 22 Mar 2019 14:33:17 +0100 Subject: [PATCH 8/8] Moved DisplayValueOptions type back, #16134 --- packages/grafana-ui/src/types/displayValue.ts | 25 +------------------ packages/grafana-ui/src/utils/displayValue.ts | 22 +++++++++++++++- 2 files changed, 22 insertions(+), 25 deletions(-) diff --git a/packages/grafana-ui/src/types/displayValue.ts b/packages/grafana-ui/src/types/displayValue.ts index 3d8f2d23ddb..65bf916b3f4 100644 --- a/packages/grafana-ui/src/types/displayValue.ts +++ b/packages/grafana-ui/src/types/displayValue.ts @@ -1,31 +1,8 @@ -import { DecimalCount } from '../utils'; -import { ValueMapping } from './panel'; -import { Threshold } from './threshold'; -import { GrafanaTheme } from './theme'; - export interface DisplayValue { text: string; // Show in the UI numeric: number; // Use isNaN to check if it is a real number color?: string; // color based on configs or Threshold -} - -export interface DisplayValueOptions { - unit?: string; - decimals?: DecimalCount; - dateFormat?: string; // If set try to convert numbers to date - - color?: string; - mappings?: ValueMapping[]; - thresholds?: Threshold[]; - prefix?: string; - suffix?: string; - - // Alternative to empty string - noValue?: string; - - // Context - isUtc?: boolean; - theme?: GrafanaTheme; // Will pick 'dark' if not defined + title?: string; } export interface DecimalInfo { diff --git a/packages/grafana-ui/src/utils/displayValue.ts b/packages/grafana-ui/src/utils/displayValue.ts index 5c6e9a7d8f4..012505be700 100644 --- a/packages/grafana-ui/src/utils/displayValue.ts +++ b/packages/grafana-ui/src/utils/displayValue.ts @@ -8,10 +8,30 @@ import { getMappedValue } from './valueMappings'; import { getColorFromHexRgbOrName } from './namedColorsPalette'; // Types -import { DecimalInfo, DisplayValue, DisplayValueOptions, GrafanaTheme, GrafanaThemeType, Threshold } from '../types'; +import { Threshold, ValueMapping, DecimalInfo, DisplayValue, GrafanaTheme, GrafanaThemeType } from '../types'; +import { DecimalCount } from './valueFormats/valueFormats'; export type DisplayProcessor = (value: any) => DisplayValue; +export interface DisplayValueOptions { + unit?: string; + decimals?: DecimalCount; + dateFormat?: string; // If set try to convert numbers to date + + color?: string; + mappings?: ValueMapping[]; + thresholds?: Threshold[]; + prefix?: string; + suffix?: string; + + // Alternative to empty string + noValue?: string; + + // Context + isUtc?: boolean; + theme?: GrafanaTheme; // Will pick 'dark' if not defined +} + export function getDisplayProcessor(options?: DisplayValueOptions): DisplayProcessor { if (options && !_.isEmpty(options)) { const formatFunc = getValueFormat(options.unit || 'none');