-
Notifications
You must be signed in to change notification settings - Fork 14.5k
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): Adding colors to BigNumber with Time Comparison chart #27052
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,11 @@ | |
*/ | ||
import React, { createRef } from 'react'; | ||
import { css, styled, useTheme } from '@superset-ui/core'; | ||
import { PopKPIComparisonValueStyleProps, PopKPIProps } from './types'; | ||
import { | ||
PopKPIComparisonSymbolStyleProps, | ||
PopKPIComparisonValueStyleProps, | ||
PopKPIProps, | ||
} from './types'; | ||
|
||
const ComparisonValue = styled.div<PopKPIComparisonValueStyleProps>` | ||
${({ theme, subheaderFontSize }) => ` | ||
|
@@ -30,16 +34,51 @@ | |
`} | ||
`; | ||
|
||
const SymbolWrapper = styled.div<PopKPIComparisonSymbolStyleProps>` | ||
${({ theme, percentDifferenceNumber, comparisonColorEnabled }) => { | ||
const defaultBackgroundColor = theme.colors.grayscale.light4; | ||
const defaultTextColor = theme.colors.grayscale.base; | ||
const isPositiveChange = percentDifferenceNumber > 0; | ||
const backgroundColor = | ||
percentDifferenceNumber === 0 | ||
? defaultBackgroundColor | ||
: isPositiveChange | ||
? theme.colors.success.light2 | ||
: theme.colors.error.light2; | ||
const textColor = | ||
percentDifferenceNumber === 0 | ||
? defaultTextColor | ||
: isPositiveChange | ||
? theme.colors.success.base | ||
: theme.colors.error.base; | ||
|
||
return ` | ||
background-color: ${ | ||
comparisonColorEnabled ? backgroundColor : defaultBackgroundColor | ||
}; | ||
color: ${comparisonColorEnabled ? textColor : defaultTextColor}; | ||
padding: 2px 5px; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could we use full grid units? Or does it look weird? |
||
border-radius: ${theme.gridUnit * 2}px; | ||
display: inline-block; | ||
margin-right: ${theme.gridUnit}px; | ||
`; | ||
}} | ||
`; | ||
|
||
const SYMBOLS = ['#', '△', '%']; | ||
|
||
export default function PopKPI(props: PopKPIProps) { | ||
const { | ||
height, | ||
width, | ||
bigNumber, | ||
prevNumber, | ||
valueDifference, | ||
percentDifference, | ||
percentDifferenceFormattedString, | ||
headerFontSize, | ||
subheaderFontSize, | ||
comparisonColorEnabled, | ||
percentDifferenceNumber, | ||
} = props; | ||
|
||
const rootElem = createRef<HTMLDivElement>(); | ||
|
@@ -63,9 +102,28 @@ | |
text-align: center; | ||
`; | ||
|
||
const getArrowIndicatorColor = () => { | ||
if (!comparisonColorEnabled) return theme.colors.grayscale.base; | ||
return percentDifferenceNumber > 0 | ||
? theme.colors.success.base | ||
: theme.colors.error.base; | ||
}; | ||
|
||
const arrowIndicatorStyle = css` | ||
color: ${getArrowIndicatorColor()}; | ||
margin-left: ${theme.gridUnit}px; | ||
`; | ||
|
||
return ( | ||
<div ref={rootElem} css={wrapperDivStyles}> | ||
<div css={bigValueContainerStyles}>{bigNumber}</div> | ||
<div css={bigValueContainerStyles}> | ||
{bigNumber} | ||
{percentDifferenceNumber !== 0 && ( | ||
<span css={arrowIndicatorStyle}> | ||
{percentDifferenceNumber > 0 ? '↑' : '↓'} | ||
</span> | ||
)} | ||
</div> | ||
<div | ||
css={css` | ||
width: 100%; | ||
|
@@ -77,18 +135,26 @@ | |
display: table-row; | ||
`} | ||
> | ||
<ComparisonValue subheaderFontSize={subheaderFontSize}> | ||
{' '} | ||
#: {prevNumber} | ||
</ComparisonValue> | ||
<ComparisonValue subheaderFontSize={subheaderFontSize}> | ||
{' '} | ||
Δ: {valueDifference} | ||
</ComparisonValue> | ||
<ComparisonValue subheaderFontSize={subheaderFontSize}> | ||
{' '} | ||
%: {percentDifference} | ||
</ComparisonValue> | ||
{SYMBOLS.map((symbol, index) => ( | ||
<ComparisonValue | ||
key={`comparison-symbol-${symbol}`} | ||
subheaderFontSize={subheaderFontSize} | ||
> | ||
<SymbolWrapper | ||
percentDifferenceNumber={ | ||
index > 0 ? percentDifferenceNumber : 0 | ||
} | ||
comparisonColorEnabled={comparisonColorEnabled} | ||
> | ||
{symbol} | ||
</SymbolWrapper> | ||
{index === 0 | ||
? prevNumber | ||
: index === 1 | ||
? valueDifference | ||
: percentDifferenceFormattedString} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm I'm not sure about coupling the values with indexes like this. I think it would be cleaner if we had some key-value mapping between symbols and values, or an array like [['#', prevNumber], ['△', valueDifference], ['%', percentDifferenceFormattedString]] |
||
</ComparisonValue> | ||
))} | ||
</div> | ||
</div> | ||
</div> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,7 @@ export interface PopKPIStylesProps { | |
headerFontSize: keyof typeof supersetTheme.typography.sizes; | ||
subheaderFontSize: keyof typeof supersetTheme.typography.sizes; | ||
boldText: boolean; | ||
comparisonColorEnabled: boolean; | ||
} | ||
|
||
interface PopKPICustomizeProps { | ||
|
@@ -39,6 +40,11 @@ export interface PopKPIComparisonValueStyleProps { | |
subheaderFontSize?: keyof typeof supersetTheme.typography.sizes; | ||
} | ||
|
||
export interface PopKPIComparisonSymbolStyleProps { | ||
percentDifferenceNumber: number; | ||
comparisonColorEnabled: boolean; | ||
} | ||
|
||
export type PopKPIQueryFormData = QueryFormData & | ||
PopKPIStylesProps & | ||
PopKPICustomizeProps; | ||
|
@@ -48,9 +54,10 @@ export type PopKPIProps = PopKPIStylesProps & | |
data: TimeseriesDataRecord[]; | ||
metrics: Metric[]; | ||
metricName: String; | ||
bigNumber: Number; | ||
prevNumber: Number; | ||
valueDifference: Number; | ||
percentDifference: Number; | ||
bigNumber: string; | ||
prevNumber: string; | ||
valueDifference: string; | ||
percentDifferenceFormattedString: string; | ||
compType: String; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we also fix those capital S strings? 😛 |
||
percentDifferenceNumber: number; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting approach with putting js inside of styled component like that! But I wonder if deducing colours from more "high-level" data belongs to a styled component. Maybe we should move that logic to
PopKPI
and just passbackgroundColor
andtextColor
toSymbolWrapper
?But if you think this solution is better then I won't argue, I don't have a strong opinion here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hehe yeah, I had it like that before and moved to this, it doesn't feel quite "well" but now that you noticed too, let's move it there instead. Thanks