1+ /* eslint-disable @wordpress/no-unsafe-wp-apis */
2+ import {
3+ __experimentalText as WPText ,
4+ __experimentalHStack as HStack ,
5+ } from '@wordpress/components' ;
6+ import { BaseLegendItem } from '../../../components/legend/types' ;
17import {
28 chartDecorator ,
39 sharedChartArgTypes ,
@@ -7,7 +13,7 @@ import {
713} from '../../../stories' ;
814import { Group } from '../../../visx/group' ;
915import { Text } from '../../../visx/text' ;
10- import { PieChart } from '../../pie-chart' ;
16+ import { PieChart , PieChartUnresponsive } from '../../pie-chart' ;
1117import type { Meta , StoryObj } from '@storybook/react' ;
1218
1319type StoryArgs = ChartStoryArgs < React . ComponentProps < typeof PieChart > > ;
@@ -85,7 +91,6 @@ export const Default: Story = {
8591 resize : 'none' ,
8692 thickness : 0.5 ,
8793 gapScale : 0.03 ,
88- padding : 20 ,
8994 cornerScale : 0.03 ,
9095 withTooltips : true ,
9196 data,
@@ -197,6 +202,7 @@ export const WithLegend: Story = {
197202 args : {
198203 ...Default . args ,
199204 showLegend : true ,
205+ containerHeight : '500px' ,
200206 } ,
201207} ;
202208
@@ -264,6 +270,7 @@ export const WithCompositionLegend: Story = {
264270 args : {
265271 data,
266272 thickness : 0.5 ,
273+ containerHeight : '500px' ,
267274 } ,
268275 parameters : {
269276 docs : {
@@ -319,3 +326,109 @@ export const CustomLegendPositioning: Story = {
319326 } ,
320327 } ,
321328} ;
329+
330+ const WooPieLegend = ( {
331+ legendItems,
332+ legendData,
333+ withComparison = false ,
334+ } : {
335+ legendItems : BaseLegendItem [ ] ;
336+ legendData : { label : string ; value : number ; formattedValue : string ; comparison ?: number } [ ] ;
337+ withComparison ?: boolean ;
338+ } ) => (
339+ < div
340+ style = { {
341+ display : 'inline-grid' ,
342+ gridTemplateColumns : '1fr auto auto' ,
343+ gap : 'var(--wpds-spacing-05, 5px) var(--wpds-spacing-10, 10px)' ,
344+ } }
345+ >
346+ { legendData . map ( ( item , index ) => {
347+ const { color } = legendItems [ index ] ;
348+
349+ return (
350+ < >
351+ < HStack direction = "row" justify = "flex-start" gap = { 2 } >
352+ < div
353+ style = { {
354+ width : '8px' ,
355+ height : '8px' ,
356+ borderRadius : '50%' ,
357+ flexShrink : 0 ,
358+ backgroundColor : color ,
359+ } }
360+ />
361+ < WPText size = "small" > { item . label } </ WPText >
362+ </ HStack >
363+ < WPText size = "small" weight = { 600 } style = { { textAlign : 'right' } } >
364+ { item . formattedValue }
365+ </ WPText >
366+ < WPText size = "small" style = { { textAlign : 'right' , color : '#008a20' } } >
367+ { withComparison && item . comparison }
368+ </ WPText >
369+ </ >
370+ ) ;
371+ } ) }
372+ </ div >
373+ ) ;
374+
375+ export const CustomLegend : Story = {
376+ render : args => (
377+ < PieChartUnresponsive { ...args } >
378+ < PieChartUnresponsive . Legend
379+ // eslint-disable-next-line react/jsx-no-bind
380+ render = { items => (
381+ < WooPieLegend
382+ legendItems = { items }
383+ legendData = { args . legendData }
384+ withComparison = { args . withComparison }
385+ />
386+ ) }
387+ />
388+ </ PieChartUnresponsive >
389+ ) ,
390+ args : {
391+ ...Default . args ,
392+ data : [
393+ {
394+ label : '' ,
395+ value : 302331.26999999984 ,
396+ percentage : 66.97002374697931 ,
397+ } ,
398+ {
399+ label : '' ,
400+ value : 149111.40999999995 ,
401+ percentage : 33.029976253020635 ,
402+ } ,
403+ ] ,
404+ legendData : [
405+ {
406+ label : 'New' ,
407+ value : 302331.26999999984 ,
408+ formattedValue : '$302.33K' ,
409+ comparison : '14%' ,
410+ } ,
411+ {
412+ label : 'Returning' ,
413+ value : 149111.40999999995 ,
414+ formattedValue : '$149.11K' ,
415+ comparison : '133%' ,
416+ } ,
417+ ] ,
418+ showLegend : false ,
419+ children : null ,
420+ thickness : 0.3 ,
421+ cornerScale : 0.03 ,
422+ gapScale : 0.01 ,
423+ size : 164 ,
424+ withComparison : true ,
425+ withTooltips : false ,
426+ } ,
427+ parameters : {
428+ docs : {
429+ description : {
430+ story : 'Demonstrates how to customize the legend using the render prop.' ,
431+ } ,
432+ } ,
433+ } ,
434+ } ;
0 commit comments