DonutChart (React Native + react-native-svg
)
#2772
harun-alrosyid
started this conversation in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
DonutChart (React Native +
react-native-svg
)A lightweight, reusable donut chart for React Native that supports multiple custom colors and a custom center component. Built purely with
react-native-svg
.Quick Start
Prerequisites
react-native-svg
Installation
Loook component here:
DonutChart.tsx
Types
Props
segments
Segment[]
[{ value: 40, color: '#F23C6D' }, ...]
. Total values must be > 0.radius
number
100
strokeWidth
number
30
startAngle
number
-90
0°
points to 3 o’clock. Use-90
to start at the top (12 o’clock).gapDegrees
number
2
0
for no gaps.trackColor
string
undefined
centerComponent
React.ReactNode
undefined
roundedCaps
boolean
false
"round"
) or square ("butt"
) slice endings. Rounded ends make gaps look visually wider.Behavior notes
sweep = percentage * 360 - gapDegrees
roundedCaps
+gapDegrees
can make gaps appear larger due to the rounded stroke ends.(radius * 2 + strokeWidth)
for both width and height.Usage
Tips & Best Practices
startAngle={-90}
.gapDegrees={2–8}
usually looks good.trackColor
(e.g., light gray/white) so your primary segments stand out.roundedCaps
, you may want to slightly reducegapDegrees
to compensate for the perceived wider gap.FAQ
Where do I control the gap between slices?
Use the
gapDegrees
prop. It’s measured in degrees and applied at every slice junction.How do I render custom content in the middle?
Pass any React node to
centerComponent
(texts, icons, totals, legend, etc.). It is absolutely centered.Can I animate it?
Yes wrap the values/angles in Reanimated or drive
strokeDasharray
changes over time. Animation isn’t included by default to keep the core small, but the component is compatible with common RN animation approaches.Troubleshooting
value > 0
and the total acrosssegments
is > 0.strokeWidth
less than or equal toradius
.gapDegrees
and/or setroundedCaps={false}
.More Detail
Beta Was this translation helpful? Give feedback.
All reactions