|
| 1 | +import React, { useRef, useEffect, useState } from "react"; |
| 2 | +import * as d3 from "d3"; |
| 3 | +import { MetricData } from "../types"; |
| 4 | + |
| 5 | +interface BaseChartProps { |
| 6 | + data: MetricData[]; |
| 7 | + metricKey: string; |
| 8 | + title?: string; |
| 9 | + description?: string; |
| 10 | + children: ( |
| 11 | + svg: d3.Selection<SVGGElement, unknown, null, undefined>, |
| 12 | + dimensions: { |
| 13 | + width: number; |
| 14 | + height: number; |
| 15 | + margin: { top: number; right: number; bottom: number; left: number }; |
| 16 | + }, |
| 17 | + ) => void; |
| 18 | +} |
| 19 | + |
| 20 | +const TOP_MARGIN = 20; |
| 21 | +const ASPECT_RATIO = 0.5; |
| 22 | +const LEGEND_SPACE = 20; |
| 23 | +const X_AXIS_SPACE = 60; |
| 24 | +const Y_AXIS_SPACE = 40; |
| 25 | +const TITLE_SPACE = 50; |
| 26 | + |
| 27 | +const DEFAULT_MARGIN = { |
| 28 | + top: TOP_MARGIN + TITLE_SPACE, |
| 29 | + right: 40, |
| 30 | + bottom: Y_AXIS_SPACE + LEGEND_SPACE, |
| 31 | + left: X_AXIS_SPACE, |
| 32 | +}; |
| 33 | + |
| 34 | +const BaseChart: React.FC<BaseChartProps> = ({ |
| 35 | + data, |
| 36 | + metricKey, |
| 37 | + title, |
| 38 | + description, |
| 39 | + children, |
| 40 | +}: BaseChartProps) => { |
| 41 | + const svgRef = useRef<SVGSVGElement>(null); |
| 42 | + const wrapperRef = useRef<HTMLDivElement>(null); |
| 43 | + const [dimensions, setDimensions] = useState<{ |
| 44 | + width: number; |
| 45 | + height: number; |
| 46 | + margin: typeof DEFAULT_MARGIN; |
| 47 | + } | null>(null); |
| 48 | + |
| 49 | + const initialDimensions = useRef(dimensions); |
| 50 | + initialDimensions.current = dimensions; |
| 51 | + |
| 52 | + useEffect(() => { |
| 53 | + const updateDimensions = () => { |
| 54 | + if (wrapperRef.current) { |
| 55 | + const width = wrapperRef.current.offsetWidth; |
| 56 | + const height = width * ASPECT_RATIO; |
| 57 | + // Ensure minimum dimensions after margin calculation |
| 58 | + const calculatedWidth = Math.max( |
| 59 | + 100, |
| 60 | + width - DEFAULT_MARGIN.left - DEFAULT_MARGIN.right, |
| 61 | + ); |
| 62 | + const calculatedHeight = Math.max( |
| 63 | + 50, |
| 64 | + height - DEFAULT_MARGIN.top - DEFAULT_MARGIN.bottom, |
| 65 | + ); |
| 66 | + setDimensions({ |
| 67 | + width: calculatedWidth, |
| 68 | + height: calculatedHeight, |
| 69 | + margin: DEFAULT_MARGIN, |
| 70 | + }); |
| 71 | + } |
| 72 | + }; |
| 73 | + |
| 74 | + // Ensure initial dimensions are set even if ResizeObserver fires quickly |
| 75 | + if (!initialDimensions.current && wrapperRef.current?.offsetWidth) { |
| 76 | + updateDimensions(); |
| 77 | + } |
| 78 | + |
| 79 | + const resizeObserver = new ResizeObserver(updateDimensions); |
| 80 | + if (wrapperRef.current) { |
| 81 | + resizeObserver.observe(wrapperRef.current); |
| 82 | + } |
| 83 | + |
| 84 | + return () => { |
| 85 | + resizeObserver.disconnect(); |
| 86 | + }; |
| 87 | + // Only depends on the existence of the wrapper ref |
| 88 | + }, []); |
| 89 | + |
| 90 | + useEffect(() => { |
| 91 | + if (!svgRef.current || !dimensions) return; |
| 92 | + |
| 93 | + const svgRoot = d3.select(svgRef.current); |
| 94 | + svgRoot.selectAll("*").remove(); // Clear previous render |
| 95 | + |
| 96 | + const svg = svgRoot |
| 97 | + .attr( |
| 98 | + "width", |
| 99 | + dimensions.width + dimensions.margin.left + dimensions.margin.right, |
| 100 | + ) |
| 101 | + .attr( |
| 102 | + "height", |
| 103 | + dimensions.height + dimensions.margin.top + dimensions.margin.bottom, |
| 104 | + ) |
| 105 | + .append("g") |
| 106 | + .attr( |
| 107 | + "transform", |
| 108 | + `translate(${dimensions.margin.left},${dimensions.margin.top})`, |
| 109 | + ); |
| 110 | + |
| 111 | + if (title) { |
| 112 | + svg |
| 113 | + .append("text") |
| 114 | + .attr("x", dimensions.width / 2) |
| 115 | + .attr("y", -TOP_MARGIN - 20) |
| 116 | + .attr("text-anchor", "middle") |
| 117 | + .style("font-size", "16px") |
| 118 | + .style("font-weight", "bold") |
| 119 | + .text(title); |
| 120 | + } |
| 121 | + |
| 122 | + if (description) { |
| 123 | + svg |
| 124 | + .append("text") |
| 125 | + .attr("x", dimensions.width / 2) |
| 126 | + .attr("y", -TOP_MARGIN) |
| 127 | + .attr("text-anchor", "middle") |
| 128 | + .style("font-size", "12px") |
| 129 | + .style("fill", "#666") |
| 130 | + .text(description); |
| 131 | + } |
| 132 | + |
| 133 | + children(svg, dimensions); |
| 134 | + |
| 135 | + // Re-render when data or dimensions change |
| 136 | + }, [data, metricKey, title, description, children, dimensions]); |
| 137 | + |
| 138 | + return ( |
| 139 | + <div className="chart-wrapper" ref={wrapperRef}> |
| 140 | + {/* Render SVG only when dimensions are known */} |
| 141 | + {dimensions && <svg ref={svgRef}></svg>} |
| 142 | + </div> |
| 143 | + ); |
| 144 | +}; |
| 145 | + |
| 146 | +export default BaseChart; |
0 commit comments