Skip to content

Commit a928f8c

Browse files
fardin-developerFardin Mustaque
andauthored
feat: add metric name for big number chart types apache#33013 (apache#33099)
Co-authored-by: Fardin Mustaque <[email protected]>
1 parent afaaf64 commit a928f8c

File tree

15 files changed

+382
-124
lines changed

15 files changed

+382
-124
lines changed

superset-frontend/plugins/plugin-chart-echarts/src/BigNumber/BigNumberPeriodOverPeriod/PopKPI.tsx

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,25 @@ import {
3636
} from './types';
3737
import { useOverflowDetection } from './useOverflowDetection';
3838

39+
const MetricNameText = styled.div<{ metricNameFontSize?: number }>`
40+
${({ theme, metricNameFontSize }) => `
41+
font-family: ${theme.typography.families.sansSerif};
42+
font-weight: ${theme.typography.weights.normal};
43+
font-size: ${metricNameFontSize || theme.typography.sizes.s * 2}px;
44+
text-align: center;
45+
margin-bottom: ${theme.gridUnit * 3}px;
46+
`}
47+
`;
48+
3949
const NumbersContainer = styled.div`
4050
display: flex;
4151
justify-content: center;
4252
align-items: center;
4353
flex-direction: column;
4454
width: 100%;
55+
height: 100%;
4556
overflow: auto;
57+
padding: 12px;
4658
`;
4759

