-
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 3ab8830
Showing
16 changed files
with
269 additions
and
21 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
14 changes: 14 additions & 0 deletions
14
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,14 @@ | ||
.Table { | ||
min-width: 200px; | ||
width: 100%; | ||
border-collapse: separate; | ||
border-spacing: 0 10px; | ||
|
||
.alignLeft { | ||
text-align: left; | ||
} | ||
|
||
.alignRight { | ||
text-align: right; | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
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,95 @@ | ||
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 || 0; | ||
|
||
return ( | ||
<tr | ||
key={name} | ||
style={{ | ||
...getColorVisionStyles(index), | ||
}} | ||
{...getColorVisionEventAttrs(index)} | ||
> | ||
<td | ||
style={{ | ||
width: '12px', | ||
}} | ||
> | ||
<SquareColorPreview | ||
color={selectedTheme.seriesColors.upToEight[index]} | ||
/> | ||
</td> | ||
|
||
<td style={{paddingLeft: '4px'}}> | ||
<span | ||
style={{ | ||
color: selectedTheme.legend.labelColor, | ||
}} | ||
className={styles.Name} | ||
> | ||
{name} | ||
</span> | ||
</td> | ||
|
||
<td style={{paddingLeft: '30px'}} /> | ||
|
||
<td className={styles.alignRight}> | ||
<span | ||
style={{ | ||
color: selectedTheme.legend.labelColor, | ||
}} | ||
> | ||
{labelFormatter(value)} | ||
</span> | ||
</td> | ||
|
||
<td | ||
className={styles.alignLeft} | ||
style={{minWidth: maxTrendIndicatorWidth}} | ||
> | ||
<span> | ||
{metadata?.trend && <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
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,13 @@ | ||
import type {DataSeries} from '@shopify/polaris-viz-core/src/types'; | ||
|
||
import type {TrendIndicatorProps} from '../TrendIndicator'; | ||
|
||
export type MetaDataTrendIndicator = Omit<TrendIndicatorProps, 'theme'>; | ||
|
||
export interface MetaData { | ||
trend?: MetaDataTrendIndicator; | ||
} | ||
|
||
export interface DonutChartDataSeries extends DataSeries { | ||
metadata?: MetaData; | ||
} |
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
Oops, something went wrong.