|
| 1 | +import React from 'react'; |
| 2 | +import {ColorValue, StyleSheet} from 'react-native'; |
| 3 | +import View from '../view'; |
| 4 | +import {SvgPackage} from '../../optionalDependencies'; |
| 5 | +import {Colors} from '../../style'; |
| 6 | +const {Svg, Path} = SvgPackage; |
| 7 | + |
| 8 | +export type PieSegmentProps = { |
| 9 | + /** |
| 10 | + * The percentage of pie the segment should cover |
| 11 | + */ |
| 12 | + percentage: number; |
| 13 | + /** |
| 14 | + * The radius of the containing pie |
| 15 | + */ |
| 16 | + radius: number; |
| 17 | + /** |
| 18 | + * The color of the segment |
| 19 | + */ |
| 20 | + color: string; |
| 21 | + /** |
| 22 | + * The start angle of the segment |
| 23 | + */ |
| 24 | + startAngle?: number; |
| 25 | + /** |
| 26 | + * The padding between the segments and the container of the pie. |
| 27 | + */ |
| 28 | + padding?: number; |
| 29 | + /** |
| 30 | + * The width of the divider between the segments |
| 31 | + */ |
| 32 | + dividerWidth?: number; |
| 33 | + /** |
| 34 | + * The color of the divider between the segments |
| 35 | + */ |
| 36 | + dividerColor?: ColorValue; |
| 37 | +}; |
| 38 | + |
| 39 | +const PieSegment = (props: PieSegmentProps) => { |
| 40 | + const { |
| 41 | + percentage, |
| 42 | + radius, |
| 43 | + color, |
| 44 | + startAngle = 0, |
| 45 | + padding = 0, |
| 46 | + dividerWidth = 4, |
| 47 | + dividerColor = Colors.$backgroundDefault |
| 48 | + } = props; |
| 49 | + |
| 50 | + const actualRadius = radius - padding; |
| 51 | + const centerXAndY = radius; |
| 52 | + const amountToCover = (percentage / 100) * 360; |
| 53 | + const angleFromTop = startAngle - 90; |
| 54 | + |
| 55 | + const startRad = (angleFromTop * Math.PI) / 180; |
| 56 | + const endRad = startRad + (amountToCover * Math.PI) / 180; |
| 57 | + |
| 58 | + const startX = centerXAndY + Math.cos(startRad) * actualRadius; |
| 59 | + const startY = centerXAndY + Math.sin(startRad) * actualRadius; |
| 60 | + const endX = centerXAndY + Math.cos(endRad) * actualRadius; |
| 61 | + const endY = centerXAndY + Math.sin(endRad) * actualRadius; |
| 62 | + |
| 63 | + const largeArcFlag = amountToCover > 180 ? 1 : 0; |
| 64 | + const sweepFlag = 1; |
| 65 | + |
| 66 | + const arcPath = ` |
| 67 | + M ${centerXAndY} ${centerXAndY} |
| 68 | + L ${startX} ${startY} |
| 69 | + A ${actualRadius} ${actualRadius} 0 ${largeArcFlag} ${sweepFlag} ${endX} ${endY} |
| 70 | + Z |
| 71 | + `; |
| 72 | + const startBorderLine = `M ${centerXAndY} ${centerXAndY} L ${startX} ${startY}`; |
| 73 | + const endBorderLine = `M ${centerXAndY} ${centerXAndY} L ${endX} ${endY}`; |
| 74 | + |
| 75 | + const arc = <Path d={arcPath} fill={color}/>; |
| 76 | + const borders = ( |
| 77 | + <Path |
| 78 | + d={`${startBorderLine} ${endBorderLine}`} |
| 79 | + fill="none" |
| 80 | + stroke={dividerColor} |
| 81 | + strokeWidth={dividerWidth / 2} |
| 82 | + strokeLinejoin="round" |
| 83 | + /> |
| 84 | + ); |
| 85 | + const totalSize = radius * 2 + padding; |
| 86 | + |
| 87 | + return ( |
| 88 | + <View style={styles.container}> |
| 89 | + <Svg width={totalSize} height={totalSize} viewBox={`0 0 ${totalSize} ${totalSize}`} style={styles.svg}> |
| 90 | + {arc} |
| 91 | + {borders} |
| 92 | + </Svg> |
| 93 | + </View> |
| 94 | + ); |
| 95 | +}; |
| 96 | + |
| 97 | +export default PieSegment; |
| 98 | + |
| 99 | +const styles = StyleSheet.create({ |
| 100 | + container: { |
| 101 | + position: 'absolute' |
| 102 | + }, |
| 103 | + svg: { |
| 104 | + position: 'absolute' |
| 105 | + } |
| 106 | +}); |
0 commit comments