From 50ddb8e6fd8eec1a608f05673e43178b910a9bbe Mon Sep 17 00:00:00 2001 From: Evan Rusackas Date: Tue, 29 Nov 2022 09:19:16 -0700 Subject: [PATCH] adding formatters --- .../src/utils/D3Formatting.ts | 43 +++++++++++-------- .../src/setup/setupFormatters.ts | 40 +++++++++++++++++ 2 files changed, 64 insertions(+), 19 deletions(-) diff --git a/superset-frontend/packages/superset-ui-chart-controls/src/utils/D3Formatting.ts b/superset-frontend/packages/superset-ui-chart-controls/src/utils/D3Formatting.ts index 0e30f82d7b44e..c53984f589610 100644 --- a/superset-frontend/packages/superset-ui-chart-controls/src/utils/D3Formatting.ts +++ b/superset-frontend/packages/superset-ui-chart-controls/src/utils/D3Formatting.ts @@ -16,7 +16,7 @@ * 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( @@ -24,24 +24,29 @@ export const D3_FORMAT_DOCS = t( ); // 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', diff --git a/superset-frontend/src/setup/setupFormatters.ts b/superset-frontend/src/setup/setupFormatters.ts index bc5efc1ed35be..6ebbfbed09247 100644 --- a/superset-frontend/src/setup/setupFormatters.ts +++ b/superset-frontend/src/setup/setupFormatters.ts @@ -18,6 +18,7 @@ */ import { createDurationFormatter, + createD3NumberFormatter, getNumberFormatter, getNumberFormatterRegistry, NumberFormats, @@ -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()