Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | import { useMemo } from "react";
import * as d3 from "d3";
export const useGapChartScales = (
xAxisDomain: number[],
yAxisDomain: string[],
width: number,
height: number,
margin: { top: number; right: number; bottom: number; left: number },
xAxisDataType: "number" | "date_annual" | "date_monthly",
tickHtmlWidth?: number
) => {
const xScale = useMemo(() => {
// Adjust the left margin to account for tickHtmlWidth
const adjustedLeftMargin = margin.left + (tickHtmlWidth || 0);
if (xAxisDataType === "number") {
return d3
.scaleLinear()
.domain(xAxisDomain as [number, number])
.range([adjustedLeftMargin, width - margin.right])
.nice();
} else if (xAxisDataType === "date_annual") {
const [min, max] = xAxisDomain;
return d3
.scaleTime()
.domain([new Date(min, 0, 1), new Date(max, 0, 1)])
.range([adjustedLeftMargin, width - margin.right]);
} else {
// date_monthly
const [min, max] = xAxisDomain;
return d3
.scaleTime()
.domain([new Date(min), new Date(max)])
.range([adjustedLeftMargin, width - margin.right]);
}
}, [xAxisDomain, width, margin, xAxisDataType, tickHtmlWidth]);
const yScale = useMemo(
() =>
d3
.scaleBand()
.domain(yAxisDomain)
.range([margin.top, height - margin.bottom])
.padding(0.3),
[yAxisDomain, height, margin]
);
return { xScale, yScale };
};
|