|
| 1 | +/** |
| 2 | + * CostByModelChart.tsx — Horizontal bar chart showing cost per model. |
| 3 | + * |
| 4 | + * Displays total USD grouped by model with color coding. |
| 5 | + * Part of issue #3273: Cost Analytics Panels. |
| 6 | + */ |
| 7 | + |
| 8 | +import { |
| 9 | + BarChart, |
| 10 | + Bar, |
| 11 | + XAxis, |
| 12 | + YAxis, |
| 13 | + Tooltip, |
| 14 | + CartesianGrid, |
| 15 | + Cell, |
| 16 | +} from 'recharts'; |
| 17 | +import { formatCurrency } from '../../utils/formatNumber'; |
| 18 | +import { ChartFrame } from '../shared/ChartFrame'; |
| 19 | + |
| 20 | +export interface CostByModelDataPoint { |
| 21 | + model: string; |
| 22 | + cost: number; |
| 23 | +} |
| 24 | + |
| 25 | +export interface CostByModelChartProps { |
| 26 | + data?: CostByModelDataPoint[]; |
| 27 | + loading?: boolean; |
| 28 | + className?: string; |
| 29 | +} |
| 30 | + |
| 31 | +const MODEL_COLORS: Record<string, string> = { |
| 32 | + 'claude-opus-4.7': 'var(--color-accent-purple)', |
| 33 | + 'claude-sonnet-4.6': 'var(--color-accent-cyan)', |
| 34 | + 'claude-haiku-4.5': 'var(--color-success)', |
| 35 | + 'gpt-5.4': 'var(--color-warning)', |
| 36 | + 'gpt-4.1': 'var(--color-info)', |
| 37 | + other: 'var(--color-text-muted)', |
| 38 | +}; |
| 39 | + |
| 40 | +const MOCK_DATA: CostByModelDataPoint[] = [ |
| 41 | + { model: 'claude-opus-4.7', cost: 47.82 }, |
| 42 | + { model: 'claude-sonnet-4.6', cost: 28.15 }, |
| 43 | + { model: 'claude-haiku-4.5', cost: 3.40 }, |
| 44 | + { model: 'gpt-5.4', cost: 12.67 }, |
| 45 | + { model: 'gpt-4.1', cost: 5.91 }, |
| 46 | +]; |
| 47 | + |
| 48 | +function CustomTooltip({ active, payload }: { |
| 49 | + active?: boolean; |
| 50 | + payload?: Array<{ payload: CostByModelDataPoint }>; |
| 51 | +}) { |
| 52 | + if (!active || !payload?.length) return null; |
| 53 | + const point = payload[0].payload; |
| 54 | + return ( |
| 55 | + <div className="rounded-lg border border-[var(--color-border-strong)] bg-[var(--color-surface)] p-3 shadow-xl"> |
| 56 | + <p className="mb-1 text-xs font-mono text-[var(--color-text-muted)]"> |
| 57 | + {point.model} |
| 58 | + </p> |
| 59 | + <p className="text-sm font-mono font-medium text-[var(--color-text-primary)]"> |
| 60 | + {formatCurrency(point.cost)} |
| 61 | + </p> |
| 62 | + </div> |
| 63 | + ); |
| 64 | +} |
| 65 | + |
| 66 | +export function CostByModelChart({ data, loading = false, className = '' }: CostByModelChartProps) { |
| 67 | + const chartData = data ?? MOCK_DATA; |
| 68 | + |
| 69 | + if (loading) { |
| 70 | + return ( |
| 71 | + <section |
| 72 | + className={`rounded-lg border border-[var(--color-border-strong)] bg-[var(--color-surface-strong)] p-5 ${className}`} |
| 73 | + aria-label="Cost by model chart loading" |
| 74 | + > |
| 75 | + <h3 className="mb-4 text-lg font-medium text-[var(--color-text-primary)]"> |
| 76 | + Cost by Model |
| 77 | + </h3> |
| 78 | + <div className="flex h-64 items-center justify-center"> |
| 79 | + <div className="h-4 w-4 animate-spin rounded-full border-2 border-[var(--color-accent-purple)] border-t-transparent" /> |
| 80 | + </div> |
| 81 | + </section> |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + if (chartData.length === 0) { |
| 86 | + return ( |
| 87 | + <section |
| 88 | + className={`rounded-lg border border-[var(--color-border-strong)] bg-[var(--color-surface-strong)] p-5 ${className}`} |
| 89 | + aria-label="Cost by model chart" |
| 90 | + > |
| 91 | + <h3 className="mb-4 text-lg font-medium text-[var(--color-text-primary)]"> |
| 92 | + Cost by Model |
| 93 | + </h3> |
| 94 | + <p className="py-12 text-center text-sm text-[var(--color-text-muted)]"> |
| 95 | + No model cost data available yet. |
| 96 | + </p> |
| 97 | + </section> |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | + return ( |
| 102 | + <section |
| 103 | + className={`rounded-lg border border-[var(--color-border-strong)] bg-[var(--color-surface-strong)] p-5 ${className}`} |
| 104 | + aria-label="Cost by model chart" |
| 105 | + > |
| 106 | + <div className="mb-4 flex items-center justify-between"> |
| 107 | + <h3 className="text-lg font-medium text-[var(--color-text-primary)]"> |
| 108 | + Cost by Model |
| 109 | + </h3> |
| 110 | + <span className="text-xs text-[var(--color-text-muted)]"> |
| 111 | + Total spend per model |
| 112 | + </span> |
| 113 | + </div> |
| 114 | + |
| 115 | + {/* Legend */} |
| 116 | + <div className="mb-3 flex flex-wrap gap-3"> |
| 117 | + {chartData.map((entry) => ( |
| 118 | + <div key={entry.model} className="flex items-center gap-1.5"> |
| 119 | + <div |
| 120 | + className="h-2.5 w-2.5 rounded-sm" |
| 121 | + style={{ backgroundColor: MODEL_COLORS[entry.model] ?? MODEL_COLORS.other }} |
| 122 | + /> |
| 123 | + <span className="text-xs text-[var(--color-text-muted)]">{entry.model}</span> |
| 124 | + </div> |
| 125 | + ))} |
| 126 | + </div> |
| 127 | + |
| 128 | + <ChartFrame className="h-64 min-w-0" label="Cost by model chart loading"> |
| 129 | + {({ width, height }) => ( |
| 130 | + <BarChart width={width} height={height} data={chartData} layout="vertical" margin={{ left: 20 }}> |
| 131 | + <CartesianGrid strokeDasharray="3 3" stroke="var(--color-void-lighter)" horizontal={false} /> |
| 132 | + <XAxis |
| 133 | + type="number" |
| 134 | + tickFormatter={(v: number) => `$${v.toFixed(0)}`} |
| 135 | + tick={{ fill: 'var(--color-text-muted)', fontSize: 11 }} |
| 136 | + stroke="var(--color-void-lighter)" |
| 137 | + /> |
| 138 | + <YAxis |
| 139 | + type="category" |
| 140 | + dataKey="model" |
| 141 | + tick={{ fill: 'var(--color-text-muted)', fontSize: 11 }} |
| 142 | + stroke="var(--color-void-lighter)" |
| 143 | + width={120} |
| 144 | + /> |
| 145 | + <Tooltip content={<CustomTooltip />} /> |
| 146 | + <Bar dataKey="cost" radius={[0, 4, 4, 0]} animationDuration={500}> |
| 147 | + {chartData.map((entry) => ( |
| 148 | + <Cell |
| 149 | + key={entry.model} |
| 150 | + fill={MODEL_COLORS[entry.model] ?? MODEL_COLORS.other} |
| 151 | + /> |
| 152 | + ))} |
| 153 | + </Bar> |
| 154 | + </BarChart> |
| 155 | + )} |
| 156 | + </ChartFrame> |
| 157 | + </section> |
| 158 | + ); |
| 159 | +} |
0 commit comments