Skip to content

Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.

[Enchancement] number formatting improvements (#2106)

Igor Dykhtacf8d33210ab9
diff --git a/src/components/src/common/color-legend.tsx b/src/components/src/common/color-legend.tsx
index 045011749..e63c2e5c7 100644
--- a/src/components/src/common/color-legend.tsx
+++ b/src/components/src/common/color-legend.tsx
@@ -21,10 +21,9 @@
import React, {Component} from 'react';
import styled from 'styled-components';
import {createSelector} from 'reselect';
-import {format} from 'd3-format';
import moment from 'moment';
import {SCALE_TYPES, SCALE_FUNC, ALL_FIELD_TYPES} from '@kepler.gl/constants';
-import {getTimeWidgetHintFormatter} from '@kepler.gl/utils';
+import {getTimeWidgetHintFormatter, formatNumber} from '@kepler.gl/utils';
const ROW_H = 10;
const GAP = 4;
@@ -51,23 +50,13 @@ const getTimeLabelFormat = domain => {
return val => moment.utc(val).format(formatter);
};
-const getNumericLabelFormat = domain => {
- const diff = domain[1] - domain[0];
-
- if (diff < 10) {
- return format('.2f');
- }
-
- return format('.1f');
-};
-
const getQuantLabelFormat = (domain, fieldType) => {
// quant scale can only be assigned to linear Fields: real, timestamp, integer
return fieldType === ALL_FIELD_TYPES.timestamp
? getTimeLabelFormat(domain)
: !fieldType
? defaultFormat
- : getNumericLabelFormat(domain);
+ : n => formatNumber(n, fieldType);
};
const getOrdinalLegends = scale => {
diff --git a/src/utils/src/data-utils.ts b/src/utils/src/data-utils.ts
index 7036d2d9b..86945748c 100644
--- a/src/utils/src/data-utils.ts
+++ b/src/utils/src/data-utils.ts
@@ -372,6 +372,41 @@ export function applyCustomFormat(format, field): FieldFormatter {
}
}
+function formatLargeNumber(n) {
+ // SI-prefix with 4 significant digits
+ return d3Format('.4~s')(n);
+}
+
+export function formatNumber(n: number, type: string | undefined): string {
+ switch (type) {
+ case ALL_FIELD_TYPES.integer:
+ if (n < 0) {
+ return `-${formatNumber(-n, 'integer')}`;
+ }
+ if (n < 1000) {
+ return `${Math.round(n)}`;
+ }
+ if (n < 10 * 1000) {
+ return d3Format(',')(Math.round(n));
+ }
+ return formatLargeNumber(n);
+ case ALL_FIELD_TYPES.real:
+ if (n < 0) {
+ return `-${formatNumber(-n, 'number')}`;
+ }
+ if (n < 1000) {
+ return d3Format('.4~r')(n);
+ }
+ if (n < 10 * 1000) {
+ return d3Format(',')(Math.round(n));
+ }
+ return formatLargeNumber(n);
+
+ default:
+ return formatNumber(n, 'real');
+ }
+}
+
/**
* Format epoch milliseconds with a format string
* @type timezone
diff --git a/src/utils/src/index.ts b/src/utils/src/index.ts
index 0ab01f665..325538578 100644
--- a/src/utils/src/index.ts
+++ b/src/utils/src/index.ts
@@ -62,7 +62,8 @@ export {
applyDefaultFormat,
getBooleanFormatter,
applyCustomFormat,
- datetimeFormatter
+ datetimeFormatter,
+ formatNumber
} from './data-utils';
export {
datasetColorMaker,
diff --git a/test/node/utils/data-utils-test.js b/test/node/utils/data-utils-test.js
index bc10f6756..05f5807c1 100644
--- a/test/node/utils/data-utils-test.js
+++ b/test/node/utils/data-utils-test.js
@@ -29,7 +29,8 @@ import {
snapToMarks,
arrayMove,
getFormatter,
- defaultFormatter
+ defaultFormatter,
+ formatNumber
} from '@kepler.gl/utils';
import {ALL_FIELD_TYPES} from '@kepler.gl/constants';
@@ -190,3 +191,26 @@ test('dataUtils -> getFormatter', t => {
});
t.end();
});
+
+test('dataUtils -> formatNumber', t => {
+ const TEST_CASES = [
+ {input: ['3.14'], output: '3.14', message: 'field type is not given'},
+ {input: ['3.14123'], output: '3.141', message: 'field type is not given'},
+ {input: ['3'], output: '3', message: 'field type is not given'},
+ {input: ['331', 'integer'], output: '331', message: 'format integer'},
+ {input: ['-33.1', 'integer'], output: '-33', message: 'format integer'},
+ {input: ['1234', 'integer'], output: '1,234', message: 'format integer'},
+ {input: ['123456', 'integer'], output: '123.5k', message: 'format integer'},
+ {input: ['123456.2', 'real'], output: '123.5k', message: 'format real'},
+ {input: ['123.23', 'real'], output: '123.2', message: 'format real'},
+ {input: ['12.3', 'real'], output: '12.3', message: 'format real'},
+ {input: ['12.345', 'real'], output: '12.35', message: 'format real'}
+ ];
+
+ TEST_CASES.forEach(tc => {
+ const output = formatNumber(...tc.input);
+ t.equal(output, tc.output, `formatNumber should be correct when ${tc.message}`);
+ });
+
+ t.end();
+});