mirror of
https://github.com/apache/superset.git
synced 2026-04-16 14:45:21 +00:00
feat: Implement currencies formatter for saved metrics (#24517)
This commit is contained in:
committed by
GitHub
parent
e402c94a9f
commit
83ff4cd86a
@@ -17,7 +17,7 @@
|
||||
* under the License.
|
||||
*/
|
||||
import { isNumber } from 'lodash';
|
||||
import { DataRecord, DTTM_ALIAS, NumberFormatter } from '@superset-ui/core';
|
||||
import { DataRecord, DTTM_ALIAS, ValueFormatter } from '@superset-ui/core';
|
||||
import { OptionName } from 'echarts/types/src/util/types';
|
||||
import { TooltipMarker } from 'echarts/types/src/util/format';
|
||||
import {
|
||||
@@ -91,7 +91,7 @@ export const formatForecastTooltipSeries = ({
|
||||
}: ForecastValue & {
|
||||
seriesName: string;
|
||||
marker: TooltipMarker;
|
||||
formatter: NumberFormatter;
|
||||
formatter: ValueFormatter;
|
||||
}): string => {
|
||||
let row = `${marker}${sanitizeHtml(seriesName)}: `;
|
||||
let isObservation = false;
|
||||
|
||||
@@ -31,6 +31,7 @@ import {
|
||||
SupersetTheme,
|
||||
normalizeTimestamp,
|
||||
LegendState,
|
||||
ValueFormatter,
|
||||
} from '@superset-ui/core';
|
||||
import { SortSeriesType } from '@superset-ui/chart-controls';
|
||||
import { format, LegendComponentOption, SeriesOption } from 'echarts';
|
||||
@@ -345,7 +346,7 @@ export function formatSeriesName(
|
||||
timeFormatter,
|
||||
coltype,
|
||||
}: {
|
||||
numberFormatter?: NumberFormatter;
|
||||
numberFormatter?: ValueFormatter;
|
||||
timeFormatter?: TimeFormatter;
|
||||
coltype?: GenericDataType;
|
||||
} = {},
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
import {
|
||||
Currency,
|
||||
CurrencyFormatter,
|
||||
ensureIsArray,
|
||||
getNumberFormatter,
|
||||
isSavedMetric,
|
||||
QueryFormMetric,
|
||||
ValueFormatter,
|
||||
} from '@superset-ui/core';
|
||||
|
||||
export const buildCustomFormatters = (
|
||||
metrics: QueryFormMetric | QueryFormMetric[] | undefined,
|
||||
currencyFormats: Record<string, Currency>,
|
||||
columnFormats: Record<string, string>,
|
||||
d3Format: string | undefined,
|
||||
) => {
|
||||
const metricsArray = ensureIsArray(metrics);
|
||||
return metricsArray.reduce((acc, metric) => {
|
||||
const actualD3Format = isSavedMetric(metric)
|
||||
? columnFormats[metric] ?? d3Format
|
||||
: d3Format;
|
||||
if (isSavedMetric(metric)) {
|
||||
return currencyFormats[metric]
|
||||
? {
|
||||
...acc,
|
||||
[metric]: new CurrencyFormatter({
|
||||
d3Format: actualD3Format,
|
||||
currency: currencyFormats[metric],
|
||||
}),
|
||||
}
|
||||
: {
|
||||
...acc,
|
||||
[metric]: getNumberFormatter(actualD3Format),
|
||||
};
|
||||
}
|
||||
return acc;
|
||||
}, {});
|
||||
};
|
||||
|
||||
export const getCustomFormatter = (
|
||||
customFormatters: Record<string, ValueFormatter>,
|
||||
metrics: QueryFormMetric | QueryFormMetric[] | undefined,
|
||||
key?: string,
|
||||
) => {
|
||||
const metricsArray = ensureIsArray(metrics);
|
||||
if (metricsArray.length === 1 && isSavedMetric(metricsArray[0])) {
|
||||
return customFormatters[metricsArray[0]];
|
||||
}
|
||||
return key ? customFormatters[key] : undefined;
|
||||
};
|
||||
|
||||
export const getValueFormatter = (
|
||||
metrics: QueryFormMetric | QueryFormMetric[] | undefined,
|
||||
currencyFormats: Record<string, Currency>,
|
||||
columnFormats: Record<string, string>,
|
||||
d3Format: string | undefined,
|
||||
key?: string,
|
||||
) =>
|
||||
getCustomFormatter(
|
||||
buildCustomFormatters(metrics, currencyFormats, columnFormats, d3Format),
|
||||
metrics,
|
||||
key,
|
||||
) ?? getNumberFormatter(d3Format);
|
||||
Reference in New Issue
Block a user