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

feat(formatters): [WIP] Adding a few currency formatters #22374

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 @@ -16,32 +16,37 @@
* specific language governing permissions and limitations
* under the License.
*/
import { t, smartDateFormatter, NumberFormats } from '@superset-ui/core';
import { t, smartDateFormatter, getNumberFormatterRegistry } from '@superset-ui/core';

// D3 specific formatting config
export const D3_FORMAT_DOCS = t(
'D3 format syntax: https://github.com/d3/d3-format',
);

// input choices & options
export const D3_FORMAT_OPTIONS: [string, string][] = [
[NumberFormats.SMART_NUMBER, t('Adaptive formatting')],
['~g', t('Original value')],
[',d', ',d (12345.432 => 12,345)'],
['.1s', '.1s (12345.432 => 10k)'],
['.3s', '.3s (12345.432 => 12.3k)'],
[',.1%', ',.1% (12345.432 => 1,234,543.2%)'],
['.2%', '.2% (12345.432 => 1234543.20%)'],
['.3%', '.3% (12345.432 => 1234543.200%)'],
['.4r', '.4r (12345.432 => 12350)'],
[',.1f', ',.1f (12345.432 => 12,345.4)'],
[',.2f', ',.2f (12345.432 => 12,345.43)'],
[',.3f', ',.3f (12345.432 => 12,345.432)'],
['+,', '+, (12345.432 => +12,345.432)'],
['$,.2f', '$,.2f (12345.432 => $12,345.43)'],
['DURATION', t('Duration in ms (66000 => 1m 6s)')],
['DURATION_SUB', t('Duration in ms (1.40008 => 1ms 400µs 80ns)')],
];
// export const D3_FORMAT_OPTIONS: [string, string][] = [
// [NumberFormats.SMART_NUMBER, t('Adaptive formatting')],
// ['~g', t('Original value')],
// [',d', ',d (12345.432 => 12,345)'],
// ['.1s', '.1s (12345.432 => 10k)'],
// ['.3s', '.3s (12345.432 => 12.3k)'],
// [',.1%', ',.1% (12345.432 => 1,234,543.2%)'],
// ['.2%', '.2% (12345.432 => 1234543.20%)'],
// ['.3%', '.3% (12345.432 => 1234543.200%)'],
// ['.4r', '.4r (12345.432 => 12350)'],
// [',.1f', ',.1f (12345.432 => 12,345.4)'],
// [',.2f', ',.2f (12345.432 => 12,345.43)'],
// [',.3f', ',.3f (12345.432 => 12,345.432)'],
// ['+,', '+, (12345.432 => +12,345.432)'],
// ['$,.2f', '$,.2f (12345.432 => $12,345.43)'],
// ['CURRENCY_INDIA', 'Euro ('],
// ['CURRENCY_EURO', 'Euro Currency'],
// ['DURATION', t('Duration in ms (66000 => 1m 6s)')],
// ['DURATION_SUB', t('Duration in ms (1.40008 => 1ms 400µs 80ns)')],
// ];
export const D3_FORMAT_OPTIONS = Object.entries(getNumberFormatterRegistry().items).map(([key, value]) => {
return [key, value.value.description || key];
});

export const D3_TIME_FORMAT_DOCS = t(
'D3 time format syntax: https://github.com/d3/d3-time-format',
Expand Down
40 changes: 40 additions & 0 deletions superset-frontend/src/setup/setupFormatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
import {
createDurationFormatter,
createD3NumberFormatter,
getNumberFormatter,
getNumberFormatterRegistry,
NumberFormats,
Expand Down Expand Up @@ -66,6 +67,45 @@ export default function setupFormatters() {
.registerValue(
'DURATION_SUB',
createDurationFormatter({ formatSubMilliseconds: true }),
)
.registerValue(
'CURRENCY_INDIA',
createD3NumberFormatter({
description: 'Indian Currency',
formatString: '$,.2f',
locale: {
decimal: '.',
thousands: ',',
grouping: [3, 2, 2, 2, 2, 2, 2, 2, 2, 2],
currency: ['₹', ''],
},
}),
)
.registerValue(
'CURRENCY_EURO',
createD3NumberFormatter({
description: 'EU Currency',
formatString: '$.2f',
locale: {
decimal: '.',
thousands: ',',
grouping: [3],
currency: ['€', ''],
},
}),
)
.registerValue(
'CURRENCY_GBP',
createD3NumberFormatter({
description: 'UK Currency',
formatString: '$.2f',
locale: {
decimal: '.',
thousands: ',',
grouping: [3],
currency: ['£', ''],
},
}),
);

getTimeFormatterRegistry()
Expand Down