4860
const ComparisonValue = styled.div<PopKPIComparisonValueStyleProps>`
@@ -73,6 +85,8 @@ export default function PopKPI(props: PopKPIProps) {
7385
prevNumber,
7486
valueDifference,
7587
percentDifferenceFormattedString,
88+
metricName,
89+
metricNameFontSize,
7690
headerFontSize,
7791
subheaderFontSize,
7892
comparisonColorEnabled,
@@ -84,8 +98,8 @@ export default function PopKPI(props: PopKPIProps) {
8498
subtitle,
8599
subtitleFontSize,
86100
dashboardTimeRange,
101+
showMetricName,
87102
} = props;
88-
89103
const [comparisonRange, setComparisonRange] = useState<string>('');
90104

91105
useEffect(() => {
@@ -260,9 +274,16 @@ export default function PopKPI(props: PopKPIProps) {
260274
width: fit-content;
261275
margin: auto;
262276
align-items: flex-start;
277+
overflow: auto;
263278
`
264279
}
265280
>
281+
{showMetricName && metricName && (
282+
<MetricNameText metricNameFontSize={metricNameFontSize}>
283+
{metricName}
284+
</MetricNameText>
285+
)}
286+
266287
<div css={bigValueContainerStyles}>
267288
{bigNumber}
268289
{percentDifferenceNumber !== 0 && (

superset-frontend/plugins/plugin-chart-echarts/src/BigNumber/BigNumberPeriodOverPeriod/controlPanel.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import {
2828
subheaderFontSize,
2929
subtitleControl,
3030
subtitleFontSize,
31+
showMetricNameControl,
32+
metricNameFontSizeWithVisibility,
3133
} from '../sharedControls';
3234
import { ColorSchemeEnum } from './types';
3335

@@ -70,6 +72,8 @@ const config: ControlPanelConfig = {
7072
],
7173
[subtitleControl],
7274
[subtitleFontSize],
75+
[showMetricNameControl],
76+
[metricNameFontSizeWithVisibility],
7377
[
7478
{
7579
...subheaderFontSize,

superset-frontend/plugins/plugin-chart-echarts/src/BigNumber/BigNumberPeriodOverPeriod/transformProps.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,13 @@ import {
2626
SimpleAdhocFilter,
2727
ensureIsArray,
2828
} from '@superset-ui/core';
29-
import { getComparisonFontSize, getHeaderFontSize } from './utils';
29+
import {
30+
getComparisonFontSize,
31+
getHeaderFontSize,
32+
getMetricNameFontSize,
33+
} from './utils';
34+
35+
import { getOriginalLabel } from '../utils';
3036

3137
dayjs.extend(utc);
3238

@@ -83,6 +89,7 @@ export default function transformProps(chartProps: ChartProps) {
8389
headerFontSize,
8490
headerText,
8591
metric,
92+
metricNameFontSize,
8693
yAxisFormat,
8794
currencyFormat,
8895
subheaderFontSize,
@@ -91,11 +98,14 @@ export default function transformProps(chartProps: ChartProps) {
9198
percentDifferenceFormat,
9299
subtitle = '',
93100
subtitleFontSize,
94-
columnConfig,
101+
columnConfig = {},
95102
} = formData;
96103
const { data: dataA = [] } = queriesData[0];
97104
const data = dataA;
98105
const metricName = metric ? getMetricLabel(metric) : '';
106+
const metrics = chartProps.datasource?.metrics || [];
107+
const originalLabel = getOriginalLabel(metric, metrics);
108+
const showMetricName = chartProps.rawFormData?.show_metric_name ?? false;
99109
const timeComparison = ensureIsArray(chartProps.rawFormData?.time_compare)[0];
100110
const startDateOffset = chartProps.rawFormData?.start_date_offset;
101111
const currentTimeRangeFilter = chartProps.rawFormData?.adhoc_filters?.filter(
@@ -179,14 +189,16 @@ export default function transformProps(chartProps: ChartProps) {
179189
width,
180190
height,
181191
data,
182-
metricName,
192+
metricName: originalLabel,
183193
bigNumber,
184194
prevNumber,
185195
valueDifference,
186196
percentDifferenceFormattedString: percentDifference,
187197
boldText,
188198
subtitle,
189199
subtitleFontSize,
200+
showMetricName,
201+
metricNameFontSize: getMetricNameFontSize(metricNameFontSize),
190202
headerFontSize: getHeaderFontSize(headerFontSize),
191203
subheaderFontSize: getComparisonFontSize(subheaderFontSize),
192204
headerText,

superset-frontend/plugins/plugin-chart-echarts/src/BigNumber/BigNumberPeriodOverPeriod/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ export type PopKPIProps = PopKPIStylesProps &
6161
data: TimeseriesDataRecord[];
6262
metrics: Metric[];
6363
metricName: string;
64+
metricNameFontSize?: number;
65+
showMetricName: boolean;
6466
bigNumber: string;
6567
prevNumber: string;
6668
subtitle?: string;

superset-frontend/plugins/plugin-chart-echarts/src/BigNumber/BigNumberPeriodOverPeriod/utils.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,19 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19-
import { headerFontSize, subheaderFontSize } from '../sharedControls';
19+
import {
20+
headerFontSize,
21+
subheaderFontSize,
22+
metricNameFontSize,
23+
} from '../sharedControls';
2024

2125
const headerFontSizes = [16, 20, 30, 48, 60];
22-
const comparisonFontSizes = [16, 20, 26, 32, 40];
26+
const sharedFontSizes = [16, 20, 26, 32, 40];
27+
28+
const metricNameProportionValues =
29+
metricNameFontSize.config.options.map(
30+
(option: { label: string; value: number }) => option.value,
31+
) ?? [];
2332

2433
const headerProportionValues =
2534
headerFontSize.config.options.map(
@@ -40,20 +49,28 @@ const getFontSizeMapping = (
4049
return acc;
4150
}, {});
4251

52+
const metricNameFontSizesMapping = getFontSizeMapping(
53+
metricNameProportionValues,
54+
sharedFontSizes,
55+
);
4356
const headerFontSizesMapping = getFontSizeMapping(
4457
headerProportionValues,
4558
headerFontSizes,
4659
);
4760

4861
const comparisonFontSizesMapping = getFontSizeMapping(
4962
subheaderProportionValues,
50-
comparisonFontSizes,
63+
sharedFontSizes,
5164
);
5265

66+
export const getMetricNameFontSize = (proportionValue: number) =>
67+
metricNameFontSizesMapping[proportionValue] ??
68+
sharedFontSizes[sharedFontSizes.length - 1];
69+
5370
export const getHeaderFontSize = (proportionValue: number) =>
5471
headerFontSizesMapping[proportionValue] ??
5572
headerFontSizes[headerFontSizes.length - 1];
5673

5774
export const getComparisonFontSize = (proportionValue: number) =>
5875
comparisonFontSizesMapping[proportionValue] ??
59-
comparisonFontSizes[comparisonFontSizes.length - 1];
76+
sharedFontSizes[sharedFontSizes.length - 1];

superset-frontend/plugins/plugin-chart-echarts/src/BigNumber/BigNumberTotal/controlPanel.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import {
2828
headerFontSize,
2929
subtitleFontSize,
3030
subtitleControl,
31+
showMetricNameControl,
32+
metricNameFontSizeWithVisibility,
3133
} from '../sharedControls';
3234

3335
export default {
@@ -44,6 +46,8 @@ export default {
4446
[headerFontSize],
4547
[subtitleControl],
4648
[subtitleFontSize],
49+
[showMetricNameControl],
50+
[metricNameFontSizeWithVisibility],
4751
['y_axis_format'],
4852
['currency_format'],
4953
[

superset-frontend/plugins/plugin-chart-echarts/src/BigNumber/BigNumberTotal/transformProps.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ jest.mock('@superset-ui/core', () => ({
3636
jest.mock('../utils', () => ({
3737
getDateFormatter: jest.fn(() => (v: any) => `${v}pm`),
3838
parseMetricValue: jest.fn(val => Number(val)),
39+
getOriginalLabel: jest.fn((metric, metrics) => metric),
3940
}));
4041

4142
describe('BigNumberTotal transformProps', () => {

superset-frontend/plugins/plugin-chart-echarts/src/BigNumber/BigNumberTotal/transformProps.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import {
2929
getValueFormatter,
3030
} from '@superset-ui/core';
3131
import { BigNumberTotalChartProps, BigNumberVizProps } from '../types';
32-
import { getDateFormatter, parseMetricValue } from '../utils';
32+
import { getDateFormatter, getOriginalLabel, parseMetricValue } from '../utils';
3333
import { Refs } from '../../types';
3434

3535
export default function transformProps(
@@ -45,6 +45,7 @@ export default function transformProps(
4545
datasource: { currencyFormats = {}, columnFormats = {} },
4646
} = chartProps;
4747
const {
48+
metricNameFontSize,
4849
headerFontSize,
4950
metric = 'value',
5051
subtitle,
@@ -58,9 +59,12 @@ export default function transformProps(
5859
subheaderFontSize,
5960
} = formData;
6061
const refs: Refs = {};
61-
const { data = [], coltypes = [] } = queriesData[0];
62+
const { data = [], coltypes = [] } = queriesData[0] || {};
6263
const granularity = extractTimegrain(rawFormData as QueryFormData);
64+
const metrics = chartProps.datasource?.metrics || [];
65+
const originalLabel = getOriginalLabel(metric, metrics);
6366
const metricName = getMetricLabel(metric);
67+
const showMetricName = chartProps.rawFormData?.show_metric_name ?? false;
6468
const formattedSubtitle = subtitle?.trim() ? subtitle : subheader || '';
6569
const formattedSubtitleFontSize = subtitle?.trim()
6670
? (subtitleFontSize ?? 1)
@@ -103,7 +107,6 @@ export default function transformProps(
103107
const colorThresholdFormatters =
104108
getColorFormatters(conditionalFormatting, data, false) ??
105109
defaultColorFormatters;
106-
107110
return {
108111
width,
109112
height,
@@ -116,5 +119,8 @@ export default function transformProps(
116119
onContextMenu,
117120
refs,
118121
colorThresholdFormatters,
122+
metricName: originalLabel,
123+
showMetricName,
124+
metricNameFontSize,
119125
};
120126
}

0 commit comments

Comments
 (0)