Skip to content

Commit 4f9df3a

Browse files
authored
Convert Pie Chart example to tsx (#367)
1 parent 339b863 commit 4f9df3a

14 files changed

+406
-363
lines changed

src/docs/exampleComponents/PieChart/CustomActiveShapePieChart.js

Lines changed: 0 additions & 78 deletions
This file was deleted.
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import { Pie, PieChart, ResponsiveContainer, Sector, SectorProps } from 'recharts';
2+
3+
type Coordinate = {
4+
x: number;
5+
y: number;
6+
};
7+
8+
type PieSectorData = {
9+
percent?: number;
10+
name?: string | number;
11+
midAngle?: number;
12+
middleRadius?: number;
13+
tooltipPosition?: Coordinate;
14+
value?: number;
15+
paddingAngle?: number;
16+
dataKey?: string;
17+
payload?: any;
18+
};
19+
20+
type PieSectorDataItem = React.SVGProps<SVGPathElement> & Partial<SectorProps> & PieSectorData;
21+
22+
const data = [
23+
{ name: 'Group A', value: 400 },
24+
{ name: 'Group B', value: 300 },
25+
{ name: 'Group C', value: 300 },
26+
{ name: 'Group D', value: 200 },
27+
];
28+
29+
const renderActiveShape = ({
30+
cx,
31+
cy,
32+
midAngle,
33+
innerRadius,
34+
outerRadius,
35+
startAngle,
36+
endAngle,
37+
fill,
38+
payload,
39+
percent,
40+
value,
41+
}: PieSectorDataItem) => {
42+
const RADIAN = Math.PI / 180;
43+
const sin = Math.sin(-RADIAN * (midAngle ?? 1));
44+
const cos = Math.cos(-RADIAN * (midAngle ?? 1));
45+
const sx = (cx ?? 0) + ((outerRadius ?? 0) + 10) * cos;
46+
const sy = (cy ?? 0) + ((outerRadius ?? 0) + 10) * sin;
47+
const mx = (cx ?? 0) + ((outerRadius ?? 0) + 30) * cos;
48+
const my = (cy ?? 0) + ((outerRadius ?? 0) + 30) * sin;
49+
const ex = mx + (cos >= 0 ? 1 : -1) * 22;
50+
const ey = my;
51+
const textAnchor = cos >= 0 ? 'start' : 'end';
52+
53+
return (
54+
<g>
55+
<text x={cx} y={cy} dy={8} textAnchor="middle" fill={fill}>
56+
{payload.name}
57+
</text>
58+
<Sector
59+
cx={cx}
60+
cy={cy}
61+
innerRadius={innerRadius}
62+
outerRadius={outerRadius}
63+
startAngle={startAngle}
64+
endAngle={endAngle}
65+
fill={fill}
66+
/>
67+
<Sector
68+
cx={cx}
69+
cy={cy}
70+
startAngle={startAngle}
71+
endAngle={endAngle}
72+
innerRadius={(outerRadius ?? 0) + 6}
73+
outerRadius={(outerRadius ?? 0) + 10}
74+
fill={fill}
75+
/>
76+
<path d={`M${sx},${sy}L${mx},${my}L${ex},${ey}`} stroke={fill} fill="none" />
77+
<circle cx={ex} cy={ey} r={2} fill={fill} stroke="none" />
78+
<text x={ex + (cos >= 0 ? 1 : -1) * 12} y={ey} textAnchor={textAnchor} fill="#333">{`PV ${value}`}</text>
79+
<text x={ex + (cos >= 0 ? 1 : -1) * 12} y={ey} dy={18} textAnchor={textAnchor} fill="#999">
80+
{`(Rate ${((percent ?? 1) * 100).toFixed(2)}%)`}
81+
</text>
82+
</g>
83+
);
84+
};
85+
86+
export default function Example() {
87+
return (
88+
<ResponsiveContainer width="100%" height="100%">
89+
<PieChart width={400} height={400}>
90+
<Pie
91+
activeShape={renderActiveShape}
92+
data={data}
93+
cx="50%"
94+
cy="50%"
95+
innerRadius={60}
96+
outerRadius={80}
97+
fill="#8884d8"
98+
dataKey="value"
99+
/>
100+
</PieChart>
101+
</ResponsiveContainer>
102+
);
103+
}

src/docs/exampleComponents/PieChart/PieChartWithCustomizedLabel.js

Lines changed: 0 additions & 51 deletions
This file was deleted.
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { Cell, Pie, PieChart, ResponsiveContainer } from 'recharts';
2+
3+
type TooltipPayload = ReadonlyArray<any>;
4+
5+
type Coordinate = {
6+
x: number;
7+
y: number;
8+
};
9+
10+
type PieSectorData = {
11+
percent?: number;
12+
name?: string | number;
13+
midAngle?: number;
14+
middleRadius?: number;
15+
tooltipPosition?: Coordinate;
16+
value?: number;
17+
paddingAngle?: number;
18+
dataKey?: string;
19+
payload?: any;
20+
tooltipPayload?: ReadonlyArray<TooltipPayload>;
21+
};
22+
23+
type GeometrySector = {
24+
cx: number;
25+
cy: number;
26+
innerRadius: number;
27+
outerRadius: number;
28+
startAngle: number;
29+
endAngle: number;
30+
};
31+
32+
type PieLabelProps = PieSectorData &
33+
GeometrySector & {
34+
tooltipPayload?: any;
35+
};
36+
37+
const data = [
38+
{ name: 'Group A', value: 400 },
39+
{ name: 'Group B', value: 300 },
40+
{ name: 'Group C', value: 300 },
41+
{ name: 'Group D', value: 200 },
42+
];
43+
44+
const RADIAN = Math.PI / 180;
45+
const COLORS = ['#0088FE', '#00C49F', '#FFBB28', '#FF8042'];
46+
47+
const renderCustomizedLabel = ({ cx, cy, midAngle, innerRadius, outerRadius, percent }: PieLabelProps) => {
48+
const radius = innerRadius + (outerRadius - innerRadius) * 0.5;
49+
const x = cx + radius * Math.cos(-(midAngle ?? 0) * RADIAN);
50+
const y = cy + radius * Math.sin(-(midAngle ?? 0) * RADIAN);
51+
52+
return (
53+
<text x={x} y={y} fill="white" textAnchor={x > cx ? 'start' : 'end'} dominantBaseline="central">
54+
{`${((percent ?? 1) * 100).toFixed(0)}%`}
55+
</text>
56+
);
57+
};
58+
59+
export default function Example() {
60+
return (
61+
<ResponsiveContainer width="100%" height="100%">
62+
<PieChart width={400} height={400}>
63+
<Pie
64+
data={data}
65+
cx="50%"
66+
cy="50%"
67+
labelLine={false}
68+
label={renderCustomizedLabel}
69+
outerRadius={80}
70+
fill="#8884d8"
71+
dataKey="value"
72+
>
73+
{data.map((entry, index) => (
74+
<Cell key={`cell-${entry.name}`} fill={COLORS[index % COLORS.length]} />
75+
))}
76+
</Pie>
77+
</PieChart>
78+
</ResponsiveContainer>
79+
);
80+
}

src/docs/exampleComponents/PieChart/PieChartWithNeedle.js

Lines changed: 0 additions & 66 deletions
This file was deleted.

0 commit comments

Comments
 (0)