Replies: 1 comment 2 replies
-
|
Creating an overlapping or layered effect in Recharts (especially within shadcn/ui chart wrappers) is a common design request that can't be achieved with standard props like The "proper" way to solve this is by using a Custom Shape for the Pie sectors. By creating a custom component that renders a The Solution: Custom Overlapping SectorYou need to define a 1. Implementation Codeimport { Pie, PieChart, Sector, Cell, ResponsiveContainer } from "recharts"
const renderOverlappingSector = (props: any) => {
const { cx, cy, innerRadius, outerRadius, startAngle, endAngle, fill, payload } = props
// The "Overlap" Logic:
// We extend the endAngle by a few degrees to create the overlap.
// We also add a cornerRadius to get those smooth rounded ends.
const OVERLAP_DEGREE = 15;
return (
<g>
{/* Adding a drop shadow filter makes the "layering" pop */}
<defs>
<filter id="shadow" x="-20%" y="-20%" width="140%" height="140%">
<feDropShadow dx="2" dy="2" stdDeviation="3" floodOpacity="0.3" />
</filter>
</defs>
<Sector
{...props}
endAngle={endAngle + OVERLAP_DEGREE} // This creates the overlap
cornerRadius={innerRadius} // Creates the rounded look
stroke={fill}
strokeWidth={2}
style={{ filter: "url(#shadow)" }} // Apply the layered shadow
/>
</g>
)
}
// Inside your main Chart component
export function OverlappingDonut({ data }) {
return (
<ResponsiveContainer width="100%" height={300}>
<PieChart>
<Pie
data={data}
cx="50%"
cy="50%"
innerRadius={60}
outerRadius={80}
dataKey="value"
shape={renderOverlappingSector} // Use the custom shape here
stroke="none"
>
{data.map((entry, index) => (
<Cell key={`cell-${index}`} fill={entry.fill} />
))}
</Pie>
</PieChart>
</ResponsiveContainer>
)
}Since you are using shadcn/ui chart wrappers, you can simply add the If this custom sector approach helps you achieve that layered donut look, please consider marking this as the accepted answer to help other UI developers in the community! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi everyone
I am using Recharts along with shadcn/ui chart wrappers to create a pie chart and am trying to achieve the effect of overlapping/layering segments.
Visually, the goal looks something like this:

each segment of the pie chart slightly overlaps the next one.
the overlap is visible at the segment boundaries
I tried using prop paddingAngle with a negative value, but it doesn't give the proper result.
Is there an official or recommended way in Recharts to implement overlapping/layered pie chart segments?
Any tips, examples, or hints would be very helpful.
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions