Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(plugins): add color options for big number with time comparison #27524

Merged
merged 2 commits into from
Mar 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import { css, styled, t, useTheme } from '@superset-ui/core';
import { Tooltip } from '@superset-ui/chart-controls';
import {
ColorSchemeEnum,
PopKPIComparisonSymbolStyleProps,
PopKPIComparisonValueStyleProps,
PopKPIProps,
Expand Down Expand Up @@ -66,6 +67,7 @@
headerFontSize,
subheaderFontSize,
comparisonColorEnabled,
comparisonColorScheme,
percentDifferenceNumber,
comparatorText,
} = props;
Expand All @@ -90,8 +92,18 @@
`;

const getArrowIndicatorColor = () => {
if (!comparisonColorEnabled) return theme.colors.grayscale.base;
return percentDifferenceNumber > 0
if (!comparisonColorEnabled || percentDifferenceNumber === 0) {
return theme.colors.grayscale.base;

Check warning on line 96 in superset-frontend/plugins/plugin-chart-echarts/src/BigNumber/BigNumberPeriodOverPeriod/PopKPI.tsx

View check run for this annotation

Codecov / codecov/patch

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

Added line #L96 was not covered by tests
}

if (percentDifferenceNumber > 0) {
// Positive difference
return comparisonColorScheme === ColorSchemeEnum.Green
? theme.colors.success.base
: theme.colors.error.base;
}
// Negative difference
return comparisonColorScheme === ColorSchemeEnum.Red
? theme.colors.success.base
: theme.colors.error.base;
};
Expand All @@ -106,23 +118,32 @@
const { backgroundColor, textColor } = useMemo(() => {
let bgColor = defaultBackgroundColor;
let txtColor = defaultTextColor;
if (percentDifferenceNumber > 0) {
if (comparisonColorEnabled) {
bgColor = theme.colors.success.light2;
txtColor = theme.colors.success.base;
}
} else if (percentDifferenceNumber < 0) {
if (comparisonColorEnabled) {
bgColor = theme.colors.error.light2;
txtColor = theme.colors.error.base;
}
if (comparisonColorEnabled && percentDifferenceNumber !== 0) {
const useSuccess =
(percentDifferenceNumber > 0 &&
comparisonColorScheme === ColorSchemeEnum.Green) ||
(percentDifferenceNumber < 0 &&
comparisonColorScheme === ColorSchemeEnum.Red);

// Set background and text colors based on the conditions
bgColor = useSuccess
? theme.colors.success.light2
: theme.colors.error.light2;
txtColor = useSuccess
? theme.colors.success.base
: theme.colors.error.base;
}

return {
backgroundColor: bgColor,
textColor: txtColor,
};
}, [theme, comparisonColorEnabled, percentDifferenceNumber]);
}, [
theme,
comparisonColorScheme,
comparisonColorEnabled,
percentDifferenceNumber,
]);

const SYMBOLS_WITH_VALUES = useMemo(
() => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
sharedControls,
} from '@superset-ui/chart-controls';
import { headerFontSize, subheaderFontSize } from '../sharedControls';
import { ColorSchemeEnum } from './types';

const config: ControlPanelConfig = {
controlPanelSections: [
Expand Down Expand Up @@ -156,6 +157,27 @@
},
},
],
[
{
name: 'comparison_color_scheme',
config: {
type: 'SelectControl',
label: t('color scheme for comparison'),
default: ColorSchemeEnum.Green,
renderTrigger: true,
choices: [
[ColorSchemeEnum.Green, 'Green for increase, red for decrease'],
[ColorSchemeEnum.Red, 'Red for increase, green for decrease'],
],
visibility: ({ controls }) =>
controls?.comparison_color_enabled?.value === true,

Check warning on line 173 in superset-frontend/plugins/plugin-chart-echarts/src/BigNumber/BigNumberPeriodOverPeriod/controlPanel.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L173 was not covered by tests
description: t(
'Adds color to the chart symbols based on the positive or ' +
'negative change from the comparison value.',
),
},
},
],
],
},
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export default function transformProps(chartProps: ChartProps) {
yAxisFormat,
currencyFormat,
subheaderFontSize,
comparisonColorScheme,
comparisonColorEnabled,
percentDifferenceFormat,
} = formData;
Expand Down Expand Up @@ -152,6 +153,7 @@ export default function transformProps(chartProps: ChartProps) {
headerText,
compType,
comparisonColorEnabled,
comparisonColorScheme,
percentDifferenceNumber: percentDifferenceNum,
comparatorText,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,10 @@ export type PopKPIProps = PopKPIStylesProps &
compType: string;
percentDifferenceNumber: number;
comparatorText: string;
comparisonColorScheme?: string;
};

export enum ColorSchemeEnum {
Green = 'Green',
Red = 'Red',
}
Loading