Skip to content

Commit

Permalink
PieChart: iterate over component. Introduce size, thickness and `…
Browse files Browse the repository at this point in the history
…padding` property (#40993)

* replace width and height with size
This is a Pie chart. Size is more accurate

* introduce padding prop

* changelog

* tweak legendOrientation story control

* change thickness arg story order

* update thickness value in storieps

* computer outer, inner, and thickness props
  • Loading branch information
retrofox authored Jan 13, 2025
1 parent 2b7feac commit d34f9d4
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: changed

PieChart: iterate a bit over component API
33 changes: 28 additions & 5 deletions projects/js-packages/charts/src/components/pie-chart/pie-chart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,28 @@ import type { BaseChartProps, DataPointPercentage } from '../../types';

// TODO: add animation

interface PieChartProps extends BaseChartProps< DataPointPercentage[] > {
type OmitBaseChartProps = Omit< BaseChartProps< DataPointPercentage[] >, 'width' | 'height' >;

interface PieChartProps extends OmitBaseChartProps {
/**
* Inner radius in pixels. If > 0, creates a donut chart. Defaults to 0.
*/
innerRadius?: number;

/**
* Size of the chart in pixels
*/
size?: number;

/**
* Add padding to the chart
*/
padding?: number;

/**
* Thickness of the pie chart. A value between 0 and 1
*/
thickness?: number;
}

/**
Expand All @@ -27,20 +44,23 @@ interface PieChartProps extends BaseChartProps< DataPointPercentage[] > {
*/
const PieChart = ( {
data,
width = 500, //TODO: replace when making the components responsive
height = 500, //TODO: replace when making the components responsive
size = 500, //TODO: replace when making the components responsive
thickness = 1,
withTooltips = false,
innerRadius = 0,
className,
showLegend,
legendOrientation,
padding = 20,
}: PieChartProps ) => {
const providerTheme = useChartTheme();
const { onMouseMove, onMouseLeave, tooltipOpen, tooltipData, tooltipLeft, tooltipTop } =
useChartMouseHandler( {
withTooltips,
} );

const width = size;
const height = size;

// Calculate radius based on width/height
const radius = Math.min( width, height ) / 2;
const centerX = width / 2;
Expand All @@ -52,6 +72,9 @@ const PieChart = ( {
index,
} ) );

const outerRadius = radius - padding;
const innerRadius = outerRadius * ( 1 - thickness );

const accessors = {
value: ( d: DataPointPercentage ) => d.value,
// Use the color property from the data object as a last resort. The theme provides colours by default.
Expand All @@ -73,7 +96,7 @@ const PieChart = ( {
<Pie< DataPointPercentage & { index: number } >
data={ dataWithIndex }
pieValue={ accessors.value }
outerRadius={ radius - 20 } // Leave space for labels/tooltips
outerRadius={ outerRadius }
innerRadius={ innerRadius }
>
{ pie => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,34 @@ export default {
},
defaultValue: undefined,
},
size: {
control: {
type: 'range',
min: 100,
max: 800,
step: 1,
},
},
thickness: {
control: {
type: 'range',
min: 0,
max: 1,
step: 0.01,
},
},
padding: {
control: {
type: 'range',
min: 0,
max: 100,
step: 1,
},
},
legendOrientation: {
control: 'radio',
options: [ 'horizontal', 'vertical' ],
},
},
decorators: [
( Story, { args } ) => (
Expand All @@ -41,12 +69,12 @@ export default {

export const Default: StoryType = {
args: {
width: 400,
height: 400,
size: 400,
thickness: 1,
padding: 20,
withTooltips: false,
data,
theme: 'default',
innerRadius: 0,
showLegend: false,
legendOrientation: 'horizontal',
},
Expand All @@ -71,12 +99,12 @@ export const WithVerticalLegend: StoryType = {
export const Doughnut: StoryType = {
args: {
...Default.args,
innerRadius: 80,
thickness: 0.5,
},
parameters: {
docs: {
description: {
story: 'Doughnut chart variant with inner radius of 80px.',
story: 'Doughnut chart variant with the thickness set to 0.5 (50%).',
},
},
},
Expand All @@ -100,7 +128,6 @@ export const WithTooltipsDoughnut: StoryType = {
args: {
...Default.args,
withTooltips: true,
innerRadius: 100,
},
parameters: {
docs: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ const Template = args => <PieSemiCircleChart { ...args } />;
export const Default = Template.bind( {} );
Default.args = {
width: 500,
thickness: 0.4,
data,
label: 'OS',
note: 'Windows +10%',
thickness: 0.4,
clockwise: true,
showLegend: false,
legendOrientation: 'horizontal',
Expand Down

0 comments on commit d34f9d4

Please sign in to comment.