Skip to content

Commit 51127ed

Browse files
add donut
1 parent b27b9d4 commit 51127ed

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

src/components/chart-elements/DonutChart/DonutChart.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { parseData, parseLabelInput } from "./inputParser";
1818
import type { EventProps } from "components/chart-elements/common";
1919
import { CustomTooltipProps } from "components/chart-elements/common/CustomTooltipProps";
2020
import type BaseAnimationTimingProps from "../common/BaseAnimationTimingProps";
21+
import { PieLabel } from "recharts/types/polar/Pie";
2122

2223
type DonutChartVariant = "donut" | "pie";
2324

@@ -36,6 +37,7 @@ export interface DonutChartProps extends BaseAnimationTimingProps {
3637
className?: string;
3738
onValueChange?: (value: EventProps) => void;
3839
customTooltip?: React.ComponentType<CustomTooltipProps>;
40+
renderLabel?: PieLabel;
3941
}
4042

4143
const renderInactiveShape = (props: any) => {
@@ -89,6 +91,7 @@ const DonutChart = React.forwardRef<HTMLDivElement, DonutChartProps>((props, ref
8991
noDataText,
9092
onValueChange,
9193
customTooltip,
94+
renderLabel,
9295
className,
9396
...other
9497
} = props;
@@ -177,6 +180,7 @@ const DonutChart = React.forwardRef<HTMLDivElement, DonutChartProps>((props, ref
177180
activeIndex={activeIndex}
178181
inactiveShape={renderInactiveShape}
179182
style={{ outline: "none" }}
183+
label={renderLabel}
180184
/>
181185
{/* {showTooltip ? (
182186
<Tooltip

src/stories/chart-elements/DonutChart.stories.tsx

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,56 @@ export const CustomTooltipSimple: Story = {
171171
},
172172
},
173173
};
174+
175+
export const DataLabelsSimple: Story = {
176+
args: {
177+
renderLabel: true,
178+
},
179+
};
180+
181+
export const DataLabelsObject: Story = {
182+
args: {
183+
renderLabel: { fill: "red", fontSize: 20 },
184+
},
185+
};
186+
187+
const RADIAN = Math.PI / 180;
188+
const renderCustomizedLabel = ({
189+
cx,
190+
cy,
191+
midAngle,
192+
innerRadius,
193+
outerRadius,
194+
percent,
195+
}: {
196+
cx: number;
197+
cy: number;
198+
midAngle: number;
199+
innerRadius: number;
200+
outerRadius: number;
201+
percent: number;
202+
}) => {
203+
const radius = innerRadius + 10 + (outerRadius - innerRadius) * 0.6;
204+
const x = cx + radius * Math.cos(-midAngle * RADIAN);
205+
const y = cy + radius * Math.sin(-midAngle * RADIAN);
206+
207+
return (
208+
<text
209+
x={x}
210+
y={y}
211+
fill="white"
212+
fontSize="9"
213+
textAnchor={x > cx ? "start" : "end"}
214+
dominantBaseline="auto"
215+
>
216+
{`${(percent * 100).toFixed(0)}%`}
217+
</text>
218+
);
219+
};
220+
221+
export const DataLabelsFunction: Story = {
222+
args: {
223+
renderLabel: renderCustomizedLabel,
224+
variant: "pie",
225+
},
226+
};

0 commit comments

Comments
 (0)