Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LANDGRIF-1509 Fix small number formatting #1112

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useCallback, useMemo } from 'react';

import { useAppDispatch } from 'store/hooks';
import { setLayer } from 'store/features/analysis/map';
import { NUMBER_FORMAT } from 'utils/number-format';
import {NUMBER_FORMAT, SMALL_NUMBER_FORMAT} from 'utils/number-format';
import LegendItem from 'components/legend/item';
import LegendTypeBasic from 'components/legend/types/basic';
import LegendTypeCategorical from 'components/legend/types/categorical';
Expand Down Expand Up @@ -38,7 +38,9 @@ const ContextualLegendItem = ({ layer }: ContextualLegendItemProps) => {
...item,
label:
item.label ||
`${!Number.isNaN(item.value) ? NUMBER_FORMAT(item.value as number) : item.value}`,
`${Number.isNaN(item.value) ? item.value :
item.value > 1 ? NUMBER_FORMAT(item.value as number) : SMALL_NUMBER_FORMAT(item.value as number)
}`,
})),
};
switch (layer.metadata.legend.type) {
Expand Down
6 changes: 5 additions & 1 deletion client/src/utils/number-format.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { format } from 'd3-format';

export const NUMBER_FORMAT = format(',.3~s');
// for numbers bigger than 1
export const NUMBER_FORMAT = format('.3~s');

// for numbers smaller than 1
export const SMALL_NUMBER_FORMAT = format('.3~g');

export const PRECISE_NUMBER_FORMAT = format(',.3~r');

Expand Down
Loading