diff --git a/projects/js-packages/charts/changelog/charts-65-iterate-over-pie-and-piesemicircle-composition b/projects/js-packages/charts/changelog/charts-65-iterate-over-pie-and-piesemicircle-composition new file mode 100644 index 0000000000000..508251433ccfe --- /dev/null +++ b/projects/js-packages/charts/changelog/charts-65-iterate-over-pie-and-piesemicircle-composition @@ -0,0 +1,4 @@ +Significance: minor +Type: changed + +Charts: improve pie semi circle chart composition diff --git a/projects/js-packages/charts/src/components/pie-semi-circle-chart/pie-semi-circle-chart.tsx b/projects/js-packages/charts/src/components/pie-semi-circle-chart/pie-semi-circle-chart.tsx index ba25fab031e1c..4ee1986778bc8 100644 --- a/projects/js-packages/charts/src/components/pie-semi-circle-chart/pie-semi-circle-chart.tsx +++ b/projects/js-packages/charts/src/components/pie-semi-circle-chart/pie-semi-circle-chart.tsx @@ -1,7 +1,6 @@ import { localPoint } from '@visx/event'; import { Group } from '@visx/group'; import { Pie } from '@visx/shape'; -import { Text } from '@visx/text'; import { useTooltip, useTooltipInPortal } from '@visx/tooltip'; import clsx from 'clsx'; import { useCallback, useContext, useMemo } from 'react'; @@ -46,18 +45,9 @@ export interface PieSemiCircleChartProps extends BaseChartProps< DataPointPercen */ clockwise?: boolean; - /** - * Label text to display above the chart - */ - label?: string; - - /** - * Note text to display below the label - */ - note?: string; - /** * Use the children prop to render additional elements on the chart. + * Supports composition API with PieSemiCircleChart.SVG and PieSemiCircleChart.HTML components. */ children?: ReactNode; @@ -133,8 +123,6 @@ const PieSemiCircleChartInternal: FC< PieSemiCircleChartProps > = ( { legendItemClassName, legendShape = 'circle', legendValueDisplay = 'percentage', - label, - note, className, children, tooltipOffsetX = 0, @@ -316,26 +304,6 @@ const PieSemiCircleChartInternal: FC< PieSemiCircleChartProps > = ( { } } - { /* Label and note text */ } - - - { label } - - - { note } - - - { /* Render SVG children from composition API */ } { svgChildren } diff --git a/projects/js-packages/charts/src/components/pie-semi-circle-chart/stories/index.stories.tsx b/projects/js-packages/charts/src/components/pie-semi-circle-chart/stories/index.stories.tsx index 408077883698f..1589cc7637c67 100644 --- a/projects/js-packages/charts/src/components/pie-semi-circle-chart/stories/index.stories.tsx +++ b/projects/js-packages/charts/src/components/pie-semi-circle-chart/stories/index.stories.tsx @@ -53,9 +53,23 @@ export const Default: Story = { resize: 'none', thickness: 0.4, data, - label: 'OS', - note: 'Windows +10%', clockwise: true, + children: ( + + + OS + + + Windows +10% + + + ), }, }; @@ -95,24 +109,46 @@ export const WithCompositionLegend: Story = { + > + + + Performance Metrics + + + Q4 2023 Results + + +

Composition API with Legend Component

- + + + + Performance Metrics + + + Q4 2023 Results + + + + OS + + + Windows +10% + + + ), }, parameters: { docs: { @@ -261,15 +311,15 @@ export const CompositionAPI: Story = { >

With Custom SVG Elements

- + + + OS Usage + + + Q4 2023 +

With Custom Legend and HTML Content

- + + + + + Performance + + + Latest Results + + +
-

Legacy Support - Direct Group Components

+

Direct Group Components

- For backward compatibility, Group components are still supported directly: + Group components are supported directly for simpler use cases:

- + + + Direct Usage + + + Simple and clean! + - Direct Group usage + Additional annotation diff --git a/projects/js-packages/charts/src/components/pie-semi-circle-chart/test/pie-semi-circle-chart.test.tsx b/projects/js-packages/charts/src/components/pie-semi-circle-chart/test/pie-semi-circle-chart.test.tsx index 38098ed471bcc..8f27a63a0d82a 100644 --- a/projects/js-packages/charts/src/components/pie-semi-circle-chart/test/pie-semi-circle-chart.test.tsx +++ b/projects/js-packages/charts/src/components/pie-semi-circle-chart/test/pie-semi-circle-chart.test.tsx @@ -1,5 +1,7 @@ import { render, screen, waitFor } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; +import { Group } from '@visx/group'; +import { Text } from '@visx/text'; import { act } from 'react'; import { GlobalChartsProvider } from '../../../providers'; import PieSemiCircleChart from '../pie-semi-circle-chart'; @@ -35,13 +37,23 @@ describe( 'PieSemiCircleChart', () => { expect( segments ).toHaveLength( 2 ); } ); - it( 'renders label and note when provided', () => { - const label = 'Chart Title'; - const note = 'Additional Info'; - renderPieChart( { data: mockData, label, note } ); + it( 'renders custom children content', () => { + renderPieChart( { + data: mockData, + children: ( + + + Chart Title + + + Additional Info + + + ), + } ); - expect( screen.getByText( label ) ).toBeInTheDocument(); - expect( screen.getByText( note ) ).toBeInTheDocument(); + expect( screen.getByText( 'Chart Title' ) ).toBeInTheDocument(); + expect( screen.getByText( 'Additional Info' ) ).toBeInTheDocument(); } ); it( 'shows legend when showLegend is true', () => { @@ -178,4 +190,89 @@ describe( 'PieSemiCircleChart', () => { ).toBeInTheDocument(); } ); } ); + + describe( 'Composition API', () => { + it( 'renders children with SVG content', () => { + renderPieChart( { + data: mockData, + children: ( + + + Custom Title + + + ), + } ); + + expect( screen.getByText( 'Custom Title' ) ).toBeInTheDocument(); + } ); + + it( 'renders PieSemiCircleChart.SVG compound component', () => { + render( + + + + + + SVG Component Title + + + + + + ); + + expect( screen.getByText( 'SVG Component Title' ) ).toBeInTheDocument(); + } ); + + it( 'renders PieSemiCircleChart.HTML compound component', () => { + render( + + + +
HTML Content
+
+
+
+ ); + + expect( screen.getByTestId( 'html-content' ) ).toBeInTheDocument(); + expect( screen.getByText( 'HTML Content' ) ).toBeInTheDocument(); + } ); + + it( 'renders mixed SVG and HTML compound components', () => { + render( + + + + + + SVG Title + + + + +
Chart Footer
+
+
+
+ ); + + expect( screen.getByText( 'SVG Title' ) ).toBeInTheDocument(); + expect( screen.getByTestId( 'footer' ) ).toBeInTheDocument(); + } ); + + it( 'renders Legend as compound component', () => { + render( + + + + + + ); + + expect( screen.getByText( 'Category A' ) ).toBeInTheDocument(); + expect( screen.getByText( 'Category B' ) ).toBeInTheDocument(); + } ); + } ); } ); diff --git a/projects/js-packages/charts/src/providers/chart-context/stories/index.stories.tsx b/projects/js-packages/charts/src/providers/chart-context/stories/index.stories.tsx index fe1506ab589bc..5e0c3516f15e2 100644 --- a/projects/js-packages/charts/src/providers/chart-context/stories/index.stories.tsx +++ b/projects/js-packages/charts/src/providers/chart-context/stories/index.stories.tsx @@ -1,4 +1,6 @@ import { Meta, StoryObj } from '@storybook/react'; +import { Group } from '@visx/group'; +import { Text } from '@visx/text'; import { LineChart, BarChart, @@ -186,10 +188,21 @@ const ChartGrid = ( { args }: { args: StoryArgs } ) => { + > + + + Semi-Circle Chart + + + @@ -247,10 +260,21 @@ const ChartGridWithColorOverrides = ( { args }: { args: StoryArgs } ) => { + > + + + Semi-Circle Chart + + +