2024-02-03 20:23:31 -07:00
|
|
|
import { path as d3 } from "d3-path";
|
2024-01-10 13:30:46 -07:00
|
|
|
|
2024-02-03 20:30:00 -07:00
|
|
|
import { ScaleBandType, ScaleLinearType } from "./graph-types";
|
2024-01-10 13:30:46 -07:00
|
|
|
|
|
|
|
type BarCalculationProps = {
|
2024-02-03 20:23:31 -07:00
|
|
|
scaleX: ScaleBandType;
|
|
|
|
scaleY: ScaleLinearType;
|
|
|
|
data: { value: number };
|
|
|
|
barNumber: number;
|
|
|
|
index: number;
|
|
|
|
barWidth: number;
|
|
|
|
gap: number;
|
2024-01-10 13:30:46 -07:00
|
|
|
};
|
|
|
|
|
2024-01-10 19:09:47 -07:00
|
|
|
export function calculateBarOrigin({
|
2024-02-03 20:23:31 -07:00
|
|
|
scaleX,
|
|
|
|
scaleY,
|
|
|
|
barWidth,
|
|
|
|
data,
|
|
|
|
index,
|
|
|
|
barNumber,
|
|
|
|
gap,
|
2024-01-10 13:30:46 -07:00
|
|
|
}: BarCalculationProps): { xOrigin: number; yOrigin: number; height: number } {
|
2024-02-03 20:23:31 -07:00
|
|
|
const firstBar = barNumber === 0;
|
|
|
|
const xOrigin =
|
|
|
|
scaleX(index) + (firstBar ? 0 : barWidth * barNumber + gap * barNumber);
|
|
|
|
const yOrigin = scaleY(data.value);
|
|
|
|
const height = scaleY(0) - yOrigin;
|
2024-01-10 13:30:46 -07:00
|
|
|
|
2024-02-03 20:23:31 -07:00
|
|
|
return { xOrigin, yOrigin, height };
|
2024-01-10 13:30:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export function drawBarPath(
|
2024-02-03 20:23:31 -07:00
|
|
|
xOrigin: number,
|
|
|
|
yOrigin: number,
|
|
|
|
barWidth: number,
|
|
|
|
height: number,
|
|
|
|
roundedRadius: number,
|
2024-01-10 13:30:46 -07:00
|
|
|
): string {
|
2024-02-03 20:23:31 -07:00
|
|
|
const path = d3();
|
|
|
|
path.moveTo(xOrigin, yOrigin + height);
|
|
|
|
path.lineTo(xOrigin, yOrigin + roundedRadius);
|
|
|
|
path.arcTo(xOrigin, yOrigin, xOrigin + roundedRadius, yOrigin, roundedRadius);
|
|
|
|
path.lineTo(xOrigin + barWidth - roundedRadius, yOrigin);
|
|
|
|
path.arcTo(
|
|
|
|
xOrigin + barWidth,
|
|
|
|
yOrigin,
|
|
|
|
xOrigin + barWidth,
|
|
|
|
yOrigin + roundedRadius,
|
|
|
|
roundedRadius,
|
|
|
|
);
|
|
|
|
path.lineTo(xOrigin + barWidth, yOrigin + height);
|
|
|
|
path.lineTo(xOrigin, yOrigin + height);
|
|
|
|
path.closePath();
|
2024-01-10 13:30:46 -07:00
|
|
|
|
2024-02-03 20:23:31 -07:00
|
|
|
return path.toString();
|
2024-01-10 13:30:46 -07:00
|
|
|
}
|
|
|
|
|
2024-02-03 20:23:31 -07:00
|
|
|
export const calculateBarWidth = (
|
|
|
|
bandwidth: number,
|
|
|
|
combinedDataLength: number,
|
|
|
|
gap: number,
|
|
|
|
) => (bandwidth - gap * (combinedDataLength - 1)) / combinedDataLength;
|