import React, {useEffect} from 'react'; import { PieChartPropsType, pieColors, usePiePro, } from 'gifted-charts-core'; import { Defs, Path, Stop, Svg, Text as SvgText, RadialGradient, } from 'react-native-svg'; import {Animated, View} from 'react-native'; export const PieChartPro = (props: PieChartPropsType) => { const { radius, total, donut, strokeWidth, maxStrokeWidth, animationDuration, initial, dInitial, dFinal, getStartCaps, getEndCaps, getTextCoordinates, height, heightFactor, svgProps, } = usePiePro(props); const { data, curvedStartEdges, curvedEndEdges, edgesRadius = 0, showGradient, ring, centerLabelComponent, strokeDashArray, semiCircle, } = props; let {isAnimated} = props; if (!props.semiCircle && data.some(dataItem => dataItem.value > total / 2)) { // if we have an obtuse arc, we cant animate isAnimated = false; } const AnimatedPath = Animated.createAnimatedComponent(Path); const AnimatedText = Animated.createAnimatedComponent(SvgText); const animatedValues = data.map(i => new Animated.Value(0)); const animatedOpacityValue = new Animated.Value(0); const animatedPaths = data.map((item, index) => animatedValues[index]?.interpolate({ inputRange: [0, 1], outputRange: [dInitial[index], dFinal[index]], }), ); const animatedOpacity = animatedOpacityValue.interpolate({ inputRange: [0, 1], outputRange: [0, 1], }); useEffect(() => { Animated.timing(animatedOpacityValue, { toValue: 1, duration: 10, useNativeDriver: false, delay: animationDuration, }).start(); animatedValues.forEach(animatedValue => Animated.timing(animatedValue, { toValue: 1, duration: animationDuration, useNativeDriver: false, }).start(), ); }, [data]); const adjustHeight = height * heightFactor; const rnSvgProps = semiCircle ? {} : svgProps; return ( {centerLabelComponent ? centerLabelComponent() : null} {total ? ( <> {data.map((item, index) => { return ( ); })} {data.map((item, index) => { const borderWidth = item.strokeWidth ?? strokeWidth; const borderColor = item.strokeColor ?? props.strokeColor ?? (borderWidth ? 'black' : 'transparent'); const strokeDashArrayLocal = item.strokeDashArray ?? strokeDashArray; return ( ); })} {donut ? data.map((item, index) => { if ( curvedStartEdges || edgesRadius || item.isStartEdgeCurved || item.startEdgeRadius ) return ( ); return null; }) : null} {donut ? data.map((item, index) => { if ( curvedEndEdges || edgesRadius || item.isEndEdgeCurved || item.endEdgeRadius ) return ( ); return null; }) : null} {data.map((item, index) => { const {x, y} = getTextCoordinates(index, item.labelPosition); return ( { item.onLabelPress ? item.onLabelPress() : props.onLabelPress ? props.onLabelPress(item, index) : item.onPress ? item.onPress() : props.onPress?.(item, index); }}> {item.text || (props.showValuesAsLabels ? item.value + '' : '')} ); })} ) : null} ); };