-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add legend with values + trend indicator data
- Loading branch information
1 parent
90d2535
commit a503db7
Showing
17 changed files
with
275 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
packages/polaris-viz/src/components/DonutChart/components/LegendValues/LegendValues.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
.Table { | ||
min-width: 200px; | ||
width: 100%; | ||
border-collapse: separate; | ||
border-spacing: 0 10px; | ||
|
||
.ColorPreview { | ||
width: 12px; | ||
} | ||
|
||
.Name { | ||
padding-left: 4px; | ||
} | ||
|
||
.TableSpacer { | ||
padding-left: 30px; | ||
} | ||
|
||
.alignLeft { | ||
text-align: left; | ||
} | ||
|
||
.alignRight { | ||
text-align: right; | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
packages/polaris-viz/src/components/DonutChart/components/LegendValues/LegendValues.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import type {ColorVisionInteractionMethods, DataSeries} from 'index'; | ||
import type {LabelFormatter} from '@shopify/polaris-viz-core'; | ||
import {useTheme} from '@shopify/polaris-viz-core'; | ||
|
||
import {TrendIndicator} from '../../../TrendIndicator'; | ||
import {getTrendIndicatorData} from '../../../../utilities/getTrendIndicatorData'; | ||
import {SquareColorPreview} from '../../../../components/SquareColorPreview'; | ||
|
||
import styles from './LegendValues.scss'; | ||
|
||
interface LegendContentProps { | ||
data: DataSeries[]; | ||
labelFormatter: LabelFormatter; | ||
getColorVisionStyles: ColorVisionInteractionMethods['getColorVisionStyles']; | ||
getColorVisionEventAttrs: ColorVisionInteractionMethods['getColorVisionEventAttrs']; | ||
} | ||
|
||
export function LegendValues({ | ||
data, | ||
labelFormatter, | ||
getColorVisionStyles, | ||
getColorVisionEventAttrs, | ||
}: LegendContentProps) { | ||
const selectedTheme = useTheme(); | ||
|
||
const maxTrendIndicatorWidth = data.reduce((maxWidth, {metadata}) => { | ||
if (!metadata?.trend) { | ||
return maxWidth; | ||
} | ||
|
||
const {trendIndicatorWidth} = getTrendIndicatorData(metadata.trend); | ||
|
||
return Math.max(maxWidth, trendIndicatorWidth); | ||
}, 0); | ||
|
||
return ( | ||
<table className={styles.Table}> | ||
{data.map(({name, data, metadata}, index) => { | ||
const value = data[0].value; | ||
const valueExists = value !== null && value !== undefined; | ||
|
||
return ( | ||
<tr | ||
key={name} | ||
style={{ | ||
...getColorVisionStyles(index), | ||
}} | ||
{...getColorVisionEventAttrs(index)} | ||
> | ||
<td className={styles.ColorPreview}> | ||
<SquareColorPreview | ||
color={selectedTheme.seriesColors.upToEight[index]} | ||
/> | ||
</td> | ||
|
||
<td className={styles.Name}> | ||
<span | ||
style={{ | ||
color: selectedTheme.legend.labelColor, | ||
}} | ||
> | ||
{name} | ||
</span> | ||
</td> | ||
|
||
<td className={styles.TableSpacer} /> | ||
|
||
<td className={styles.alignRight}> | ||
<span | ||
style={{ | ||
color: selectedTheme.legend.labelColor, | ||
}} | ||
> | ||
{valueExists ? labelFormatter(value) : '-'} | ||
</span> | ||
</td> | ||
|
||
<td | ||
className={styles.alignLeft} | ||
style={{minWidth: maxTrendIndicatorWidth, paddingLeft: '4px'}} | ||
> | ||
<span> | ||
{metadata?.trend && valueExists && ( | ||
<TrendIndicator {...metadata.trend} /> | ||
)} | ||
</span> | ||
</td> | ||
</tr> | ||
); | ||
})} | ||
</table> | ||
); | ||
} |
1 change: 1 addition & 0 deletions
1
packages/polaris-viz/src/components/DonutChart/components/LegendValues/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export {LegendValues} from './LegendValues'; |
1 change: 1 addition & 0 deletions
1
packages/polaris-viz/src/components/DonutChart/components/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export {InnerValue} from './InnerValue'; | ||
export {LegendValues} from './LegendValues'; |
60 changes: 60 additions & 0 deletions
60
packages/polaris-viz/src/components/DonutChart/stories/WithLegendValues.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import type {Story} from '@storybook/react'; | ||
|
||
export {META as default} from './meta'; | ||
|
||
import type {DonutChartProps} from '../../DonutChart'; | ||
|
||
import {DEFAULT_PROPS, Template} from './data'; | ||
|
||
export const WithLegendValues: Story<DonutChartProps> = Template.bind({}); | ||
|
||
WithLegendValues.args = { | ||
...DEFAULT_PROPS, | ||
showLegend: true, | ||
showLegendValues: true, | ||
legendPosition: 'right', | ||
labelFormatter: (value) => `$${value}`, | ||
data: [ | ||
{ | ||
name: 'Shopify Payments', | ||
data: [{key: 'april - march', value: 50000}], | ||
metadata: { | ||
trend: { | ||
value: '5%', | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: 'Paypal', | ||
data: [{key: 'april - march', value: 25000}], | ||
metadata: { | ||
trend: { | ||
value: '50%', | ||
direction: 'downward', | ||
trend: 'negative', | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: 'Other', | ||
data: [{key: 'april - march', value: 10000}], | ||
metadata: { | ||
trend: { | ||
value: '100%', | ||
direction: 'upward', | ||
trend: 'positive', | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: 'Amazon Pay', | ||
data: [{key: 'april - march', value: 5000}], | ||
metadata: { | ||
trend: { | ||
direction: 'upward', | ||
trend: 'positive', | ||
}, | ||
}, | ||
}, | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.