Created
August 1, 2019 14:12
-
-
Save lucalanca/342d4cefa4976a580bca455c2547ddeb to your computer and use it in GitHub Desktop.
SVG Donut
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as React from "react"; | |
interface DonutProps { | |
segments: { | |
color: string; | |
percentage: number; | |
}[]; | |
thickness?: number; | |
} | |
const INITIAL_POINT = 25; | |
export const SvgDonut: React.FC<DonutProps> = ({ segments, thickness = 3, ...rest }) => { | |
let cumulativeLength = 0; | |
return ( | |
<svg width="100%" height="100%" viewBox="0 0 42 42" {...rest}> | |
{segments.map(({ color, percentage }, index) => { | |
cumulativeLength += index === 0 | |
? 0 | |
: segments[index - 1].percentage; | |
return ( | |
<circle | |
key={index} | |
cx="21" | |
cy="21" | |
r='16' | |
fill="transparent" | |
stroke={color} | |
strokeWidth={thickness} | |
strokeDasharray={`${percentage} ${100 - percentage}`} | |
strokeDashoffset={100 - cumulativeLength + INITIAL_POINT} | |
/> | |
) | |
})} | |
</svg> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment