This commit is contained in:
worker24h 2024-05-05 02:12:47 -03:00 committed by GitHub
commit 745e65e3e1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 61 additions and 0 deletions

View File

@ -57,6 +57,9 @@ export const D3_FORMAT_OPTIONS: [string, string][] = [
...d3Formatted,
['DURATION', t('Duration in ms (66000 => 1m 6s)')],
['DURATION_SUB', t('Duration in ms (1.40008 => 1ms 400µs 80ns)')],
['LENGTH', t('Length in m (12345m => 12.34km)')],
['LENGTH_CM_KM', t('Length in cm (12345678cm => 123.45km)')],
['LENGTH_CM_M', t('Length in cm (12345cm => 123.45m)')],
];
export const D3_TIME_FORMAT_DOCS = t(

View File

@ -0,0 +1,47 @@
/*
* 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 NumberFormatter from '../NumberFormatter';
export default function createLengthFormatter(config: {
description?: string;
id?: string;
label?: string;
convertType?: string;
}) {
const { description, id, label, convertType } = config;
return new NumberFormatter({
description,
formatFunc: value => {
if (convertType === 'm => km') {
return `${(value / 1000).toFixed(2).toString()}KM`;
}
if (convertType === 'cm => km') {
return `${(value / 100_000).toFixed(2).toString()}KM`;
}
if (convertType === 'cm => m') {
return `${(value / 100).toFixed(2).toString()}M`;
}
return value.toString();
},
id: id ?? 'length_format',
label: label ?? `Length formatter`,
});
}

View File

@ -33,3 +33,4 @@ export { default as createD3NumberFormatter } from './factories/createD3NumberFo
export { default as createDurationFormatter } from './factories/createDurationFormatter';
export { default as createSiAtMostNDigitFormatter } from './factories/createSiAtMostNDigitFormatter';
export { default as createSmartNumberFormatter } from './factories/createSmartNumberFormatter';
export { default as createLengthFormatter } from './factories/createLengthFormatter';

View File

@ -24,6 +24,7 @@ import {
getTimeFormatterRegistry,
smartDateFormatter,
smartDateVerboseFormatter,
createLengthFormatter,
} from '@superset-ui/core';
import { FormatLocaleDefinition } from 'd3-format';
@ -70,6 +71,15 @@ export default function setupFormatters(
.registerValue(
'DURATION_SUB',
createDurationFormatter({ formatSubMilliseconds: true }),
)
.registerValue('LENGTH', createLengthFormatter({ convertType: 'm => km' }))
.registerValue(
'LENGTH_CM_KM',
createLengthFormatter({ convertType: 'cm => km' }),
)
.registerValue(
'LENGTH_CM_M',
createLengthFormatter({ convertType: 'cm => m' }),
);
getTimeFormatterRegistry()