/** * DataChart Component * * Renders bar or line charts with support for dual Y-axes. */ import React from "react"; import { BarChart, Bar, LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip, Legend, ResponsiveContainer, } from "recharts"; interface UiConfig { type?: "table" | "chart" | "auto"; chartType?: "line" | "bar"; xAxis?: string; leftYAxis?: string | string[]; rightYAxis?: string | string[]; yAxis?: string; } export interface DataChartProps { data: unknown; config?: UiConfig | null; chartType: "bar" | "line"; xAxis: string; leftYAxes: string[]; rightYAxes: string[]; } // Color palette for charts const COLORS = [ "#8884d8", "#82ca9d", "#ffc658", "#ff7300", "#0088fe", "#00c49f", "#ff6b6b", "#4ecdc4", ]; export function DataChart({ data, chartType, xAxis, leftYAxes, rightYAxes, }: DataChartProps) { // Validation checks if (!Array.isArray(data) || data.length === 0) { return
No data for chart
; } const first = data[0]; if (typeof first !== "object" || first === null) { return
Data is not chartable
; } const hasLeftAxis = leftYAxes.length > 0; const hasRightAxis = rightYAxes.length > 0; const hasDualAxis = hasLeftAxis && hasRightAxis; if (!hasLeftAxis && !hasRightAxis) { return
Select at least one Y-axis field
; } const ChartComponent = chartType === "line" ? LineChart : BarChart; const DataComponent = chartType === "line" ? Line : Bar; return (
[]}> {/* Left Y-Axis */} {hasLeftAxis && ( )} {/* Right Y-Axis (only if we have fields for it) */} {hasRightAxis && ( )} {/* Left Y-Axis Data */} {leftYAxes.map((key, i) => chartType === "line" ? ( ) : ( ) )} {/* Right Y-Axis Data */} {rightYAxes.map((key, i) => chartType === "line" ? ( ) : ( ) )}
); }