import {Fragment, useEffect} from 'react'; import {Animated, View} from 'react-native'; import Svg, { Line, Polygon, Circle, Text as SvgText, Defs, RadialGradient, Stop, TextAnchor, AlignmentBaseline, TSpan, G, } from 'react-native-svg'; import {RadarChartProps, useRadarChart} from 'gifted-charts-core'; export const RadarChart = (props: RadarChartProps) => { const { data, dataSet, center, radius, chartSize, chartContainerProps, polarToCartesian, labels, labelConfigArray, labelsPositionOffset, dataLabelsConfigArray, maxValue, dataLabels, dataLabelsArray, gridSections, gridFill, fontSize, stroke, textAnchor, alignmentBaseline, fontWeight, fontFamily, dataLabelsPositionOffset, polygonStroke, polygonStrokeWidth, polygonStrokeDashArray, polygonFill, polygonGradientColor, polygonShowGradient, polygonOpacity, polygonGradientOpacity, polygonIsAnimated, polygonAnimationDuration, asterLinesStroke, asterLinesStrokeWidth, asterLinesStrokeDashArray, polygonPoints, initialPolygonPoints, polygonPointsArray, initialPolygonPointsArray, polygonConfigArray, angleStep, circular, hideGrid, hideLabels, hideAsterLines, getGridLevelProps, animateTogether, startAngle, } = useRadarChart(props); const initialPolygonPointsAr = initialPolygonPoints.split(' '); const finalPolygonPointsAr = polygonPoints.split(' '); const AnimatedPolygon = Animated.createAnimatedComponent(Polygon); const animatedValues = data.map(_ => new Animated.Value(0)); const animatedPaths = data.map((_, index) => animatedValues[index]?.interpolate({ inputRange: [0, 1], outputRange: [initialPolygonPointsAr[index], finalPolygonPointsAr[index]], }), ); useEffect(() => { if (dataSet?.length) return; animatedValues.forEach(animatedValue => Animated.timing(animatedValue, { toValue: 1, duration: polygonAnimationDuration, useNativeDriver: false, }).start(), ); }, [data]); /******************************************************************************************/ /********************* Animation handling for dataSet *********************/ let animatedValuesForSet: Animated.Value[][] = []; let animatedPathsForSet: any[] = []; if (dataSet?.length) { polygonConfigArray?.forEach((_, index) => { const set = dataSet[index]; const initialPolygonPointsAr = initialPolygonPointsArray[index].split(' '); const finalPolygonPointsAr = polygonPointsArray[index].split(' '); const animatedValues = set.map((_: any) => new Animated.Value(0)); animatedValuesForSet.push(animatedValues); const animatedPaths = set.map((i: any, ind: number) => animatedValues[ind].interpolate({ inputRange: [0, 1], outputRange: [initialPolygonPointsAr[ind], finalPolygonPointsAr[ind]], }), ); animatedPathsForSet.push(animatedPaths); }); } useEffect(() => { if (!dataSet?.length) return; animatedValuesForSet?.forEach((animatedValues, index) => { setTimeout( () => { animatedValues?.forEach(animatedValue => Animated.timing(animatedValue, { toValue: 1, duration: polygonAnimationDuration, useNativeDriver: false, }).start(), ); }, animateTogether ? 0 : index * polygonAnimationDuration, ); }); }); /******************************************************************************************/ /******************************************************************************************/ return ( {polygonConfigArray?.length ? ( polygonConfigArray.map((polygonConfigItem, index) => { const {fill, gradientColor, opacity, gradientOpacity} = polygonConfigItem; return ( ); }) ) : polygonShowGradient ? ( ) : null} {hideGrid ? null : gridSections.map((l, ind) => { const { level, gridGradientColorLocal, gridFillColorLocal, gridOpacityLocal, gridGradientOpacityLocal, gridStrokeLocal, gridStrokeWidthLocal, gridShowGradientLocal, gridStrokeDashArrayLocal, levelPolygonPoints, r, } = getGridLevelProps(l, ind); return ( {circular ? ( ) : ( )} ); })} {/* Draw the data polygon */} {dataSet ? ( polygonConfigArray?.map((item, index) => { const polygonPoints = polygonPointsArray[index]; const animatedPolygonPoints = animatedPathsForSet[index]; const { stroke, strokeWidth, strokeDashArray, fill, showGradient, opacity, isAnimated = polygonIsAnimated, } = item; return ( ); }) ) : ( )} {dataSet?.length && dataLabelsArray?.length ? ( dataLabelsArray?.map((labels, index) => { const dataItem = dataSet[index]; return labels?.map((label, labelIndex) => { const {x, y} = polarToCartesian( labelIndex * angleStep, dataItem[labelIndex] + dataLabelsPositionOffset, ); const { fontSize: dataLabelsFontSize, stroke: dataLabelsColor, textAnchor: dataLabelsTextAnchor, alignmentBaseline: dataLabelsAlignmentBaseline, fontWeight: dataLabelsFontWeight, fontFamily: dataLabelsFontFamily, } = dataLabelsConfigArray?.[labelIndex] ?? {}; return ( {label} ); }); }) ) : dataLabels?.length ? ( {dataLabels.map((label, index) => { const {x, y} = polarToCartesian( index * angleStep, data[index] + dataLabelsPositionOffset, ); const { fontSize: dataLabelsFontSize, stroke: dataLabelsColor, textAnchor: dataLabelsTextAnchor, alignmentBaseline: dataLabelsAlignmentBaseline, fontWeight: dataLabelsFontWeight, fontFamily: dataLabelsFontFamily, } = dataLabelsConfigArray?.[index] ?? {}; return ( {label} ); })} ) : null} {/* Draw lines from center to the points (axes) */} {hideAsterLines ? null : labels.map((_, index) => { const angle = index * angleStep + startAngle; const {x, y} = polarToCartesian(angle, maxValue); return ( ); })} {/* Draw category labels */} {hideLabels ? null : labels.map((category, index) => { const angle = index * angleStep + startAngle; const {x, y} = polarToCartesian( angle, maxValue + labelsPositionOffset, ); // Offset for label position const fontSizeLocal = labelConfigArray?.[index]?.fontSize ?? fontSize; const fontWeightLocal = labelConfigArray?.[index]?.fontWeight ?? fontWeight; const fontFamilyLocal = labelConfigArray?.[index]?.fontFamily ?? fontFamily; const colorLocal = labelConfigArray?.[index]?.stroke ?? stroke; const textAnchorLocal = labelConfigArray?.[index]?.textAnchor ?? textAnchor; const alignmentBaselineLocal = labelConfigArray?.[index]?.alignmentBaseline ?? alignmentBaseline; return ( {category.split('\n').map((line, idx) => ( {line} ))} ); })} ); };