Skip to content

Commit

Permalink
Use asPercent for y-ticks
Browse files Browse the repository at this point in the history
  • Loading branch information
dgieselaar committed Jun 18, 2020
1 parent 4c9baa8 commit 09a2af4
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

import React, { useMemo } from 'react';
import numeral from '@elastic/numeral';
import { throttle } from 'lodash';
import { NOT_AVAILABLE_LABEL } from '../../../../../common/i18n';
import { Coordinate, TimeSeries } from '../../../../../typings/timeseries';
Expand All @@ -21,7 +20,7 @@ interface Props {
}

const tickFormatY = (y: Maybe<number>) => {
return numeral(y || 0).format('0 %');
return asPercent(y ?? 0, 1);
};

const formatTooltipValue = (coordinate: Coordinate) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ import { asPercent } from '../formatters';
describe('formatters', () => {
describe('asPercent', () => {
it('should divide and format item as percent', () => {
expect(asPercent(3725, 10000, 'n/a')).toEqual('37.3%');
expect(asPercent(3725, 10000, 'n/a')).toEqual('37%');
});

it('should add a decimal when value is below 10', () => {
expect(asPercent(0.092, 1)).toEqual('9.2%');
});

it('should format when numerator is 0', () => {
expect(asPercent(0, 1, 'n/a')).toEqual('0.0%');
expect(asPercent(0, 1, 'n/a')).toEqual('0%');
});

it('should return fallback when denominator is undefined', () => {
Expand Down
5 changes: 5 additions & 0 deletions x-pack/plugins/apm/public/utils/formatters/formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,10 @@ export function asPercent(
}

const decimal = numerator / denominator;

if (Math.abs(decimal) >= 0.1 || decimal === 0) {
return numeral(decimal).format('0%');
}

return numeral(decimal).format('0.0%');
}

0 comments on commit 09a2af4

Please sign in to comment.