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 be703f9734fbf..8b9d7b4a84623 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 @@ -32,6 +32,10 @@ export const D3_NUMBER_FORMAT_DESCRIPTION_PERCENTAGE_TEXT = t( // input choices & options export const D3_FORMAT_OPTIONS: [string, string][] = [ + [NumberFormats.INDIAN_FORMAT_NUMBER, t('Indian format')], + [NumberFormats.INDIAN_FORMAT_CURRENCY, t('Indian currency format')], + [NumberFormats.INDIAN_FORMAT_NUMBER_SHORT, t('Indian format (in units)')], + [NumberFormats.INDIAN_FORMAT_CURRENCY_SHORT, t('Indian currency format (in units)')], [NumberFormats.SMART_NUMBER, t('Adaptive formatting')], ['~g', t('Original value')], [',d', ',d (12345.432 => 12,345)'], diff --git a/superset-frontend/packages/superset-ui-core/src/number-format/NumberFormats.ts b/superset-frontend/packages/superset-ui-core/src/number-format/NumberFormats.ts index 605da5d30e7b7..737c24cd56125 100644 --- a/superset-frontend/packages/superset-ui-core/src/number-format/NumberFormats.ts +++ b/superset-frontend/packages/superset-ui-core/src/number-format/NumberFormats.ts @@ -53,6 +53,11 @@ const SI = SI_3_DIGIT; const SMART_NUMBER = 'SMART_NUMBER'; const SMART_NUMBER_SIGNED = 'SMART_NUMBER_SIGNED'; const OVER_MAX_HIDDEN = 'OVER_MAX_HIDDEN'; +const INDIAN_FORMAT_NUMBER = 'INDIAN_FORMAT_NUMBER'; +const INDIAN_FORMAT_CURRENCY = 'INDIAN_FORMAT_CURRENCY'; + +const INDIAN_FORMAT_NUMBER_SHORT = 'INDIAN_FORMAT_NUMBER_SHORT'; +const INDIAN_FORMAT_CURRENCY_SHORT = 'INDIAN_FORMAT_CURRENCY_SHORT'; const NumberFormats = { DOLLAR, @@ -84,6 +89,10 @@ const NumberFormats = { SMART_NUMBER, SMART_NUMBER_SIGNED, OVER_MAX_HIDDEN, + INDIAN_FORMAT_NUMBER, + INDIAN_FORMAT_CURRENCY, + INDIAN_FORMAT_NUMBER_SHORT, + INDIAN_FORMAT_CURRENCY_SHORT, }; export default NumberFormats; diff --git a/superset-frontend/packages/superset-ui-core/src/number-format/factories/createIndianNumberFormatter.ts b/superset-frontend/packages/superset-ui-core/src/number-format/factories/createIndianNumberFormatter.ts new file mode 100644 index 0000000000000..c860364c47b51 --- /dev/null +++ b/superset-frontend/packages/superset-ui-core/src/number-format/factories/createIndianNumberFormatter.ts @@ -0,0 +1,88 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +// import { format as d3Format } from 'd3-format'; +import NumberFormatter from '../NumberFormatter'; + +// const float2PointFormatter = d3Format(`.2~f`); +const indianLocaleFormatter = Intl.NumberFormat('en-IN', { + maximumFractionDigits: 2, + minimumFractionDigits: 0 +}).format // d3Format or regex whould be alternative + +function formatIndianUnitNumber( + value: number, + { refoldAfterCrs = false }, +): string { + let formated_number = 0; + let sufix = []; + + do { + if (value >= 10000000) { + formated_number = value / 10000000; + sufix.unshift('Cr'); + } else if (value >= 100000) { + formated_number = value / 100000; + sufix.unshift('L'); + } else if (value >= 1000) { + formated_number = value / 1000; + sufix.unshift('K'); + } else { + formated_number = value; + } + value = formated_number; + } while (refoldAfterCrs && value >= 1000); + + return indianLocaleFormatter(value) + sufix.join(' '); +} + +function formatIndianNumber(value: number): string { + return indianLocaleFormatter(value) +} + +export default function createIndianNumberFormatter( + config: { + prefix?: string; + foldToUnit?: boolean; + refoldAfterCrs?: boolean; + id?: string; + label?: string; + description?: string; + } = { + prefix: '', + foldToUnit: false, + refoldAfterCrs: false, + }, +) { + const { prefix, foldToUnit, refoldAfterCrs, id, label, description } = config; + + return new NumberFormatter({ + description: description || 'Indian Number Formatter', + formatFunc: function (value) { + const sign = value >= 0 ? '' : '-'; + debugger + const formattedNumber = foldToUnit + ? formatIndianUnitNumber(Math.abs(value), { refoldAfterCrs }) + : formatIndianNumber(Math.abs(value)); + return sign + prefix + formattedNumber; + }, + id: id || 'indian_nunber_formatter', + label: label || 'Indian Number Formatter', + }); +} diff --git a/superset-frontend/packages/superset-ui-core/src/number-format/index.ts b/superset-frontend/packages/superset-ui-core/src/number-format/index.ts index 0c501b68515a7..2f38b78e9838d 100644 --- a/superset-frontend/packages/superset-ui-core/src/number-format/index.ts +++ b/superset-frontend/packages/superset-ui-core/src/number-format/index.ts @@ -29,5 +29,6 @@ export { export { default as NumberFormatterRegistry } from './NumberFormatterRegistry'; export { default as createD3NumberFormatter } from './factories/createD3NumberFormatter'; export { default as createDurationFormatter } from './factories/createDurationFormatter'; +export { default as createIndianNumberFormatter } from './factories/createIndianNumberFormatter'; export { default as createSiAtMostNDigitFormatter } from './factories/createSiAtMostNDigitFormatter'; export { default as createSmartNumberFormatter } from './factories/createSmartNumberFormatter'; diff --git a/superset-frontend/src/setup/setupFormatters.ts b/superset-frontend/src/setup/setupFormatters.ts index 8c7d5fd4e8e85..b93b04e71040e 100644 --- a/superset-frontend/src/setup/setupFormatters.ts +++ b/superset-frontend/src/setup/setupFormatters.ts @@ -24,7 +24,7 @@ import { getTimeFormatterRegistry, smartDateFormatter, smartDateVerboseFormatter, - createD3NumberFormatter, + createIndianNumberFormatter, } from '@superset-ui/core'; export default function setupFormatters() { @@ -69,27 +69,35 @@ export default function setupFormatters() { createDurationFormatter({ formatSubMilliseconds: true }), ) .registerValue( - 'CURRENCY_INDIA', - createD3NumberFormatter({ - locale: { - decimal: '.', - thousands: ',', - grouping: [3, 2, 2, 2, 2, 2, 2, 2, 2, 2], - currency: ['₹', ''], - }, - formatString: '$,.0f', + NumberFormats.INDIAN_FORMAT_NUMBER, + createIndianNumberFormatter({ + prefix: '', + foldToUnit: false, + refoldAfterCrs: false, }), ) .registerValue( - 'NUMBER_INDIA', - createD3NumberFormatter({ - locale: { - decimal: '.', - thousands: ',', - grouping: [3, 2, 2, 2, 2, 2, 2, 2, 2, 2], - currency: ['', ''], - }, - formatString: '$,.0f', + NumberFormats.INDIAN_FORMAT_CURRENCY, + createIndianNumberFormatter({ + prefix: '₹', + foldToUnit: false, + refoldAfterCrs: false, + }), + ) + .registerValue( + NumberFormats.INDIAN_FORMAT_NUMBER_SHORT, + createIndianNumberFormatter({ + prefix: '', + foldToUnit: true, + refoldAfterCrs: true, + }), + ) + .registerValue( + NumberFormats.INDIAN_FORMAT_CURRENCY_SHORT, + createIndianNumberFormatter({ + prefix: '₹', + foldToUnit: true, + refoldAfterCrs: false, }), );