'use client' import { BarChart, Bar, LineChart, Line, PieChart, Pie, Cell, XAxis, YAxis, CartesianGrid, Tooltip, ResponsiveContainer, } from 'recharts' import type { ChartArtifact } from '@/types/chat' const DEFAULT_COLORS = [ 'var(--color-chart-1, #3b82f6)', 'var(--color-chart-2, #10b981)', 'var(--color-chart-3, #f59e0b)', 'var(--color-chart-4, #ef4444)', 'var(--color-chart-5, #8b5cf6)', 'var(--color-chart-6, #ec4899)', 'var(--color-chart-7, #06b6d4)', 'var(--color-chart-8, #84cc16)', ] function formatValue(value: number, unit?: string): string { const formatted = new Intl.NumberFormat('sv-SE').format(Math.round(value)) return unit ? `${formatted} ${unit}` : formatted } interface ChatChartProps { artifact: ChartArtifact } export function ChatChart({ artifact }: ChatChartProps) { const { type, title, data, unit, subtitle } = artifact const chartData = data.map((d, i) => ({ ...d, fill: d.color || DEFAULT_COLORS[i % DEFAULT_COLORS.length], })) return (

{title}

{subtitle &&

{subtitle}

}
{type === 'pie_chart' ? ( { const name = props.name ?? '' const percent = typeof props.percent === 'number' ? props.percent : 0 return `${name} (${(percent * 100).toFixed(0)}%)` }} labelLine={false} > {chartData.map((entry, i) => ( ))} formatValue(Number(value ?? 0), unit)} /> ) : type === 'line_chart' ? ( formatValue(v, unit)} className="text-muted-foreground" /> formatValue(Number(value ?? 0), unit)} /> ) : ( formatValue(v, unit)} className="text-muted-foreground" /> formatValue(Number(value ?? 0), unit)} /> {chartData.map((entry, i) => ( ))} )}
) }