import React from 'react'; import { TableData, ExtractedDataRow } from '../types'; import { LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip, Legend, ResponsiveContainer, ScatterChart, Scatter, BarChart, Bar } from 'recharts'; interface DataVisualizerProps { tables: TableData[]; } const DataVisualizer: React.FC = ({ tables }) => { // Filter tables that have configuration from LLM const visualizableTables = tables.filter(t => t.visualization_config && t.rows.length > 0); if (visualizableTables.length === 0) { return (

No visualizable data found.

The AI determines which tables contain data suitable for charting.

); } // Helper to validate chart data const isValidChartData = (rows: ExtractedDataRow[], xKey: string, yKeys: string[]): boolean => { if (!rows || rows.length === 0) return false; if (!xKey || !yKeys || yKeys.length === 0) return false; // Check if at least some rows have valid data for the specified keys const hasValidData = rows.some(row => { const hasX = row[xKey] !== undefined && row[xKey] !== null; const hasY = yKeys.some(yKey => row[yKey] !== undefined && row[yKey] !== null); return hasX && hasY; }); return hasValidData; }; // Common chart margins - increased to prevent overlap const chartMargins = { top: 20, right: 40, left: 70, bottom: 80 }; // Custom axis tick formatter for long labels const formatAxisTick = (value: any): string => { if (typeof value === 'string' && value.length > 12) { return value.substring(0, 12) + '...'; } return String(value); }; return (
{visualizableTables.map((table, idx) => { const config = table.visualization_config!; const { type, xAxisKey, yAxisKeys, title } = config; // Validate chart data before rendering if (!isValidChartData(table.rows, xAxisKey, yAxisKeys)) { return (

{title || table.title}

Insufficient data for chart visualization

); } // Generate chart description const chartDescription = `Showing ${yAxisKeys.join(', ')} plotted against ${xAxisKey}${table.rows.length > 0 ? ` (${table.rows.length} data points)` : ''}`; return (

{title || table.title}

{(() => { switch (type) { case 'line_chart': return ( {yAxisKeys.map((key, i) => ( ))} ); case 'bar_chart': return ( {yAxisKeys.map((key, i) => ( ))} ); case 'scatter_chart': const yKey = yAxisKeys[0]; return ( ); default: return null; } })()}
{/* Chart Description */}

{chartDescription}

); })}
); }; export default DataVisualizer;