Skip to content
Snippets Groups Projects
Unverified Commit f1943a7e authored by Nemanja Glumac's avatar Nemanja Glumac Committed by GitHub
Browse files

Revert static viz fixes (#26739)

* Revert "Fix incorrect static viz formatting (#26737)"

This reverts commit c155666c.

* Revert "Fix weird static viz data point values rounding (#26704)"

This reverts commit 25329609.
parent c155666c
No related branches found
No related tags found
No related merge requests found
......@@ -24,7 +24,7 @@ interface FormatNumberOptionsType {
negativeInParentheses?: boolean;
number_separators?: string;
number_style?: string;
scale?: number;
scale?: string;
type?: string;
}
......@@ -83,12 +83,7 @@ export function formatNumber(
} else {
try {
let nf;
if (
number < 1 &&
number > -1 &&
options.decimals == null &&
options.number_style !== "percent"
) {
if (number < 1 && number > -1 && options.decimals == null) {
// NOTE: special case to match existing behavior for small numbers, use
// max significant digits instead of max fraction digits
nf = numberFormatterForOptions({
......
import { merge } from "icepick";
import { formatNumber as appFormatNumber } from "metabase/lib/formatting/numbers";
export type NumberFormatOptions = {
number_style?: "currency" | "decimal" | "scientific" | "percentage";
......@@ -25,9 +24,74 @@ const DEFAULT_OPTIONS = {
};
export const formatNumber = (number: number, options?: NumberFormatOptions) => {
const { prefix, suffix } = { ...DEFAULT_OPTIONS, ...options };
const {
number_style,
currency,
currency_style,
number_separators: [decimal_separator, grouping_separator],
decimals,
scale,
prefix,
suffix,
compact,
} = handleSmallNumberFormat(number, { ...DEFAULT_OPTIONS, ...options });
return `${prefix}${appFormatNumber(number, options)}${suffix}`;
function createFormat(compact?: boolean) {
if (compact) {
return new Intl.NumberFormat("en", {
style: number_style !== "scientific" ? number_style : "decimal",
notation: "compact",
compactDisplay: "short",
currency: currency,
currencyDisplay: currency_style,
useGrouping: true,
maximumFractionDigits: decimals != null ? decimals : 2,
});
}
return new Intl.NumberFormat("en", {
style: number_style !== "scientific" ? number_style : "decimal",
notation: number_style !== "scientific" ? "standard" : "scientific",
currency: currency,
currencyDisplay: currency_style,
useGrouping: true,
minimumFractionDigits: decimals,
maximumFractionDigits: decimals != null ? decimals : 2,
});
}
const format = createFormat(compact);
const separatorMap = {
",": grouping_separator || "",
".": decimal_separator,
};
const formattedNumber = format
.format(number * scale)
.replace(/,|\./g, separator => separatorMap[separator as "." | ","]);
return `${prefix}${formattedNumber}${suffix}`;
};
// Simple hack to handle small decimal numbers (0-1)
function handleSmallNumberFormat<T>(value: number, options: T): T {
const hasAtLeastThreeDecimalPoints = Math.abs(value) < 0.01;
if (hasAtLeastThreeDecimalPoints && Math.abs(value) > 0) {
options = maybeMerge(options, {
compact: true,
decimals: 4,
});
}
return options;
}
const maybeMerge = <T, S1>(collection: T, object: S1) => {
if (collection == null) {
return collection;
}
return merge(collection, object);
};
export const formatPercent = (percent: number) =>
......
......@@ -46,7 +46,7 @@ describe("formatNumber", () => {
number_style: "scientific",
});
expect(text).toEqual("1.2e+3");
expect(text).toEqual("1.2E3");
});
it("should format a number with custom number separators", () => {
......
......@@ -103,12 +103,6 @@ describe("formatting", () => {
});
it("should format percentages", () => {
const options = { compact: true, number_style: "percent" };
expect(formatNumber(0.867, { number_style: "percent" })).toEqual(
"86.7%",
);
expect(formatNumber(1.2345, { number_style: "percent" })).toEqual(
"123.45%",
);
expect(formatNumber(0, options)).toEqual("0%");
expect(formatNumber(0.001, options)).toEqual("0.1%");
expect(formatNumber(0.0001, options)).toEqual("0.01%");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment