2024-01-10 13:30:46 -07:00
|
|
|
|
|
|
|
import { Svg } from 'react-native-svg';
|
|
|
|
|
|
|
|
import { Bar } from './bar';
|
2024-01-15 18:40:24 -07:00
|
|
|
import { ScaleBandType, ScaleLinearType } from './graph-types';
|
2024-01-10 13:30:46 -07:00
|
|
|
import { calculateBarWidth } from './custom-bar-utils';
|
|
|
|
|
|
|
|
export interface CombinedData {
|
|
|
|
data: { value: number }[];
|
|
|
|
svg: { fill: string };
|
|
|
|
}
|
|
|
|
|
|
|
|
interface CustomBarsProps {
|
2024-01-15 18:40:24 -07:00
|
|
|
x: ScaleBandType;
|
|
|
|
y: ScaleLinearType;
|
2024-01-10 13:30:46 -07:00
|
|
|
bandwidth: number;
|
|
|
|
barColors: string[];
|
2024-01-15 18:40:24 -07:00
|
|
|
xValues: unknown[]; // TODO: update this value when this data type is defined
|
|
|
|
barData: CombinedData[];
|
2024-01-10 13:30:46 -07:00
|
|
|
gap: number;
|
|
|
|
roundedRadius: number;
|
|
|
|
}
|
|
|
|
|
2024-01-15 18:40:24 -07:00
|
|
|
export const CustomBars: React.FC<Partial<CustomBarsProps>> = ({
|
2024-01-10 13:30:46 -07:00
|
|
|
x: scaleX,
|
|
|
|
y: scaleY,
|
|
|
|
bandwidth,
|
2024-01-15 18:40:24 -07:00
|
|
|
barData,
|
|
|
|
xValues,
|
2024-01-10 13:30:46 -07:00
|
|
|
barColors,
|
|
|
|
gap = 2,
|
|
|
|
roundedRadius = 4
|
|
|
|
}) => {
|
2024-01-15 18:40:24 -07:00
|
|
|
const barWidth = calculateBarWidth(bandwidth, barData.length, gap);
|
2024-01-10 13:30:46 -07:00
|
|
|
|
2024-01-15 18:40:24 -07:00
|
|
|
return xValues.map((_, index) => (
|
2024-01-10 13:30:46 -07:00
|
|
|
<Svg key={`group-${index}`} testID={`svg-${index}`}>
|
2024-01-15 18:40:24 -07:00
|
|
|
{barData.map((item, i) => (
|
2024-01-10 13:30:46 -07:00
|
|
|
<Bar
|
|
|
|
key={`bar-${i}-${index}`}
|
|
|
|
scaleX={scaleX}
|
|
|
|
scaleY={scaleY}
|
|
|
|
data={item.data[index]}
|
2024-01-15 18:40:24 -07:00
|
|
|
barNumber={i}
|
|
|
|
index={index}
|
2024-01-10 13:30:46 -07:00
|
|
|
fill={barColors[i]}
|
|
|
|
barWidth={barWidth}
|
|
|
|
gap={gap}
|
|
|
|
roundedRadius={roundedRadius}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</Svg>
|
|
|
|
));
|
|
|
|
};
|