You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by ru...@apache.org on 2026/07/20 23:25:55 UTC

(superset) branch master updated: feat: add distance unit (#24444)

This is an automated email from the ASF dual-hosted git repository.

rusackas pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/superset.git


The following commit(s) were added to refs/heads/master by this push:
     new b4529c2654f feat: add distance unit (#24444)
b4529c2654f is described below

commit b4529c2654f5bd6b935bdd3b2e2ca5ccf8db190c
Author: worker24h <xx...@163.com>
AuthorDate: Tue Jul 21 07:25:48 2026 +0800

    feat: add distance unit (#24444)
    
    Co-authored-by: xuxiaobing <xu...@neolix.cn>
    Co-authored-by: Claude Code <no...@anthropic.com>
    Co-authored-by: Evan <ev...@preset.io>
    Co-authored-by: Evan Rusackas <ev...@rusackas.com>
---
 .../src/utils/D3Formatting.ts                      |  3 +
 .../factories/createLengthFormatter.ts             | 51 ++++++++++++++++
 .../superset-ui-core/src/number-format/index.ts    |  1 +
 .../factories/createLengthFormatter.test.ts        | 68 ++++++++++++++++++++++
 .../test/number-format/index.test.ts               |  2 +
 .../src/setup/setupFormatters.test.ts              |  1 +
 superset-frontend/src/setup/setupFormatters.ts     | 10 ++++
 7 files changed, 136 insertions(+)

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 b276a54f299..bbb17740b38 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
@@ -68,6 +68,9 @@ export const D3_FORMAT_OPTIONS: [string, string][] = [
     'MEMORY_TRANSFER_RATE_BINARY',
     t('Memory transfer rate in bytes - binary (1024B => 1KiB/s)'),
   ],
+  ['LENGTH', t('Length in m (12345m => 12.35km)')],
+  ['LENGTH_CM_KM', t('Length in cm (12345678cm => 123.46km)')],
+  ['LENGTH_CM_M', t('Length in cm (12345cm => 123.45m)')],
 ];
 
 export const D3_TIME_FORMAT_DOCS = t(
diff --git a/superset-frontend/packages/superset-ui-core/src/number-format/factories/createLengthFormatter.ts b/superset-frontend/packages/superset-ui-core/src/number-format/factories/createLengthFormatter.ts
new file mode 100644
index 00000000000..998f0949e7c
--- /dev/null
+++ b/superset-frontend/packages/superset-ui-core/src/number-format/factories/createLengthFormatter.ts
@@ -0,0 +1,51 @@
+/*
+ * 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 type LengthConvertType = 'm => km' | 'cm => km' | 'cm => m';
+
+export default function createLengthFormatter(
+  config: {
+    description?: string;
+    id?: string;
+    label?: string;
+    convertType?: LengthConvertType;
+  } = {},
+) {
+  const { description, id, label, convertType } = config;
+
+  return new NumberFormatter({
+    description,
+    formatFunc: (value: number) => {
+      if (convertType === 'm => km') {
+        return `${(value / 1000).toFixed(2)}km`;
+      }
+      if (convertType === 'cm => km') {
+        return `${(value / 100_000).toFixed(2)}km`;
+      }
+      if (convertType === 'cm => m') {
+        return `${(value / 100).toFixed(2)}m`;
+      }
+      return value.toString();
+    },
+    id: id ?? 'length_format',
+    label: label ?? `Length 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 95ac8cd8789..9cb05e84960 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
@@ -35,3 +35,4 @@ export { default as createMemoryFormatter } from './factories/createMemoryFormat
 export { default as createSiAtMostNDigitFormatter } from './factories/createSiAtMostNDigitFormatter';
 export { default as createSmartNumberFormatter } from './factories/createSmartNumberFormatter';
 export { default as getSmallNumberFormatter } from './getSmallNumberFormatter';
+export { default as createLengthFormatter } from './factories/createLengthFormatter';
diff --git a/superset-frontend/packages/superset-ui-core/test/number-format/factories/createLengthFormatter.test.ts b/superset-frontend/packages/superset-ui-core/test/number-format/factories/createLengthFormatter.test.ts
new file mode 100644
index 00000000000..6b39a9d653d
--- /dev/null
+++ b/superset-frontend/packages/superset-ui-core/test/number-format/factories/createLengthFormatter.test.ts
@@ -0,0 +1,68 @@
+/*
+ * 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, createLengthFormatter } from '@superset-ui/core';
+
+test('creates an instance of NumberFormatter with default id and label', () => {
+  const formatter = createLengthFormatter();
+  expect(formatter).toBeInstanceOf(NumberFormatter);
+  expect(formatter.id).toBe('length_format');
+  expect(formatter.label).toBe('Length formatter');
+});
+
+test('passes the value through unconverted without a convertType', () => {
+  const formatter = createLengthFormatter();
+  expect(formatter(0)).toBe('0');
+  expect(formatter(1234)).toBe('1234');
+  expect(formatter(-56.7)).toBe('-56.7');
+});
+
+test('converts meters to kilometers', () => {
+  const formatter = createLengthFormatter({ convertType: 'm => km' });
+  expect(formatter(1000)).toBe('1.00km');
+  expect(formatter(1500)).toBe('1.50km');
+  expect(formatter(0)).toBe('0.00km');
+  expect(formatter(-2500)).toBe('-2.50km');
+});
+
+test('converts centimeters to kilometers', () => {
+  const formatter = createLengthFormatter({ convertType: 'cm => km' });
+  expect(formatter(100_000)).toBe('1.00km');
+  expect(formatter(250_000)).toBe('2.50km');
+  expect(formatter(1234)).toBe('0.01km');
+});
+
+test('converts centimeters to meters', () => {
+  const formatter = createLengthFormatter({ convertType: 'cm => m' });
+  expect(formatter(100)).toBe('1.00m');
+  expect(formatter(42)).toBe('0.42m');
+  expect(formatter(-300)).toBe('-3.00m');
+});
+
+test('honors custom id, label, and description', () => {
+  const formatter = createLengthFormatter({
+    id: 'my_length',
+    label: 'My length',
+    description: 'Converts lengths',
+    convertType: 'm => km',
+  });
+  expect(formatter.id).toBe('my_length');
+  expect(formatter.label).toBe('My length');
+  expect(formatter.description).toBe('Converts lengths');
+});
diff --git a/superset-frontend/packages/superset-ui-core/test/number-format/index.test.ts b/superset-frontend/packages/superset-ui-core/test/number-format/index.test.ts
index 48175cea0cb..5dd4992bdef 100644
--- a/superset-frontend/packages/superset-ui-core/test/number-format/index.test.ts
+++ b/superset-frontend/packages/superset-ui-core/test/number-format/index.test.ts
@@ -20,6 +20,7 @@
 import {
   createD3NumberFormatter,
   createDurationFormatter,
+  createLengthFormatter,
   createSiAtMostNDigitFormatter,
   createMemoryFormatter,
   formatNumber,
@@ -35,6 +36,7 @@ describe('index', () => {
     [
       createD3NumberFormatter,
       createDurationFormatter,
+      createLengthFormatter,
       createSiAtMostNDigitFormatter,
       createMemoryFormatter,
       formatNumber,
diff --git a/superset-frontend/src/setup/setupFormatters.test.ts b/superset-frontend/src/setup/setupFormatters.test.ts
index 5d20f8466fd..3c77844f183 100644
--- a/superset-frontend/src/setup/setupFormatters.test.ts
+++ b/superset-frontend/src/setup/setupFormatters.test.ts
@@ -40,6 +40,7 @@ jest.mock('@superset-ui/core', () => ({
   createSmartDateFormatter: jest.fn(() => jest.fn()),
   createSmartDateVerboseFormatter: jest.fn(() => jest.fn()),
   createSmartDateDetailedFormatter: jest.fn(() => jest.fn()),
+  createLengthFormatter: jest.fn(() => jest.fn()),
   NumberFormats: { INTEGER: ',d', INTEGER_SIGNED: '+,d' },
   SMART_DATE_ID: 'smart_date',
   SMART_DATE_DETAILED_ID: 'smart_date_detailed',
diff --git a/superset-frontend/src/setup/setupFormatters.ts b/superset-frontend/src/setup/setupFormatters.ts
index 4a68790b265..6a7a89ed4d3 100644
--- a/superset-frontend/src/setup/setupFormatters.ts
+++ b/superset-frontend/src/setup/setupFormatters.ts
@@ -30,6 +30,7 @@ import {
   createSmartDateDetailedFormatter,
   createMemoryFormatter,
   setCurrencyLocale,
+  createLengthFormatter,
 } from '@superset-ui/core';
 import { FormatLocaleDefinition } from 'd3-format';
 import { TimeLocaleDefinition } from 'd3-time-format';
@@ -105,6 +106,15 @@ export default function setupFormatters(
     .registerValue(
       'MEMORY_TRANSFER_RATE_BINARY',
       createMemoryFormatter({ binary: true, transfer: true }),
+    )
+    .registerValue('LENGTH', createLengthFormatter({ convertType: 'm => km' }))
+    .registerValue(
+      'LENGTH_CM_KM',
+      createLengthFormatter({ convertType: 'cm => km' }),
+    )
+    .registerValue(
+      'LENGTH_CM_M',
+      createLengthFormatter({ convertType: 'cm => m' }),
     );
 
   const timeFormatterRegistry = getTimeFormatterRegistry();