From 9504db83bf413dc8140953e64e7e091ad3b80b3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Tue, 9 Apr 2019 13:04:09 +0200 Subject: [PATCH] Graph: Fixed auto decimals in legend values (#16455) Fixes #16448 (cherry picked from commit 52c39904120fb0b98494b961be67bb47574245b1) --- .../utils/valueFormats/valueFormats.test.ts | 47 +++++++++++-------- .../src/utils/valueFormats/valueFormats.ts | 16 +++---- 2 files changed, 35 insertions(+), 28 deletions(-) diff --git a/packages/grafana-ui/src/utils/valueFormats/valueFormats.test.ts b/packages/grafana-ui/src/utils/valueFormats/valueFormats.test.ts index a7ab2b919b8..4b28c076967 100644 --- a/packages/grafana-ui/src/utils/valueFormats/valueFormats.test.ts +++ b/packages/grafana-ui/src/utils/valueFormats/valueFormats.test.ts @@ -1,29 +1,38 @@ import { toFixed, getValueFormat } from './valueFormats'; -describe('kbn.toFixed and negative decimals', () => { - it('should treat as zero decimals', () => { - const str = toFixed(186.123, -2); - expect(str).toBe('186'); +describe('valueFormats', () => { + describe('toFixed and negative decimals', () => { + it('should treat as zero decimals', () => { + const str = toFixed(186.123, -2); + expect(str).toBe('186'); + }); }); -}); -describe('kbn ms format when scaled decimals is null do not use it', () => { - it('should use specified decimals', () => { - const str = getValueFormat('ms')(10000086.123, 1, null); - expect(str).toBe('2.8 hour'); + describe('ms format when scaled decimals is null do not use it', () => { + it('should use specified decimals', () => { + const str = getValueFormat('ms')(10000086.123, 1, null); + expect(str).toBe('2.8 hour'); + }); }); -}); -describe('kbn kbytes format when scaled decimals is null do not use it', () => { - it('should use specified decimals', () => { - const str = getValueFormat('kbytes')(10000000, 3, null); - expect(str).toBe('9.537 GiB'); + describe('kbytes format when scaled decimals is null do not use it', () => { + it('should use specified decimals', () => { + const str = getValueFormat('kbytes')(10000000, 3, null); + expect(str).toBe('9.537 GiB'); + }); }); -}); -describe('kbn deckbytes format when scaled decimals is null do not use it', () => { - it('should use specified decimals', () => { - const str = getValueFormat('deckbytes')(10000000, 3, null); - expect(str).toBe('10.000 GB'); + describe('deckbytes format when scaled decimals is null do not use it', () => { + it('should use specified decimals', () => { + const str = getValueFormat('deckbytes')(10000000, 3, null); + expect(str).toBe('10.000 GB'); + }); + }); + + describe('ms format when scaled decimals is 0', () => { + it('should use scaledDecimals and add 3', () => { + const str = getValueFormat('ms')(1200, 0, 0); + expect(str).toBe('1.200 s'); + }); }); }); diff --git a/packages/grafana-ui/src/utils/valueFormats/valueFormats.ts b/packages/grafana-ui/src/utils/valueFormats/valueFormats.ts index 25e8f0ce2ac..aeb03d5205c 100644 --- a/packages/grafana-ui/src/utils/valueFormats/valueFormats.ts +++ b/packages/grafana-ui/src/utils/valueFormats/valueFormats.ts @@ -56,17 +56,15 @@ export function toFixed(value: number, decimals?: DecimalCount): string { export function toFixedScaled( value: number, - decimals?: DecimalCount, - scaledDecimals?: DecimalCount, - additionalDecimals?: DecimalCount, + decimals: DecimalCount, + scaledDecimals: DecimalCount, + additionalDecimals: number, ext?: string ) { - if (scaledDecimals) { - if (additionalDecimals) { - return toFixed(value, scaledDecimals + additionalDecimals) + ext; - } else { - return toFixed(value, scaledDecimals) + ext; - } + if (scaledDecimals === null || scaledDecimals === undefined) { + return toFixed(value, decimals) + ext; + } else { + return toFixed(value, scaledDecimals + additionalDecimals) + ext; } return toFixed(value, decimals) + ext;