-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ui): enable formatting line graph y ticks with binary prefix
- Loading branch information
Showing
13 changed files
with
120 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
import {Base, Scale} from 'src/types' | ||
import {Scale} from 'src/types' | ||
|
||
export const AXES_SCALE_OPTIONS = { | ||
LINEAR: Scale.Linear, | ||
LOG: Scale.Log, | ||
BASE_2: Base.Two, | ||
BASE_10: Base.Ten, | ||
BASE_2: '2', | ||
BASE_10: '10', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import {formatNumber} from 'src/shared/utils/formatNumber' | ||
|
||
describe('formatNumber', () => { | ||
test('can format numbers with no unit prefixes', () => { | ||
expect(formatNumber(123456789.123456789)).toEqual('123456789.1235') | ||
}) | ||
|
||
test('can format numbers with SI unit prefixes', () => { | ||
expect(formatNumber(123456, '10')).toEqual('123.5k') | ||
expect(formatNumber(123456789.123456789, '10')).toEqual('123.5M') | ||
expect(formatNumber(12345678912345.123456789, '10')).toEqual('12.35T') | ||
}) | ||
|
||
test('can format numbers with binary unit prefixes', () => { | ||
expect(formatNumber(2 ** 10, '2')).toEqual('1K') | ||
expect(formatNumber(2 ** 20, '2')).toEqual('1M') | ||
expect(formatNumber(2 ** 30, '2')).toEqual('1G') | ||
}) | ||
|
||
test('can format negative numbers with a binary unit prefix', () => { | ||
expect(formatNumber(0 - 2 ** 30, '2')).toEqual('-1G') | ||
}) | ||
|
||
test('formats small numbers without unit prefixes', () => { | ||
expect(formatNumber(0.551249, '2')).toEqual('0.5512') | ||
expect(formatNumber(0.551249, '10')).toEqual('0.5512') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Libraries | ||
import {format} from 'd3-format' | ||
|
||
// Types | ||
import {Base} from 'src/types' | ||
|
||
const MAX_DECIMALS = 4 // We should eventually make this configurable | ||
|
||
const formatRaw = format(`.${MAX_DECIMALS}~f`) // e.g. "0.032" | ||
|
||
const formatSIPrefix = format(`.${MAX_DECIMALS}~s`) // e.g. "2.452M" | ||
|
||
const BINARY_PREFIXES = ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'] | ||
|
||
const formatBinaryPrefix = (t: number): string => { | ||
const i = Math.floor(Math.log(Math.abs(t)) / Math.log(2 ** 10)) | ||
|
||
return `${formatRaw(t / 1024 ** i)}${BINARY_PREFIXES[i]}` | ||
} | ||
|
||
export const formatNumber = (t: number, base: Base = ''): string => { | ||
const isSmallNumber = t >= -1 && t <= 1 | ||
|
||
if (base === '2' && !isSmallNumber) { | ||
return formatBinaryPrefix(t) | ||
} | ||
|
||
if (base === '10' && !isSmallNumber) { | ||
return formatSIPrefix(t) | ||
} | ||
|
||
return formatRaw(t) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.