Skip to content

Commit

Permalink
Add euro format (#3850)
Browse files Browse the repository at this point in the history
* Add type

* change comment style

* add euro, various cleanups

* remove unused function

* clean ups in formatting

* Finalize euro preset, add types

* cleanup

* fix tests

* add entry in docs

* re-enable and fix test test

* more cleanup

---------

Co-authored-by: Frederik Hagelund <[email protected]>
  • Loading branch information
bcolloran and Fredehagelund92 authored Jan 16, 2024
1 parent 04d8754 commit 6e5c22c
Show file tree
Hide file tree
Showing 20 changed files with 319 additions and 329 deletions.
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

1 comment on commit 6e5c22c

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.