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