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

Add euro format #3850

Merged
merged 13 commits into from
Jan 16, 2024
3 changes: 2 additions & 1 deletion docs/docs/reference/project-files/dashboards.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ _**`measures`**_ — numeric [aggregates](../../develop/metrics-dashboard#measur
- _**`format_preset`**_ — controls the formatting of this measure in the dashboard according to option specified below. Measures cannot have both `format_preset` and `format_d3` entries. _(optional; if neither `format_preset` nor `format_d3` is supplied, measures will be formatted with the `humanize` preset)_
- _`humanize`_ — round off numbers in an opinionated way to thousands (K), millions (M), billions (B), etc
- _`none`_ — raw output
- _`currency_usd`_ — output rounded to 2 decimal points prepended with a dollar sign
- _`currency_usd`_ — output rounded to 2 decimal points prepended with a dollar sign: `$`
- _`currency_eur`_ — output rounded to 2 decimal points prepended with a euro symbol: `€`
- _`percentage`_ — output transformed from a rate to a percentage appended with a percentage sign
- _`interval_ms`_ — time intervals given in milliseconds are transformed into human readable time units like hours (h), days (d), years (y), etc

Expand Down
1 change: 0 additions & 1 deletion web-common/src/components/data-graphic/guides/Axis.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ This component will draw an axis on the specified side.
// this formatter often does the right thing, but may not in some
// circumstances. See https://github.com/rilldata/rill/issues/3631
const formatter = new SingleDigitTimesPowerOfTenFormatter(ticks, {
strategy: "singleDigitTimesPowerOfTen",
numberKind,
padWithInsignificantZeros: false,
});
Expand Down
67 changes: 50 additions & 17 deletions web-common/src/lib/number-formatting/format-measure-value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,82 @@ import { format as d3format } from "d3-format";
import {
FormatPreset,
formatPresetToNumberKind,
NumberKind,
type FormatterFactoryOptions,
} from "./humanizer-types";
import {
formatMsInterval,
formatMsToDuckDbIntervalString,
} from "./strategies/intervals";
import { humanizedFormatterFactory } from "./humanizer";

export function defaultHumanizer(value: number): string {
return humanizeDataType(value, FormatPreset.HUMANIZE);
}
import { PerRangeFormatter } from "./strategies/per-range";
import {
defaultCurrencyOptions,
defaultGenericNumOptions,
defaultPercentOptions,
} from "./strategies/per-range-default-options";
import { NonFormatter } from "./strategies/none";

/**
* This function is intended to provides a compact,
* potentially lossy, humanized string representation of a number.
*/
function humanizeDataType(value: number, type: FormatPreset): string {
function humanizeDataType(value: number, preset: FormatPreset): string {
if (typeof value !== "number") {
console.warn(
`humanizeDataType only accepts numbers, got ${value} for FormatPreset "${type}"`,
`humanizeDataType only accepts numbers, got ${value} for FormatPreset "${preset}"`,
);

return JSON.stringify(value);
}
const numberKind = formatPresetToNumberKind(type);

let innerOptions: FormatterFactoryOptions;
const numberKind = formatPresetToNumberKind(preset);

if (type === FormatPreset.NONE) {
innerOptions = {
strategy: "none",
let options: FormatterFactoryOptions;

if (preset === FormatPreset.NONE) {
options = {
numberKind,
padWithInsignificantZeros: false,
};
} else if (type === FormatPreset.INTERVAL) {
return formatMsInterval(value);
} else {
innerOptions = {
strategy: "default",
options = {
numberKind,
};
}
return humanizedFormatterFactory([value], innerOptions).stringFormat(value);

switch (preset) {
case FormatPreset.NONE:
return new NonFormatter(options).stringFormat(value);

case FormatPreset.CURRENCY_USD:
return new PerRangeFormatter(
defaultCurrencyOptions(NumberKind.DOLLAR),
).stringFormat(value);

case FormatPreset.CURRENCY_EUR:
return new PerRangeFormatter(
defaultCurrencyOptions(NumberKind.EURO),
).stringFormat(value);

case FormatPreset.PERCENTAGE:
return new PerRangeFormatter(defaultPercentOptions).stringFormat(value);

case FormatPreset.INTERVAL:
return formatMsInterval(value);

case FormatPreset.HUMANIZE:
return new PerRangeFormatter(defaultGenericNumOptions).stringFormat(
value,
);

default:
console.warn(
"Unknown format preset, using default formatter. All number kinds should be handled.",
);
return new PerRangeFormatter(defaultGenericNumOptions).stringFormat(
value,
);
}
}

/**
Expand Down
Loading