Skip to content

Commit 09a2af4

Browse files
committed
Use asPercent for y-ticks
1 parent 4c9baa8 commit 09a2af4

3 files changed

Lines changed: 12 additions & 4 deletions

File tree

x-pack/plugins/apm/public/components/shared/TransactionBreakdown/TransactionBreakdownGraph/index.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*/
66

77
import React, { useMemo } from 'react';
8-
import numeral from '@elastic/numeral';
98
import { throttle } from 'lodash';
109
import { NOT_AVAILABLE_LABEL } from '../../../../../common/i18n';
1110
import { Coordinate, TimeSeries } from '../../../../../typings/timeseries';
@@ -21,7 +20,7 @@ interface Props {
2120
}
2221

2322
const tickFormatY = (y: Maybe<number>) => {
24-
return numeral(y || 0).format('0 %');
23+
return asPercent(y ?? 0, 1);
2524
};
2625

2726
const formatTooltipValue = (coordinate: Coordinate) => {

x-pack/plugins/apm/public/utils/formatters/__test__/formatters.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@ import { asPercent } from '../formatters';
88
describe('formatters', () => {
99
describe('asPercent', () => {
1010
it('should divide and format item as percent', () => {
11-
expect(asPercent(3725, 10000, 'n/a')).toEqual('37.3%');
11+
expect(asPercent(3725, 10000, 'n/a')).toEqual('37%');
12+
});
13+
14+
it('should add a decimal when value is below 10', () => {
15+
expect(asPercent(0.092, 1)).toEqual('9.2%');
1216
});
1317

1418
it('should format when numerator is 0', () => {
15-
expect(asPercent(0, 1, 'n/a')).toEqual('0.0%');
19+
expect(asPercent(0, 1, 'n/a')).toEqual('0%');
1620
});
1721

1822
it('should return fallback when denominator is undefined', () => {

x-pack/plugins/apm/public/utils/formatters/formatters.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,10 @@ export function asPercent(
3434
}
3535

3636
const decimal = numerator / denominator;
37+
38+
if (Math.abs(decimal) >= 0.1 || decimal === 0) {
39+
return numeral(decimal).format('0%');
40+
}
41+
3742
return numeral(decimal).format('0.0%');
3843
}

0 commit comments

Comments
 (0)