From d6bdf74cba982e512f9c8ca7cb3fd6b970284f1f Mon Sep 17 00:00:00 2001 From: Jonny Gao <32176865+jonnyggao@users.noreply.github.com> Date: Fri, 28 Jun 2024 22:56:38 +0800 Subject: [PATCH] Feature: Add circleSweepDegrees, startAngle props to PieChart (#307) --- .changeset/quiet-tigers-promise.md | 6 +++++ example/app/pie-and-donut-charts.tsx | 33 ++++++++++++++++++++++++++++ lib/src/pie/PieChart.tsx | 15 ++++++++++--- website/docs/polar/pie/pie-charts.md | 8 +++++++ 4 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 .changeset/quiet-tigers-promise.md diff --git a/.changeset/quiet-tigers-promise.md b/.changeset/quiet-tigers-promise.md new file mode 100644 index 00000000..832149ae --- /dev/null +++ b/.changeset/quiet-tigers-promise.md @@ -0,0 +1,6 @@ +--- +"example": patch +"victory-native": patch +--- + +Add circleSweepDegrees and startAngle props to PieChart diff --git a/example/app/pie-and-donut-charts.tsx b/example/app/pie-and-donut-charts.tsx index b2006b0e..b9d04f09 100644 --- a/example/app/pie-and-donut-charts.tsx +++ b/example/app/pie-and-donut-charts.tsx @@ -290,6 +290,35 @@ const PieChartSimpleCustomLegend = () => { ); }; +const HalfDonutChart = () => { + const [data] = useState(DATA(2)); + + return ( + + + {() => { + return ( + <> + + + + ); + }} + + + ); +}; + export default function PieAndDonutCharts(props: { segment: string }) { const description = descriptionForRoute(props.segment); @@ -328,6 +357,10 @@ export default function PieAndDonutCharts(props: { segment: string }) { Pie Chart with Custom Legend + + Half Donut Chart + + ); diff --git a/lib/src/pie/PieChart.tsx b/lib/src/pie/PieChart.tsx index fb3b41c0..4787198d 100644 --- a/lib/src/pie/PieChart.tsx +++ b/lib/src/pie/PieChart.tsx @@ -10,10 +10,17 @@ const CIRCLE_SWEEP_DEGREES = 360; type PieChartProps = { children?: (args: { slice: PieSliceData }) => React.ReactNode; innerRadius?: number | string; + circleSweepDegrees?: number; + startAngle?: number; }; export const PieChart = (props: PieChartProps) => { - const { innerRadius = 0, children } = props; + const { + innerRadius = 0, + circleSweepDegrees = CIRCLE_SWEEP_DEGREES, + startAngle: _startAngle = 0, + children, + } = props; const { canvasSize, data: _data, @@ -33,7 +40,7 @@ export const PieChart = (props: PieChartProps) => { const center = vec(width / 2, height / 2); const data = React.useMemo(() => { - let startAngle = 0; // Initialize the start angle for the first slice + let startAngle = _startAngle; // Initialize the start angle for the first slice const enhanced = _data.map((datum): PieSliceData => { const sliceValue = datum[valueKey] as number; @@ -41,7 +48,7 @@ export const PieChart = (props: PieChartProps) => { const sliceColor = datum[colorKey] as Color; const initialStartAngle = startAngle; // grab the initial start angle - const sweepAngle = (sliceValue / totalCircleValue) * CIRCLE_SWEEP_DEGREES; // Calculate the sweep angle for the slice as a part of the entire pie + const sweepAngle = (sliceValue / totalCircleValue) * circleSweepDegrees; // Calculate the sweep angle for the slice as a part of the entire pie const endAngle = initialStartAngle + sweepAngle; // the sum of sweep + start startAngle += sweepAngle; // the next startAngle is the accumulation of each sweep @@ -70,6 +77,8 @@ export const PieChart = (props: PieChartProps) => { radius, center, innerRadius, + circleSweepDegrees, + _startAngle, ]); return data.map((slice, index) => { diff --git a/website/docs/polar/pie/pie-charts.md b/website/docs/polar/pie/pie-charts.md index ca60226f..3ef8cde6 100644 --- a/website/docs/polar/pie/pie-charts.md +++ b/website/docs/polar/pie/pie-charts.md @@ -62,6 +62,14 @@ A `number` or `string` (as a percentage) which turns the `Pie` chart into a `Don The `innerRadius` prop must be a `number` or a `string` like `50%`. ::: +### `circleSweepDegrees` + +A `number` which defines how many degrees of the chart should be drawn. The default is `360` which will draw a full circle. If you want to draw a partial circle, you can set this prop to a value between `0` and `360`. + +### `startAngle` + +A `number` which defines the starting angle of the chart. Changing this prop will rotate the chart. + ### `children` The `children` prop is a render function which maps through the data and whose sole argument is each individual `slice` of the pie, allowing you to customize each slice as needed. E.g. this slice will have all the data needed to render a `Pie.Slice />`.