Skip to content

Instantly share code, notes, and snippets.

@ozcanzaferayan
Last active January 26, 2024 13:39
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ozcanzaferayan/7c86ffe5fd033c9ae80b92eb186530d6 to your computer and use it in GitHub Desktop.
Save ozcanzaferayan/7c86ffe5fd033c9ae80b92eb186530d6 to your computer and use it in GitHub Desktop.
React Native Donut Chart with react-native-svg
import React from 'react';
import { View } from 'react-native';
export const App: React.FC = () => {
return (
<View style={{flexDirection: 'row', justifyContent:'center'}}>
<DonutChart radius={15} percentage={25} width={100}/>
<DonutChart radius={15} percentage={65} width={100}/>
<DonutChart radius={15} percentage={70} width={100}/>
</View>
);
}
export default App;
const colors = {
transparent: 'transparent',
background: '#fff',
textButton: '#ffffff',
textFaded: '#666666',
text: 'rgba(217,217,217,1.00)',
primary: '#1565C0',
hairline: 'rgb(47, 51, 54)',
donutGreyRing: '#e5e5e5',
}
export default colors;
import React from 'react';
import Svg, { Path, SvgProps, Circle } from 'react-native-svg';
import colors from '../../res/styles/colors';
import spacing from '../../res/styles/spacing';
interface DonutChartProps extends SvgProps {
radius: number,
percentage : number,
width : number,
}
const DonutChart: React.FC<DonutChartProps> = props => {
const radius = props.radius;
const perimeter = 2 * Math.PI * radius;
const percentage = props.percentage;
const strokeDashOffset = perimeter - (perimeter * percentage / 100);
return (
<Svg viewBox="0 0 42 42" style={{ overflow: 'visible', transform: [{ rotateZ: '-90deg' }], width: props.width}}>
<Circle cx="21" cy="21" r={radius} fill={colors.background} />
<Circle cx="21" cy="21" r={radius} fill={colors.transparent} stroke={colors.donutGreyRing} stroke-width={spacing.donutStrokeWidth} />
<Circle cx="21" cy="21" r={radius} fill={colors.transparent} stroke={colors.primary} stroke-width={spacing.donutStrokeWidth}
strokeDasharray={perimeter} strokeDashoffset={strokeDashOffset} />
</Svg>
);
}
export default DonutChart;
const spacing = {
textButton: 14,
textMenu: 18,
icon: 24,
iconTweet: 20,
iconSearch: 17.5,
iconSettings: 21,
iconCaret: 14,
iconTweetAction: 17.5,
iconTweetBox: 21,
radius: 9999,
heightTweetButton: 44,
hairline: 1,
donutStrokeWidth: 8,
}
export default spacing;
@mahmut-gundogdu
Copy link

thank you. You save my times. there is a typo in 'DonuChartProps' in DonutChart.tsx

@ozcanzaferayan
Copy link
Author

Thank you Mahmut. I fixed it :)

@arifemir
Copy link

arifemir commented May 4, 2021

thanks zafer abi 💯

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment