-
Notifications
You must be signed in to change notification settings - Fork 835
Charts: Add custom legend rendering #45347
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
Changes from all commits
b29e11e
0a3c531
afc6c6b
9b9d0a8
93ae3bd
be74ad6
559bff5
21c4a02
840cfd3
d632b02
28e207e
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Significance: minor | ||
Type: added | ||
|
||
Charts: Add custom legend support |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,6 @@ | |
display: flex; | ||
flex-direction: column; | ||
overflow: hidden; | ||
align-items: center; | ||
gap: 20px; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -142,7 +142,7 @@ const PieChartInternal = ( { | |
legendShape = 'circle', | ||
size, | ||
thickness = 1, | ||
padding = 20, | ||
padding = 0, | ||
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. This is being set to 0 by our consumers and it seems a more sensible default |
||
gapScale = 0, | ||
cornerScale = 0, | ||
showLabels = true, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,21 @@ | ||
/* eslint-disable @wordpress/no-unsafe-wp-apis */ | ||
import { | ||
__experimentalText as WPText, | ||
__experimentalHStack as HStack, | ||
} from '@wordpress/components'; | ||
import { Fragment } from 'react'; | ||
import { BaseLegendItem } from '../../../components/legend/types'; | ||
import { | ||
chartDecorator, | ||
sharedChartArgTypes, | ||
ChartStoryArgs, | ||
legendArgTypes, | ||
themeArgTypes, | ||
} from '../../../stories'; | ||
import { customerRevenueData, customerRevenueLegendData } from '../../../stories/sample-data'; | ||
import { Group } from '../../../visx/group'; | ||
import { Text } from '../../../visx/text'; | ||
import { PieChart } from '../../pie-chart'; | ||
import { PieChart, PieChartUnresponsive } from '../../pie-chart'; | ||
import type { Meta, StoryObj } from '@storybook/react'; | ||
|
||
type StoryArgs = ChartStoryArgs< React.ComponentProps< typeof PieChart > >; | ||
|
@@ -85,7 +93,6 @@ export const Default: Story = { | |
resize: 'none', | ||
thickness: 0.5, | ||
gapScale: 0.03, | ||
padding: 20, | ||
cornerScale: 0.03, | ||
withTooltips: true, | ||
data, | ||
|
@@ -197,6 +204,7 @@ export const WithLegend: Story = { | |
args: { | ||
...Default.args, | ||
showLegend: true, | ||
containerHeight: '500px', | ||
}, | ||
}; | ||
|
||
|
@@ -264,6 +272,7 @@ export const WithCompositionLegend: Story = { | |
args: { | ||
data, | ||
thickness: 0.5, | ||
containerHeight: '500px', | ||
}, | ||
parameters: { | ||
docs: { | ||
|
@@ -319,3 +328,83 @@ export const CustomLegendPositioning: Story = { | |
}, | ||
}, | ||
}; | ||
|
||
const WooPieLegend = ( { | ||
chartItems, | ||
items, | ||
withComparison, | ||
}: { | ||
chartItems: BaseLegendItem[]; | ||
items: { label: string; value: number; formattedValue: string; comparison: string }[]; | ||
withComparison: boolean; | ||
} ) => ( | ||
<div | ||
style={ { | ||
display: 'inline-grid', | ||
gridTemplateColumns: '1fr auto auto', | ||
gap: 'var(--wpds-spacing-05, 5px) var(--wpds-spacing-10, 10px)', | ||
} } | ||
> | ||
{ items.map( ( item, index ) => { | ||
const { color } = chartItems[ index ]; | ||
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. Extract the color from the legend items in the global charts context, which have a resolved color based on the theme, group, overrides, etc. |
||
|
||
return ( | ||
<Fragment key={ index }> | ||
<HStack direction="row" justify="flex-start" gap={ 2 }> | ||
<div | ||
style={ { | ||
width: '8px', | ||
height: '8px', | ||
borderRadius: '50%', | ||
flexShrink: 0, | ||
backgroundColor: color, | ||
} } | ||
/> | ||
<WPText size="small">{ item.label }</WPText> | ||
</HStack> | ||
<WPText size="small" weight={ 600 } style={ { textAlign: 'right' } }> | ||
{ item.formattedValue } | ||
</WPText> | ||
<WPText size="small" style={ { textAlign: 'right', color: '#008a20' } }> | ||
{ withComparison && item.comparison } | ||
</WPText> | ||
</Fragment> | ||
); | ||
} ) } | ||
</div> | ||
); | ||
|
||
export const CustomLegend: Story = { | ||
render: args => ( | ||
<PieChartUnresponsive { ...args }> | ||
<PieChartUnresponsive.Legend | ||
// eslint-disable-next-line react/jsx-no-bind | ||
render={ items => ( | ||
<WooPieLegend | ||
chartItems={ items } | ||
items={ customerRevenueLegendData } | ||
withComparison={ args.withComparison } | ||
/> | ||
) } | ||
/> | ||
</PieChartUnresponsive> | ||
), | ||
args: { | ||
...Default.args, | ||
data: customerRevenueData.map( segment => ( { ...segment, label: '' } ) ), | ||
thickness: 0.3, | ||
cornerScale: 0.03, | ||
gapScale: 0.01, | ||
size: 164, | ||
withComparison: true, | ||
withTooltips: false, | ||
containerHeight: '300px', | ||
}, | ||
parameters: { | ||
docs: { | ||
description: { | ||
story: 'Demonstrates how to customize the legend using the render prop.', | ||
}, | ||
}, | ||
}, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -129,8 +129,8 @@ const wooTheme: ChartTheme = { | |
labelTextColor: '#FFFFFF', // label text color (white to match original behavior) | ||
colors: [ | ||
'#3858E9', // WooCommerce brand blue | ||
'#873EFF', // Purple | ||
'#66BDFF', // Light blue | ||
'#873EFF', // Purple | ||
Comment on lines
-132
to
+133
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. Swap this order to match that in Woo Analytics |
||
'#7B90FF', // Periwinkle blue | ||
'#EB6594', // Pink/rose | ||
], | ||
|
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.
Add some default space between the chart and legend. Can be customised using a className.