react-native-gifted-charts
Version:
The most complete library for Bar, Line, Area, Pie, Donut, Stacked Bar, Population Pyramid, Radar, Bubble and SCatter charts in React Native. Allows 2D, 3D, gradient, animations and live data updates.
99 lines (98 loc) • 7.81 kB
JavaScript
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
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';
var Animated2DWithGradient = function (props) {
var _a, _b, _c, _d;
var barBackgroundPattern = props.barBackgroundPattern, patternId = props.patternId, bWidth = props.barWidth, barStyle = props.barStyle, item = props.item, index = props.index, opacity = props.opacity, animationDuration = props.animationDuration, noGradient = props.noGradient, noAnimation = props.noAnimation, barInnerComponent = props.barInnerComponent, intactTopLabel = props.intactTopLabel, showValuesAsTopLabel = props.showValuesAsTopLabel, topLabelContainerStyle = props.topLabelContainerStyle, bottomLabelContainerStyle = props.bottomLabelContainerStyle, topLabelTextStyle = props.topLabelTextStyle, bottomLabelTextStyle = props.bottomLabelTextStyle, commonStyleForBar = props.commonStyleForBar, barStyleWithBackground = props.barStyleWithBackground, yAxisOffset = props.yAxisOffset, height = props.height, _e = props.barMarginBottom, barMarginBottom = _e === void 0 ? 0 : _e, isCandleStickChart = props.isCandleStickChart, showValuesAsBottomLabel = props.showValuesAsBottomLabel;
var barWidth = (_a = item.barWidth) !== null && _a !== void 0 ? _a : bWidth; // setting width in state for animation purpose
var topLabelPosition = (item.barWidth || barWidth || 30) * -1;
var bottomLabelPosition = height - barMarginBottom;
var animatedHeight = useRef(new Animated.Value(0)).current; // initial height = 0
var animatedLabelHeight = useRef(new Animated.Value(height + topLabelPosition)).current;
var elevate = function () {
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(function () {
if (!noAnimation) {
elevate();
}
}, []);
return (_jsxs(_Fragment, { children: [_jsxs(Animated.View, { style: [
{
position: 'absolute',
bottom: 0,
width: barWidth,
overflow: isCandleStickChart ? 'visible' : 'hidden',
height: noAnimation
? height - (isCandleStickChart ? barMarginBottom : 0)
: animatedHeight,
},
item.barStyle || barStyle,
], children: [noGradient ? (_jsx(View, { style: barStyleWithBackground, children: props.cappedBars && item.value ? (_jsx(Cap, { capThicknessFromItem: item.capThickness, capThicknessFromProps: props.capThickness, capColorFromItem: item.capColor, capColorFromProps: props.capColor, capRadiusFromItem: item.capRadius, capRadiusFromProps: props.capRadius })) : null })) : (_jsx(LinearGradient, { style: commonStyleForBar, start: { x: 0, y: 0 }, end: { x: 1, y: 1 }, colors: [
item.gradientColor || props.gradientColor || 'white',
item.frontColor || props.frontColor || 'black',
], children: props.cappedBars && (_jsx(View, { style: {
position: 'absolute',
width: '100%',
height: item.capThickness === 0
? 0
: item.capThickness || props.capThickness || 6,
backgroundColor: item.capColor || props.capColor || 'black',
borderTopLeftRadius: item.capRadius === 0
? 0
: item.capRadius || props.capRadius || 0,
borderTopRightRadius: item.capRadius === 0
? 0
: item.capRadius || props.capRadius || 0,
} })) })), (item.barBackgroundPattern || barBackgroundPattern) && (_jsxs(Svg, { children: [_jsx(Defs, { children: item.barBackgroundPattern
? item.barBackgroundPattern()
: barBackgroundPattern === null || barBackgroundPattern === void 0 ? void 0 : barBackgroundPattern() }), _jsx(Rect, { stroke: "none", x: "1", y: "1", width: item.barWidth || barWidth || 30, height: noAnimation ? Math.abs(height) : height, fill: "url(#".concat(item.patternId || patternId, ")") })] })), barInnerComponent ? (_jsx(View, { style: { height: '100%', width: '100%' }, children: barInnerComponent(item, index) })) : null] }), item.topLabelComponent || showValuesAsTopLabel ? (_jsx(Animated.View, { style: [
{
position: 'absolute',
top: noAnimation ? topLabelPosition : animatedLabelHeight,
height: item.barWidth || barWidth || 30,
width: item.barWidth || barWidth || 30,
justifyContent: (props.horizontal && !intactTopLabel) || item.value < 0
? 'center'
: 'flex-end',
alignItems: 'center',
opacity: opacity,
},
item.value < 0 && { transform: [{ rotate: '180deg' }] },
props.horizontal &&
!intactTopLabel && { transform: [{ rotate: '270deg' }] },
topLabelContainerStyle !== null && topLabelContainerStyle !== void 0 ? topLabelContainerStyle : item.topLabelContainerStyle,
], children: showValuesAsTopLabel ? (_jsx(Text, { style: topLabelTextStyle, children: item.value + yAxisOffset })) : ((_b = item.topLabelComponent) === null || _b === void 0 ? void 0 : _b.call(item)) })) : null, isCandleStickChart &&
(item.bottomLabelComponent || showValuesAsBottomLabel) ? (_jsx(Animated.View, { style: [
{
position: 'absolute',
top: noAnimation ? bottomLabelPosition : animatedLabelHeight,
height: item.barWidth || barWidth || 30,
width: item.barWidth || barWidth || 30,
justifyContent: (props.horizontal && !intactTopLabel) || item.value < 0
? 'center'
: 'flex-end',
alignItems: 'center',
opacity: opacity,
},
item.value < 0 && { transform: [{ rotate: '180deg' }] },
props.horizontal &&
!intactTopLabel && { transform: [{ rotate: '270deg' }] },
bottomLabelContainerStyle !== null && bottomLabelContainerStyle !== void 0 ? bottomLabelContainerStyle : item.bottomLabelContainerStyle,
], children: showValuesAsBottomLabel ? (_jsx(Text, { style: bottomLabelTextStyle, children: ((_c = item.lowerValue) !== null && _c !== void 0 ? _c : 0) + yAxisOffset })) : ((_d = item.bottomLabelComponent) === null || _d === void 0 ? void 0 : _d.call(item)) })) : null] }));
};
export default Animated2DWithGradient;