Fix negative unit formatting in timelion (#17624) (#17660)

* Fix negative unit formatting in timelion

* Create better readable generator
This commit is contained in:
Tim Roes 2018-04-11 19:47:38 +02:00 committed by GitHub
parent 0ffb38560a
commit f141ef304e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 36 deletions

View file

@ -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 () {

View file

@ -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' });
},