Skip to content

Commit

Permalink
DRY
Browse files Browse the repository at this point in the history
  • Loading branch information
kgabryje committed Jul 7, 2023
1 parent 40b5ce9 commit 212fff7
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ import {
NumberFormatter,
QueryFormMetric,
getCustomFormatter,
NumberFormats,
isSavedMetric,
CurrencyFormatter,
} from '@superset-ui/core';
import { getOriginalSeries } from '@superset-ui/chart-controls';
import { EChartsCoreOption, SeriesOption } from 'echarts';
Expand Down Expand Up @@ -91,6 +88,7 @@ import {
} from '../Timeseries/transformers';
import { TIMESERIES_CONSTANTS, TIMEGRAIN_TO_TIMESTAMP } from '../constants';
import { getDefaultTooltip } from '../utils/tooltip';
import { getYAxisFormatter } from '../utils/getYAxisFormatter';

const getFormatter = (
customFormatters: Record<string, ValueFormatter>,
Expand All @@ -108,32 +106,6 @@ const getFormatter = (
);
};

const getYAxisFormatter = (
metrics: QueryFormMetric[],
forcePercentFormatter: boolean,
customFormatters: Record<string, ValueFormatter>,
yAxisFormat: string = NumberFormats.SMART_NUMBER,
) => {
if (forcePercentFormatter) {
return getNumberFormatter(',.0%');
}
const metricsArray = ensureIsArray(metrics);
if (
metricsArray.every(isSavedMetric) &&
metricsArray
.map(metric => customFormatters[metric])
.every(
(formatter, _, formatters) =>
formatter instanceof CurrencyFormatter &&
(formatter as CurrencyFormatter)?.currency?.symbol ===
(formatters[0] as CurrencyFormatter)?.currency?.symbol,
)
) {
return customFormatters[metricsArray[0]];
}
return getNumberFormatter(yAxisFormat);
};

export default function transformProps(
chartProps: EchartsMixedTimeseriesProps,
): EchartsMixedTimeseriesChartTransformedProps {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import {
AnnotationLayer,
AxisType,
CategoricalColorNamespace,
CurrencyFormatter,
ensureIsArray,
GenericDataType,
getMetricLabel,
Expand All @@ -33,13 +32,9 @@ import {
isFormulaAnnotationLayer,
isIntervalAnnotationLayer,
isPhysicalColumn,
isSavedMetric,
isTimeseriesAnnotationLayer,
NumberFormats,
QueryFormMetric,
t,
TimeseriesChartDataResponseResult,
ValueFormatter,
buildCustomFormatters,
getCustomFormatter,
} from '@superset-ui/core';
Expand Down Expand Up @@ -99,32 +94,7 @@ import {
TIMEGRAIN_TO_TIMESTAMP,
} from '../constants';
import { getDefaultTooltip } from '../utils/tooltip';

const getYAxisFormatter = (
metrics: QueryFormMetric[],
forcePercentFormatter: boolean,
customFormatters: Record<string, ValueFormatter>,
yAxisFormat: string = NumberFormats.SMART_NUMBER,
) => {
if (forcePercentFormatter) {
return getNumberFormatter(',.0%');
}
const metricsArray = ensureIsArray(metrics);
if (
metricsArray.every(isSavedMetric) &&
metricsArray
.map(metric => customFormatters[metric])
.every(
(formatter, _, formatters) =>
formatter instanceof CurrencyFormatter &&
(formatter as CurrencyFormatter)?.currency?.symbol ===
(formatters[0] as CurrencyFormatter)?.currency?.symbol,
)
) {
return customFormatters[metricsArray[0]];
}
return getNumberFormatter(yAxisFormat);
};
import { getYAxisFormatter } from '../utils/getYAxisFormatter';

export default function transformProps(
chartProps: EchartsTimeseriesChartProps,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* 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 {
CurrencyFormatter,
ensureIsArray,
getNumberFormatter,
isSavedMetric,
NumberFormats,
QueryFormMetric,
ValueFormatter,
} from '@superset-ui/core';

export const getYAxisFormatter = (
metrics: QueryFormMetric[],
forcePercentFormatter: boolean,
customFormatters: Record<string, ValueFormatter>,
yAxisFormat: string = NumberFormats.SMART_NUMBER,
) => {
if (forcePercentFormatter) {
return getNumberFormatter(',.0%');
}
const metricsArray = ensureIsArray(metrics);
if (
metricsArray.every(isSavedMetric) &&
metricsArray
.map(metric => customFormatters[metric])
.every(
(formatter, _, formatters) =>
formatter instanceof CurrencyFormatter &&
(formatter as CurrencyFormatter)?.currency?.symbol ===
(formatters[0] as CurrencyFormatter)?.currency?.symbol,
)
) {
return customFormatters[metricsArray[0]];
}
return getNumberFormatter(yAxisFormat);
};

0 comments on commit 212fff7

Please sign in to comment.