import React, {Fragment, useCallback, useEffect, useMemo, useRef} from 'react'; import {View, Animated, Easing, Text} from 'react-native'; import {styles} from './styles'; import Svg, { Path, LinearGradient, Stop, Circle, Rect, Text as CanvasText, } from 'react-native-svg'; import BarAndLineChartsWrapper from '../Components/BarAndLineChartsWrapper'; import { LineChartBicolorPropsType, bicolorLineDataItem, useLineChartBiColor, } from 'gifted-charts-core'; import {screenWidth} from '../utils'; export const LineChartBicolor = (props: LineChartBicolorPropsType) => { const scrollRef = useRef(null); // const heightValue = useMemo(() => new Animated.Value(0), []); const widthValue = useMemo(() => new Animated.Value(0), []); const opacityValue = useMemo(() => new Animated.Value(0), []); const { pointsArray, fillPointsArray, selectedIndex, setSelectedIndex, containerHeight, data, labelsExtraHeight, animationDuration, startIndex1, endIndex1, initialSpacing, thickness, spacing, xAxisThickness, dataPointsHeight1, dataPointsWidth1, dataPointsRadius1, dataPointsColor1, dataPointsShape1, areaChart, textFontSize1, textColor1, totalWidth, maxValue, extendedContainerHeight, getX, getY, stepHeight, noOfSectionsBelowXAxis, thickness1, zIndex, strokeDashArray1, rotateLabel, isAnimated, hideDataPoints1, color, colorNegative, startFillColor, endFillColor, startOpacity, endOpacity, startFillColorNegative, endFillColorNegative, startOpacityNegative, endOpacityNegative, gradientDirection, xAxisTextNumberOfLines, focusEnabled, showDataPointOnFocus, showStripOnFocus, showTextOnFocus, stripHeight, stripWidth, stripColor, stripOpacity, unFocusOnPressOut, delayBeforeUnFocus, barAndLineChartsWrapperProps, allowFontScaling, } = useLineChartBiColor({ ...props, parentWidth: props.parentWidth ?? screenWidth, }); const labelsAppear = useCallback(() => { opacityValue.setValue(0); Animated.timing(opacityValue, { toValue: 1, duration: 500, easing: Easing.ease, useNativeDriver: false, }).start(); }, [opacityValue]); const appearingOpacity = opacityValue.interpolate({ inputRange: [0, 1], outputRange: [0, 1], }); const decreaseWidth = useCallback(() => { widthValue.setValue(0); Animated.timing(widthValue, { toValue: 1, duration: animationDuration, easing: Easing.linear, useNativeDriver: false, }).start(); }, [animationDuration, widthValue]); useEffect(() => { decreaseWidth(); labelsAppear(); }, [animationDuration, decreaseWidth, labelsAppear]); const renderLabel = ( index: number, label: string, labelTextStyle: any, labelComponent?: Function, ) => { return ( {labelComponent ? ( labelComponent() ) : ( {label || ''} )} ); }; const renderAnimatedLabel = ( index: number, label: string, labelTextStyle: any, labelComponent?: Function, ) => { return ( {labelComponent ? ( labelComponent() ) : ( {label || ''} )} ); }; const animatedWidth = widthValue.interpolate({ inputRange: [0, 1], outputRange: [0, totalWidth], }); const onStripPress = (item: any, index: number) => { setSelectedIndex(index); if (props.onFocus) { props.onFocus(item, index); } }; const renderDataPoints = ( dataForRender: any, dataPtsShape: any, dataPtsWidth: any, dataPtsHeight: any, dataPtsColor: any, dataPtsRadius: any, textColor: any, textFontSize: any, startIndex: number, endIndex: number, ) => { return dataForRender.map((item: bicolorLineDataItem, index: number) => { if (index < startIndex || index > endIndex) return null; if (item.hideDataPoint) { return null; } let dataPointsShape, dataPointsWidth, dataPointsHeight, dataPointsColor, dataPointsRadius, text, customDataPoint, dataPointLabelComponent; if (index === selectedIndex) { dataPointsShape = item.focusedDataPointShape || props.focusedDataPointShape || item.dataPointShape || dataPtsShape; dataPointsWidth = item.focusedDataPointWidth || props.focusedDataPointWidth || item.dataPointWidth || dataPtsWidth; dataPointsHeight = item.focusedDataPointHeight || props.focusedDataPointHeight || item.dataPointHeight || dataPtsHeight; dataPointsColor = item.focusedDataPointColor || props.focusedDataPointColor || 'orange'; dataPointsRadius = item.focusedDataPointRadius || props.focusedDataPointRadius || item.dataPointRadius || dataPtsRadius; if (showTextOnFocus) { text = item.dataPointText; } customDataPoint = item.focusedCustomDataPoint || props.focusedCustomDataPoint || item.customDataPoint || props.customDataPoint; dataPointLabelComponent = item.focusedDataPointLabelComponent || item.dataPointLabelComponent; } else { dataPointsShape = item.dataPointShape || dataPtsShape; dataPointsWidth = item.dataPointWidth || dataPtsWidth; dataPointsHeight = item.dataPointHeight || dataPtsHeight; dataPointsColor = item.dataPointColor || dataPtsColor; dataPointsRadius = item.dataPointRadius || dataPtsRadius; if (showTextOnFocus) { text = ''; } customDataPoint = item.customDataPoint || props.customDataPoint; dataPointLabelComponent = item.dataPointLabelComponent; } const currentStripHeight = item.stripHeight ?? stripHeight; const currentStripWidth = item.stripWidth ?? stripWidth; const currentStripOpacity = item.stripOpacity ?? stripOpacity; const currentStripColor = item.stripColor || stripColor; return ( {focusEnabled ? ( <> {unFocusOnPressOut ? ( onStripPress(item, index)} onPressOut={() => setTimeout(() => setSelectedIndex(-1), delayBeforeUnFocus) } x={initialSpacing + (spacing * index - spacing / 2)} y={8} width={spacing} height={containerHeight} fill={'none'} /> ) : ( onStripPress(item, index)} x={initialSpacing + (spacing * index - spacing / 2)} y={8} width={spacing} height={containerHeight} fill={'none'} /> )} ) : null} {item.showStrip || (focusEnabled && index === selectedIndex && showStripOnFocus) ? ( ) : null} {customDataPoint ? ( {customDataPoint()} ) : null} {dataPointsShape === 'rectangular' ? ( {customDataPoint ? null : ( { item.onPress ? item.onPress(item, index) : props.onPress ? props.onPress(item, index) : null; }} /> )} ) : ( {customDataPoint ? null : ( { item.onPress ? item.onPress(item, index) : props.onPress ? props.onPress(item, index) : null; }} /> )} )} {dataPointLabelComponent ? ( !showTextOnFocus || index === selectedIndex ? ( {dataPointLabelComponent()} ) : null ) : text || item.dataPointText ? ( !showTextOnFocus || index === selectedIndex ? ( {!showTextOnFocus ? item.dataPointText : text} ) : null ) : null} ); }); }; const renderSpecificVerticalLines = (dataForRender: any) => { return dataForRender.map((item: bicolorLineDataItem, index: number) => { if (item.showVerticalLine) { return ( ); } return null; }); }; const lineSvgComponent = ( pointsArray: any, currentLineThickness: number | undefined, color: string, startFillColor: string, endFillColor: string, startOpacity: number, endOpacity: number, strokeDashArray: Array | undefined | null, ) => { return ( {strokeDashArray && strokeDashArray.length === 2 && typeof strokeDashArray[0] === 'number' && typeof strokeDashArray[1] === 'number' ? pointsArray.map((points: any, index: number) => ( )) : pointsArray.map((points: any, index: number) => { return ( ); })} {/*********************** For Area Chart ************/} {areaChart && ( <> )} {areaChart ? fillPointsArray.map((item, index) => { return ( ); }) : null} {/******************************************************************/} {renderSpecificVerticalLines(data)} {/*** !!! Here it's done thrice intentionally, trying to make it to only 1 breaks things !!! ***/} {!hideDataPoints1 ? renderDataPoints( data, dataPointsShape1, dataPointsWidth1, dataPointsHeight1, dataPointsColor1, dataPointsRadius1, textColor1, textFontSize1, startIndex1, endIndex1, ) : null} ); }; const renderLine = ( zIndex: number, pointsArray: any, currentLineThickness: number | undefined, color: string, startFillColor: string, endFillColor: string, startOpacity: number, endOpacity: number, strokeDashArray: Array | undefined | null, ) => { return ( {pointsArray.length ? lineSvgComponent( pointsArray, currentLineThickness, color, startFillColor, endFillColor, startOpacity, endOpacity, strokeDashArray, ) : null} ); }; const renderAnimatedLine = ( zIndex: number, points: any, animatedWidth: any, currentLineThickness: number | undefined, color: string, startFillColor: string, endFillColor: string, startOpacity: number, endOpacity: number, strokeDashArray: Array | undefined | null, ) => { return ( {lineSvgComponent( points, currentLineThickness, color, startFillColor, endFillColor, startOpacity, endOpacity, strokeDashArray, )} ); }; const renderChartContent = () => { return ( <> {isAnimated ? renderAnimatedLine( zIndex, pointsArray, animatedWidth, thickness1, color, startFillColor, endFillColor, startOpacity, endOpacity, strokeDashArray1, ) : renderLine( zIndex, pointsArray, thickness1, color, startFillColor, endFillColor, startOpacity, endOpacity, strokeDashArray1, )} {data.map((item: bicolorLineDataItem, index: number) => { return ( {isAnimated ? renderAnimatedLabel( index, item.label || (props.xAxisLabelTexts && props.xAxisLabelTexts[index] ? props.xAxisLabelTexts[index] : ''), item.labelTextStyle || props.xAxisLabelTextStyle, item.labelComponent, ) : renderLabel( index, item.label || (props.xAxisLabelTexts && props.xAxisLabelTexts[index] ? props.xAxisLabelTexts[index] : ''), item.labelTextStyle || props.xAxisLabelTextStyle, item.labelComponent, )} {/* {renderLabel(index, item.label, item.labelTextStyle)} */} ); })} ); }; return ( props.onScroll?.(ev), bounces: props.bounces, overScrollMode: props.overScrollMode ?? 'auto', }} /> ); };