Dashboards: Fix issue with percentage difference formatting (#93389)

* Dashboards: Fix issue with percentage difference formatting

---------

Co-authored-by: Ihor Yeromin <yeryomin.igor@gmail.com>
This commit is contained in:
kay delaney 2024-09-26 11:15:51 +01:00 committed by GitHub
parent 0a01b60305
commit 6c8f24adc9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 6 additions and 4 deletions

View File

@ -55,13 +55,14 @@ describe('Stats Calculators', () => {
it('should calculate basic stats', () => {
const stats = reduceField({
field: basicTable.fields[0],
reducers: [ReducerID.first, ReducerID.last, ReducerID.mean, ReducerID.count],
reducers: [ReducerID.first, ReducerID.last, ReducerID.mean, ReducerID.count, ReducerID.diffperc],
});
expect(stats.first).toEqual(10);
expect(stats.last).toEqual(20);
expect(stats.mean).toEqual(15);
expect(stats.count).toEqual(2);
expect(stats.diffperc).toEqual(100);
});
it('should handle undefined field data without crashing', () => {

View File

@ -582,7 +582,7 @@ export function doStandardCalcs(field: Field, ignoreNulls: boolean, nullAsZero:
}
if (isNumber(calcs.firstNotNull) && isNumber(calcs.diff)) {
calcs.diffperc = calcs.diff / calcs.firstNotNull;
calcs.diffperc = (calcs.diff / calcs.firstNotNull) * 100;
}
return calcs;
}

View File

@ -41,7 +41,7 @@ describe('BigValue', () => {
/>
);
expect(screen.getByText('50%')).toBeInTheDocument();
expect(screen.getByText('0.5%')).toBeInTheDocument();
});
it('should render without percent change', () => {

View File

@ -27,6 +27,7 @@ export const PercentChange = ({ percentChange, styles }: Props) => {
);
};
// percentChange is expected to be a value between 0-100
export const percentChangeString = (percentChange: number) => {
return percentChange.toLocaleString(undefined, { style: 'percent', maximumSignificantDigits: 3 });
return (percentChange / 100).toLocaleString(undefined, { style: 'percent', maximumSignificantDigits: 3 });
};