diff --git a/src/core_plugins/timelion/public/__tests__/services/tick_formatters.js b/src/core_plugins/timelion/public/__tests__/services/tick_formatters.js index d8a6c81a4781..46f96690f77c 100644 --- a/src/core_plugins/timelion/public/__tests__/services/tick_formatters.js +++ b/src/core_plugins/timelion/public/__tests__/services/tick_formatters.js @@ -25,6 +25,13 @@ describe('Tick Formatters', function () { expect(bitFormatter(4.1 * 1000 * 1000)).to.equal('4.1mb'); expect(bitFormatter(3 * 1000 * 1000 * 1000)).to.equal('3gb'); }); + + it('formats negative values with b/kb/mb/gb', () => { + expect(bitFormatter(-7)).to.equal('-7b'); + expect(bitFormatter(-4 * 1000)).to.equal('-4kb'); + expect(bitFormatter(-4.1 * 1000 * 1000)).to.equal('-4.1mb'); + expect(bitFormatter(-3 * 1000 * 1000 * 1000)).to.equal('-3gb'); + }); }); describe('Bits/s mode', function () { @@ -43,6 +50,13 @@ describe('Tick Formatters', function () { expect(bitsFormatter(4.1 * 1000 * 1000)).to.equal('4.1mb/s'); expect(bitsFormatter(3 * 1000 * 1000 * 1000)).to.equal('3gb/s'); }); + + it('formats negative values with b/kb/mb/gb', function () { + expect(bitsFormatter(-7)).to.equal('-7b/s'); + expect(bitsFormatter(-4 * 1000)).to.equal('-4kb/s'); + expect(bitsFormatter(-4.1 * 1000 * 1000)).to.equal('-4.1mb/s'); + expect(bitsFormatter(-3 * 1000 * 1000 * 1000)).to.equal('-3gb/s'); + }); }); describe('Bytes mode', function () { @@ -61,6 +75,13 @@ describe('Tick Formatters', function () { expect(byteFormatter(10.2 * 1024 * 1024)).to.equal('10.2MB'); expect(byteFormatter(3 * 1024 * 1024 * 1024)).to.equal('3GB'); }); + + it('formats negative values with B/KB/MB/GB', function () { + expect(byteFormatter(-10)).to.equal('-10B'); + expect(byteFormatter(-10 * 1024)).to.equal('-10KB'); + expect(byteFormatter(-10.2 * 1024 * 1024)).to.equal('-10.2MB'); + expect(byteFormatter(-3 * 1024 * 1024 * 1024)).to.equal('-3GB'); + }); }); describe('Bytes/s mode', function () { @@ -79,6 +100,13 @@ describe('Tick Formatters', function () { expect(bytesFormatter(10.2 * 1024 * 1024)).to.equal('10.2MB/s'); expect(bytesFormatter(3 * 1024 * 1024 * 1024)).to.equal('3GB/s'); }); + + it('formats negative values with B/KB/MB/GB', function () { + expect(bytesFormatter(-10)).to.equal('-10B/s'); + expect(bytesFormatter(-10 * 1024)).to.equal('-10KB/s'); + expect(bytesFormatter(-10.2 * 1024 * 1024)).to.equal('-10.2MB/s'); + expect(bytesFormatter(-3 * 1024 * 1024 * 1024)).to.equal('-3GB/s'); + }); }); describe('Currency mode', function () { diff --git a/src/core_plugins/timelion/public/services/tick_formatters.js b/src/core_plugins/timelion/public/services/tick_formatters.js index 2014fbc63634..cae3569fd925 100644 --- a/src/core_plugins/timelion/public/services/tick_formatters.js +++ b/src/core_plugins/timelion/public/services/tick_formatters.js @@ -18,44 +18,26 @@ function baseTickFormatter(value, axis) { return formatted; } +function unitFormatter(divisor, units) { + return (val) => { + let index = 0; + const isNegative = val < 0; + val = Math.abs(val); + while (val >= divisor && index < units.length) { + val /= divisor; + index++; + } + const value = Math.round(val * 100) / 100 * (isNegative ? -1 : 1); + return `${value}${units[index]}`; + }; +} + export default function tickFormatters() { const formatters = { - 'bits': function (val) { - const labels = ['b', 'kb', 'mb', 'gb', 'tb', 'pb']; - let index = 0; - while (val >= 1000 && index < labels.length) { - val /= 1000; - index++; - } - return (Math.round(val * 100) / 100) + labels[index]; - }, - 'bits/s': function (val) { - const labels = ['b/s', 'kb/s', 'mb/s', 'gb/s', 'tb/s', 'pb/s']; - let index = 0; - while (val >= 1000 && index < labels.length) { - val /= 1000; - index++; - } - return (Math.round(val * 100) / 100) + labels[index]; - }, - 'bytes': function (val) { - const labels = ['B', 'KB', 'MB', 'GB', 'TB', 'PB']; - let index = 0; - while (val >= 1024 && index < labels.length) { - val /= 1024; - index++; - } - return (Math.round(val * 100) / 100) + labels[index]; - }, - 'bytes/s': function (val) { - const labels = ['B/s', 'KB/s', 'MB/s', 'GB/s', 'TB/s', 'PB/s']; - let index = 0; - while (val >= 1024 && index < labels.length) { - val /= 1024; - index++; - } - return (Math.round(val * 100) / 100) + labels[index]; - }, + 'bits': unitFormatter(1000, ['b', 'kb', 'mb', 'gb', 'tb', 'pb']), + 'bits/s': unitFormatter(1000, ['b/s', 'kb/s', 'mb/s', 'gb/s', 'tb/s', 'pb/s']), + 'bytes': unitFormatter(1024, ['B', 'KB', 'MB', 'GB', 'TB', 'PB']), + 'bytes/s': unitFormatter(1024, ['B/s', 'KB/s', 'MB/s', 'GB/s', 'TB/s', 'PB/s']), 'currency': function (val, axis) { return val.toLocaleString('en', { style: 'currency', currency: axis.options.units.prefix || 'USD' }); },