import {View, TouchableOpacity, Animated, Text} from 'react-native';
import AnimatedThreeDBar from '../Components/AnimatedThreeDBar';
import Animated2DWithGradient from './Animated2DWithGradient';
import {
getPropsForAnimated2DWithGradient,
RenderBarsPropsType,
useRenderBars,
} from 'gifted-charts-core';
import Tooltip from '../Components/BarSpecificComponents/tooltip';
const isIndexSelected = (values: number | number[], idx: number) => {
if (Array.isArray(values)) return values.includes(idx);
return values === idx;
};
const RenderBars = (props: RenderBarsPropsType) => {
const {
item,
index,
containerHeight,
maxValue,
minHeight,
spacing,
side,
data,
barBorderWidth,
barBorderColor,
isThreeD,
isAnimated,
rotateLabel,
appearingOpacity,
animationDuration,
autoShiftLabels,
label,
secondaryLabel,
labelTextStyle,
secondaryLabelTextStyle,
xAxisTextNumberOfLines,
xAxisLabelsVerticalShift,
renderTooltip,
selectedIndex,
setSelectedIndex,
horizontal,
rtl,
pointerConfig,
noOfSectionsBelowXAxis,
barWidth,
labelsDistanceFromXaxis = 0,
secondaryXAxis,
secondaryNoOfSectionsBelowXAxis,
barMarginBottom = 0,
highlightEnabled,
highlightedBarIndex,
lowlightOpacity,
isCandleStickChart,
showValuesAsBottomLabel,
} = props;
const {heightFactor, barHeight, tooltipProps} = useRenderBars(props);
const {
commonStyleForBar,
barStyleWithBackground,
commonPropsFor2dAnd3dBars,
isFocused,
focusedBarConfig,
localFrontColor,
} = getPropsForAnimated2DWithGradient({...props, barHeight});
const itemOrPropsBarInnerComponent =
item.barInnerComponent ?? props.barInnerComponent;
const localBarInnerComponent = isFocused
? (focusedBarConfig?.barInnerComponent ?? itemOrPropsBarInnerComponent)
: itemOrPropsBarInnerComponent;
const renderLabel = (
top: boolean,
label: string,
labelTextStyle: any,
value: number,
) => {
return (
{top ? (
item.secondaryLabelComponent ? (
item.secondaryLabelComponent()
) : (
{label}
)
) : item.labelComponent ? (
item.labelComponent()
) : (
{label}
)}
);
};
const renderAnimatedLabel = (
top: boolean,
label: string,
labelTextStyle: any,
value: number,
) => {
return (
{top ? (
item.secondaryLabelComponent ? (
item.secondaryLabelComponent()
) : (
{label}
)
) : item.labelComponent ? (
item.labelComponent()
) : (
{label}
)}
);
};
const barWrapperStyle = [
{
// overflow: 'visible',
opacity: highlightEnabled
? isIndexSelected(highlightedBarIndex, -1)
? 1
: isIndexSelected(highlightedBarIndex, index)
? 1
: lowlightOpacity
: 1,
marginBottom: 60 + barMarginBottom + xAxisLabelsVerticalShift - 0.5,
width: commonPropsFor2dAnd3dBars.barWidth,
height: barHeight,
marginRight: spacing,
transform: [
{
translateY:
(containerHeight || 200) -
(barHeight - 10 + xAxisLabelsVerticalShift) +
(item.value < 0
? Math.max(minHeight, Math.abs(item.value) * heightFactor)
: 0),
},
{rotateZ: item.value < 0 ? '180deg' : '0deg'},
],
},
side !== 'right' && {zIndex: data.length - index},
];
const pressDisabled =
item.disablePress ||
props.disablePress ||
(pointerConfig && pointerConfig.barTouchable !== true);
const barContent = () => {
const isBarBelowXaxisAndInvisible =
item.value < 0 &&
!(noOfSectionsBelowXAxis || secondaryNoOfSectionsBelowXAxis);
const animated2DWithGradient = (
noGradient: boolean,
noAnimation: boolean,
) => (
);
return (
<>
{(props.showXAxisIndices || item.showXAxisIndex) && (
)}
{isBarBelowXaxisAndInvisible ? null : isThreeD ? (
) : item.showGradient || props.showGradient ? (
isAnimated ? (
animated2DWithGradient(false, false)
) : (
animated2DWithGradient(false, true)
)
) : isAnimated ? (
animated2DWithGradient(true, false)
) : (
animated2DWithGradient(true, true)
)}
{isAnimated
? renderAnimatedLabel(false, label, labelTextStyle, item.value)
: renderLabel(false, label, labelTextStyle, item.value)}
{secondaryXAxis
? isAnimated
? renderAnimatedLabel(
true,
secondaryLabel,
secondaryLabelTextStyle,
item.value,
)
: renderLabel(
true,
secondaryLabel,
secondaryLabelTextStyle,
item.value,
)
: null}
>
);
};
return (
<>
{pressDisabled ? (
{barContent()}
) : (
{
if (renderTooltip || props.focusBarOnPress || highlightEnabled) {
if (props.focusedBarIndex === undefined || !props.onPress) {
setSelectedIndex([index]);
}
}
item.onPress
? item.onPress()
: props.onPress
? props.onPress(item, index)
: null;
}}
onLongPress={() => {
item.onLongPress
? item.onLongPress()
: props.onLongPress
? props.onLongPress(item, index)
: null;
}}
onPressOut={() => {
item.onPressOut
? item.onPressOut()
: props.onPressOut
? props.onPressOut(item, index)
: null;
}}
style={barWrapperStyle}>
{barContent()}
)}
{renderTooltip && selectedIndex.includes?.(index) && (
)}
>
);
};
export default RenderBars;