import {useEffect, useRef} from 'react'; import {View, Text, Animated} from 'react-native'; import Svg, {Defs, Rect} from 'react-native-svg'; import Cap from '../Components/BarSpecificComponents/cap'; import LinearGradient from '../Components/common/LinearGradient'; import {Animated2DWithGradientPropsType} from 'gifted-charts-core'; const Animated2DWithGradient = (props: Animated2DWithGradientPropsType) => { const { barBackgroundPattern, patternId, barWidth: bWidth, barStyle, item, index, opacity, animationDuration, noGradient, noAnimation, barInnerComponent, intactTopLabel, showValuesAsTopLabel, topLabelContainerStyle, bottomLabelContainerStyle, topLabelTextStyle, bottomLabelTextStyle, commonStyleForBar, barStyleWithBackground, yAxisOffset, height, barMarginBottom = 0, isCandleStickChart, showValuesAsBottomLabel, } = props; const barWidth = item.barWidth ?? bWidth; // setting width in state for animation purpose const topLabelPosition = (item.barWidth || barWidth || 30) * -1; const bottomLabelPosition = height - barMarginBottom; const animatedHeight = useRef(new Animated.Value(0)).current; // initial height = 0 const animatedLabelHeight = useRef( new Animated.Value(height + topLabelPosition), ).current; const elevate = () => { Animated.parallel([ Animated.timing(animatedHeight, { toValue: height - (isCandleStickChart ? barMarginBottom : 0), duration: animationDuration, useNativeDriver: false, }), Animated.timing(animatedLabelHeight, { toValue: topLabelPosition, duration: animationDuration, useNativeDriver: false, }), ]).start(); }; useEffect(() => { if (!noAnimation) { elevate(); } }, []); return ( <> {noGradient ? ( {props.cappedBars && item.value ? ( ) : null} ) : ( {props.cappedBars && ( )} )} {(item.barBackgroundPattern || barBackgroundPattern) && ( {item.barBackgroundPattern ? item.barBackgroundPattern() : barBackgroundPattern?.()} )} {barInnerComponent ? ( {barInnerComponent(item, index)} ) : null} {item.topLabelComponent || showValuesAsTopLabel ? ( {showValuesAsTopLabel ? ( {item.value + yAxisOffset} ) : ( item.topLabelComponent?.() )} ) : null} {isCandleStickChart && (item.bottomLabelComponent || showValuesAsBottomLabel) ? ( {showValuesAsBottomLabel ? ( {(item.lowerValue ?? 0) + yAxisOffset} ) : ( item.bottomLabelComponent?.() )} ) : null} ); }; export default Animated2DWithGradient;