|
| 1 | +import { |
| 2 | + BarChart, Bar, XAxis, YAxis, CartesianGrid, Tooltip, |
| 3 | + Legend, ResponsiveContainer, ReferenceLine, |
| 4 | +} from "recharts"; |
| 5 | + |
| 6 | +export interface DataPoint { |
| 7 | + block: string; |
| 8 | + [key: string]: string | number; |
| 9 | +} |
| 10 | + |
| 11 | +export interface SeriesConfig { |
| 12 | + key: string; |
| 13 | + label: string; |
| 14 | + color: string; |
| 15 | +} |
| 16 | + |
| 17 | +interface GroupedBarChartProps { |
| 18 | + data: DataPoint[]; |
| 19 | + series: SeriesConfig[]; |
| 20 | + yAxisLabel: string; |
| 21 | + yDomain?: [number, number]; |
| 22 | + referenceY?: number; |
| 23 | + height?: number; |
| 24 | +} |
| 25 | + |
| 26 | +const CustomTooltip = ({ active, payload, label }: any) => { |
| 27 | + if (active && payload && payload.length) { |
| 28 | + return ( |
| 29 | + <div className="rounded-lg border border-slate-200 bg-white px-3 py-2 shadow-lg text-sm"> |
| 30 | + <p className="font-semibold text-slate-800 mb-1">{label}</p> |
| 31 | + {payload.map((p: any) => ( |
| 32 | + <p key={p.name} style={{ color: p.color }}> |
| 33 | + {p.name}: <span className="font-medium">{Number(p.value).toFixed(3)}</span> |
| 34 | + </p> |
| 35 | + ))} |
| 36 | + </div> |
| 37 | + ); |
| 38 | + } |
| 39 | + return null; |
| 40 | +}; |
| 41 | + |
| 42 | +export default function GroupedBarChart({ |
| 43 | + data, |
| 44 | + series, |
| 45 | + yAxisLabel, |
| 46 | + yDomain, |
| 47 | + referenceY = 1, |
| 48 | + height = 320, |
| 49 | +}: GroupedBarChartProps) { |
| 50 | + const blocks = Array.from(new Set(data.map((d) => d.block))); |
| 51 | + |
| 52 | + // Build pivot: one row per block, columns = series keys |
| 53 | + const chartData = blocks.map((block) => { |
| 54 | + const entry: Record<string, string | number> = { block }; |
| 55 | + series.forEach((s) => { |
| 56 | + const row = data.find((d) => { |
| 57 | + if (d.block !== block) return false; |
| 58 | + const discrimKey = Object.keys(d).find( |
| 59 | + (k) => k !== "block" && typeof d[k] === "string" |
| 60 | + ); |
| 61 | + if (!discrimKey) return true; |
| 62 | + return d[discrimKey] === s.label; |
| 63 | + }); |
| 64 | + const valKey = row |
| 65 | + ? Object.keys(row).find((k) => typeof row[k] === "number") |
| 66 | + : undefined; |
| 67 | + entry[s.key] = row && valKey ? (row[valKey] as number) : 0; |
| 68 | + }); |
| 69 | + return entry; |
| 70 | + }); |
| 71 | + |
| 72 | + return ( |
| 73 | + <ResponsiveContainer width="100%" height={height}> |
| 74 | + <BarChart |
| 75 | + data={chartData} |
| 76 | + margin={{ top: 16, right: 24, left: 52, bottom: 64 }} |
| 77 | + barCategoryGap="28%" |
| 78 | + barGap={2} |
| 79 | + > |
| 80 | + <CartesianGrid strokeDasharray="3 3" stroke="#e2e8f0" /> |
| 81 | + <XAxis |
| 82 | + dataKey="block" |
| 83 | + tick={{ fontSize: 11, fill: "#475569" }} |
| 84 | + angle={-22} |
| 85 | + textAnchor="end" |
| 86 | + interval={0} |
| 87 | + height={64} |
| 88 | + /> |
| 89 | + <YAxis |
| 90 | + domain={yDomain} |
| 91 | + label={{ |
| 92 | + value: yAxisLabel, |
| 93 | + angle: -90, |
| 94 | + position: "insideLeft", |
| 95 | + offset: -36, |
| 96 | + style: { fontSize: 12, fill: "#475569", fontWeight: 600 }, |
| 97 | + }} |
| 98 | + tick={{ fontSize: 11, fill: "#475569" }} |
| 99 | + tickFormatter={(v) => Number(v).toFixed(2)} |
| 100 | + /> |
| 101 | + <Tooltip content={<CustomTooltip />} /> |
| 102 | + <Legend wrapperStyle={{ fontSize: 12, paddingTop: 8 }} /> |
| 103 | + {referenceY !== undefined && ( |
| 104 | + <ReferenceLine |
| 105 | + y={referenceY} |
| 106 | + stroke="#94a3b8" |
| 107 | + strokeDasharray="4 2" |
| 108 | + strokeWidth={1.5} |
| 109 | + /> |
| 110 | + )} |
| 111 | + {series.map((s) => ( |
| 112 | + <Bar |
| 113 | + key={s.key} |
| 114 | + dataKey={s.key} |
| 115 | + name={s.label} |
| 116 | + fill={s.color} |
| 117 | + radius={[3, 3, 0, 0]} |
| 118 | + maxBarSize={36} |
| 119 | + /> |
| 120 | + ))} |
| 121 | + </BarChart> |
| 122 | + </ResponsiveContainer> |
| 123 | + ); |
| 124 | +} |
0 commit comments