From 1f0ca4cdfa8dcb33f90aeb126c62fb0f583979e0 Mon Sep 17 00:00:00 2001 From: "Michael S. Molina" Date: Tue, 13 Feb 2024 16:13:26 -0500 Subject: [PATCH 1/2] fix: Timeseries Y-axis format with contribution mode --- .../src/number-format/NumberFormats.ts | 6 +-- .../src/Heatmap.js | 2 +- .../src/MixedTimeseries/transformProps.ts | 2 + .../src/Timeseries/transformProps.ts | 5 ++- .../src/utils/formatters.ts | 11 +++++- .../test/utils/formatters.test.ts | 37 +++++++++++++++++++ 6 files changed, 57 insertions(+), 6 deletions(-) create mode 100644 superset-frontend/plugins/plugin-chart-echarts/test/utils/formatters.test.ts 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..3825430ca0b3d 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 @@ -35,20 +35,20 @@ const FLOAT_SIGNED = FLOAT_SIGNED_2_POINT; const INTEGER = ',d'; const INTEGER_SIGNED = '+,d'; +const PERCENT = ',.0%'; const PERCENT_1_POINT = ',.1%'; const PERCENT_2_POINT = ',.2%'; const PERCENT_3_POINT = ',.3%'; -const PERCENT = PERCENT_2_POINT; +const PERCENT_SIGNED = '+,.0%'; const PERCENT_SIGNED_1_POINT = '+,.1%'; const PERCENT_SIGNED_2_POINT = '+,.2%'; const PERCENT_SIGNED_3_POINT = '+,.3%'; -const PERCENT_SIGNED = PERCENT_SIGNED_2_POINT; +const SI = '.0s'; const SI_1_DIGIT = '.1s'; const SI_2_DIGIT = '.2s'; const SI_3_DIGIT = '.3s'; -const SI = SI_3_DIGIT; const SMART_NUMBER = 'SMART_NUMBER'; const SMART_NUMBER_SIGNED = 'SMART_NUMBER_SIGNED'; diff --git a/superset-frontend/plugins/legacy-plugin-chart-heatmap/src/Heatmap.js b/superset-frontend/plugins/legacy-plugin-chart-heatmap/src/Heatmap.js index a6967301c6a6b..18493f0602ef7 100644 --- a/superset-frontend/plugins/legacy-plugin-chart-heatmap/src/Heatmap.js +++ b/superset-frontend/plugins/legacy-plugin-chart-heatmap/src/Heatmap.js @@ -250,7 +250,7 @@ function Heatmap(element, props) { hideYLabel(); } - const fp = getNumberFormatter(NumberFormats.PERCENT); + const fp = getNumberFormatter(NumberFormats.PERCENT_2_POINT); const xScale = ordScale('x', null, sortXAxis); const yScale = ordScale('y', null, sortYAxis); diff --git a/superset-frontend/plugins/plugin-chart-echarts/src/MixedTimeseries/transformProps.ts b/superset-frontend/plugins/plugin-chart-echarts/src/MixedTimeseries/transformProps.ts index cbe821d93626c..eee277f08e015 100644 --- a/superset-frontend/plugins/plugin-chart-echarts/src/MixedTimeseries/transformProps.ts +++ b/superset-frontend/plugins/plugin-chart-echarts/src/MixedTimeseries/transformProps.ts @@ -532,6 +532,7 @@ export default function transformProps( !!contributionMode, customFormatters, formatter, + yAxisFormat, ), }, scale: truncateYAxis, @@ -554,6 +555,7 @@ export default function transformProps( !!contributionMode, customFormattersSecondary, formatterSecondary, + yAxisFormatSecondary, ), }, scale: truncateYAxis, diff --git a/superset-frontend/plugins/plugin-chart-echarts/src/Timeseries/transformProps.ts b/superset-frontend/plugins/plugin-chart-echarts/src/Timeseries/transformProps.ts index b9e04b2e85ce2..68c1bf1dfc558 100644 --- a/superset-frontend/plugins/plugin-chart-echarts/src/Timeseries/transformProps.ts +++ b/superset-frontend/plugins/plugin-chart-echarts/src/Timeseries/transformProps.ts @@ -38,6 +38,7 @@ import { isTimeseriesAnnotationLayer, t, TimeseriesChartDataResponseResult, + NumberFormats, } from '@superset-ui/core'; import { extractExtraMetrics, @@ -95,6 +96,7 @@ import { } from '../constants'; import { getDefaultTooltip } from '../utils/tooltip'; import { + getPercentFormatter, getTooltipTimeFormatter, getXAxisFormatter, getYAxisFormatter, @@ -253,7 +255,7 @@ export default function transformProps( const series: SeriesOption[] = []; const forcePercentFormatter = Boolean(contributionMode || isAreaExpand); - const percentFormatter = getNumberFormatter(',.0%'); + const percentFormatter = getPercentFormatter(yAxisFormat); const defaultFormatter = currencyFormat?.symbol ? new CurrencyFormatter({ d3Format: yAxisFormat, currency: currencyFormat }) : getNumberFormatter(yAxisFormat); @@ -486,6 +488,7 @@ export default function transformProps( forcePercentFormatter, customFormatters, defaultFormatter, + yAxisFormat, ), }, scale: truncateYAxis, diff --git a/superset-frontend/plugins/plugin-chart-echarts/src/utils/formatters.ts b/superset-frontend/plugins/plugin-chart-echarts/src/utils/formatters.ts index 5416fa1577635..a8f9d2aa31d5a 100644 --- a/superset-frontend/plugins/plugin-chart-echarts/src/utils/formatters.ts +++ b/superset-frontend/plugins/plugin-chart-echarts/src/utils/formatters.ts @@ -23,6 +23,7 @@ import { getNumberFormatter, getTimeFormatter, isSavedMetric, + NumberFormats, QueryFormMetric, smartDateDetailedFormatter, smartDateFormatter, @@ -30,14 +31,22 @@ import { ValueFormatter, } from '@superset-ui/core'; +export const getPercentFormatter = (format?: string) => + getNumberFormatter( + !format || format === NumberFormats.SMART_NUMBER + ? NumberFormats.PERCENT + : format, + ); + export const getYAxisFormatter = ( metrics: QueryFormMetric[], forcePercentFormatter: boolean, customFormatters: Record, defaultFormatter: ValueFormatter, + format?: string, ) => { if (forcePercentFormatter) { - return getNumberFormatter(',.0%'); + return getPercentFormatter(format); } const metricsArray = ensureIsArray(metrics); if ( diff --git a/superset-frontend/plugins/plugin-chart-echarts/test/utils/formatters.test.ts b/superset-frontend/plugins/plugin-chart-echarts/test/utils/formatters.test.ts new file mode 100644 index 0000000000000..f8d40a5bd136c --- /dev/null +++ b/superset-frontend/plugins/plugin-chart-echarts/test/utils/formatters.test.ts @@ -0,0 +1,37 @@ +/** + * 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 { NumberFormats } from '@superset-ui/core'; +import { getPercentFormatter } from '../../src/utils/formatters'; + +describe('getPercentFormatter', () => { + const value = 0.6; + it('should format as percent if no format is specified', () => { + expect(getPercentFormatter().format(value)).toEqual('60%'); + }); + it('should format as percent if SMART_NUMBER is specified', () => { + expect( + getPercentFormatter(NumberFormats.SMART_NUMBER).format(value), + ).toEqual('60%'); + }); + it('should format using a provided format', () => { + expect( + getPercentFormatter(NumberFormats.PERCENT_2_POINT).format(value), + ).toEqual('60.00%'); + }); +}); From d9ce1b2b18229b8692b5ed97da4053c550eacfcf Mon Sep 17 00:00:00 2001 From: "Michael S. Molina" Date: Tue, 13 Feb 2024 16:24:51 -0500 Subject: [PATCH 2/2] Removes unused import --- .../plugin-chart-echarts/src/Timeseries/transformProps.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/superset-frontend/plugins/plugin-chart-echarts/src/Timeseries/transformProps.ts b/superset-frontend/plugins/plugin-chart-echarts/src/Timeseries/transformProps.ts index 68c1bf1dfc558..7bb2ef5e17934 100644 --- a/superset-frontend/plugins/plugin-chart-echarts/src/Timeseries/transformProps.ts +++ b/superset-frontend/plugins/plugin-chart-echarts/src/Timeseries/transformProps.ts @@ -38,7 +38,6 @@ import { isTimeseriesAnnotationLayer, t, TimeseriesChartDataResponseResult, - NumberFormats, } from '@superset-ui/core'; import { extractExtraMetrics,