|
1 | | - |
2 | 1 | import React from 'react'; |
3 | | -import { Chart as ChartJS, CategoryScale, LinearScale, BarElement, Title, Tooltip, Legend, ArcElement, PointElement, LineElement, PolarAreaController, RadarController, DoughnutController, PieController, BarController, LineController, ScatterController, BubbleController } from 'chart.js'; |
| 2 | +import { |
| 3 | + Chart as ChartJS, |
| 4 | + CategoryScale, LinearScale, BarElement, |
| 5 | + Title, Tooltip, Legend, ArcElement, |
| 6 | + PointElement, LineElement, |
| 7 | + PolarAreaController, RadarController, |
| 8 | + DoughnutController, PieController, |
| 9 | + BarController, LineController, |
| 10 | + ScatterController, BubbleController, |
| 11 | +} from 'chart.js'; |
4 | 12 | import { Chart } from 'react-chartjs-2'; |
5 | 13 | import { AnalysisResult } from '../types'; |
6 | | -import { AiSparkleIcon } from './icons/material-icons-imports'; |
7 | 14 | import { useTheme } from '../contexts/ThemeContext'; |
8 | 15 |
|
9 | | -// Register all necessary Chart.js components |
10 | 16 | ChartJS.register( |
11 | | - CategoryScale, LinearScale, BarElement, Title, Tooltip, Legend, ArcElement, PointElement, LineElement, |
12 | | - PolarAreaController, RadarController, DoughnutController, PieController, BarController, LineController, |
13 | | - ScatterController, BubbleController |
| 17 | + CategoryScale, LinearScale, BarElement, |
| 18 | + Title, Tooltip, Legend, ArcElement, |
| 19 | + PointElement, LineElement, |
| 20 | + PolarAreaController, RadarController, |
| 21 | + DoughnutController, PieController, |
| 22 | + BarController, LineController, |
| 23 | + ScatterController, BubbleController, |
14 | 24 | ); |
15 | 25 |
|
16 | 26 | interface AnalysisResultDisplayProps { |
17 | | - result: AnalysisResult; |
| 27 | + result: AnalysisResult; |
18 | 28 | } |
19 | 29 |
|
| 30 | +const SparkleIcon = () => ( |
| 31 | + <svg width="13" height="13" viewBox="0 0 16 16" fill="none" stroke="var(--accent)" strokeWidth="1.5" strokeLinecap="round"> |
| 32 | + <path d="M8 1v3M8 12v3M1 8h3M12 8h3M3.05 3.05l2.12 2.12M10.83 10.83l2.12 2.12M3.05 12.95l2.12-2.12M10.83 5.17l2.12-2.12" /> |
| 33 | + </svg> |
| 34 | +); |
| 35 | + |
| 36 | +const CHART_TYPE_LABELS: Record<string, string> = { |
| 37 | + bar: 'Bar chart', |
| 38 | + line: 'Line chart', |
| 39 | + pie: 'Pie chart', |
| 40 | + doughnut: 'Doughnut chart', |
| 41 | + polarArea: 'Polar area', |
| 42 | + radar: 'Radar chart', |
| 43 | + scatter: 'Scatter plot', |
| 44 | + bubble: 'Bubble chart', |
| 45 | +}; |
| 46 | + |
20 | 47 | const AnalysisResultDisplay: React.FC<AnalysisResultDisplayProps> = ({ result }) => { |
21 | | - const { theme } = useTheme(); |
| 48 | + const { theme } = useTheme(); |
| 49 | + |
| 50 | + const themedChartOptions = React.useMemo(() => { |
| 51 | + const isDark = theme === 'dark'; |
| 52 | + const textColor = isDark ? 'rgba(255,255,255,0.55)' : 'rgba(0,0,0,0.45)'; |
| 53 | + const gridColor = isDark ? 'rgba(255,255,255,0.07)' : 'rgba(0,0,0,0.07)'; |
| 54 | + const base = result.chartOptions || {}; |
22 | 55 |
|
23 | | - // Dynamically adjust chart options for dark/light theme |
24 | | - const themedChartOptions = React.useMemo(() => { |
25 | | - const isDark = theme === 'dark'; |
26 | | - const textColor = isDark ? '#cbd5e1' : '#475569'; // slate-300 : slate-600 |
27 | | - const gridColor = isDark ? 'rgba(255, 255, 255, 0.1)' : 'rgba(0, 0, 0, 0.1)'; |
| 56 | + return { |
| 57 | + ...base, |
| 58 | + responsive: true, |
| 59 | + maintainAspectRatio: false, |
| 60 | + plugins: { |
| 61 | + ...base.plugins, |
| 62 | + legend: { |
| 63 | + ...base.plugins?.legend, |
| 64 | + labels: { |
| 65 | + ...base.plugins?.legend?.labels, |
| 66 | + color: textColor, |
| 67 | + font: { family: 'Geist, sans-serif', size: 11 }, |
| 68 | + boxWidth: 10, |
| 69 | + padding: 14, |
| 70 | + }, |
| 71 | + }, |
| 72 | + title: { ...base.plugins?.title, color: textColor }, |
| 73 | + tooltip: { |
| 74 | + ...base.plugins?.tooltip, |
| 75 | + backgroundColor: isDark ? 'rgba(30,30,36,0.95)' : 'rgba(255,255,255,0.98)', |
| 76 | + titleColor: isDark ? '#e2e8f0' : '#1e293b', |
| 77 | + bodyColor: isDark ? '#94a3b8' : '#475569', |
| 78 | + borderColor: isDark ? 'rgba(255,255,255,0.1)' : 'rgba(0,0,0,0.08)', |
| 79 | + borderWidth: 1, |
| 80 | + cornerRadius: 8, |
| 81 | + padding: 10, |
| 82 | + titleFont: { family: 'Geist, sans-serif', size: 12, weight: '600' }, |
| 83 | + bodyFont: { family: 'Geist Mono, monospace', size: 11 }, |
| 84 | + }, |
| 85 | + }, |
| 86 | + scales: { |
| 87 | + x: { |
| 88 | + ...base.scales?.x, |
| 89 | + ticks: { ...base.scales?.x?.ticks, color: textColor, font: { family: 'Geist Mono, monospace', size: 10 } }, |
| 90 | + grid: { ...base.scales?.x?.grid, color: gridColor }, |
| 91 | + border: { color: gridColor }, |
| 92 | + title: { ...base.scales?.x?.title, color: textColor }, |
| 93 | + }, |
| 94 | + y: { |
| 95 | + ...base.scales?.y, |
| 96 | + ticks: { ...base.scales?.y?.ticks, color: textColor, font: { family: 'Geist Mono, monospace', size: 10 } }, |
| 97 | + grid: { ...base.scales?.y?.grid, color: gridColor }, |
| 98 | + border: { color: gridColor }, |
| 99 | + title: { ...base.scales?.y?.title, color: textColor }, |
| 100 | + }, |
| 101 | + }, |
| 102 | + }; |
| 103 | + }, [result.chartOptions, theme]); |
28 | 104 |
|
29 | | - // Deep merge our theme options into the AI-provided options |
30 | | - const baseOptions = result.chartOptions || {}; |
31 | | - |
32 | | - const themeOverrides = { |
33 | | - plugins: { |
34 | | - legend: { |
35 | | - labels: { color: textColor } |
36 | | - }, |
37 | | - title: { |
38 | | - color: textColor |
39 | | - } |
40 | | - }, |
41 | | - scales: { |
42 | | - x: { |
43 | | - ...baseOptions.scales?.x, |
44 | | - ticks: { ...baseOptions.scales?.x?.ticks, color: textColor }, |
45 | | - grid: { ...baseOptions.scales?.x?.grid, color: gridColor }, |
46 | | - title: { ...baseOptions.scales?.x?.title, color: textColor } |
47 | | - }, |
48 | | - y: { |
49 | | - ...baseOptions.scales?.y, |
50 | | - ticks: { ...baseOptions.scales?.y?.ticks, color: textColor }, |
51 | | - grid: { ...baseOptions.scales?.y?.grid, color: gridColor }, |
52 | | - title: { ...baseOptions.scales?.y?.title, color: textColor } |
53 | | - } |
54 | | - } |
55 | | - }; |
56 | | - |
57 | | - // A simple deep merge |
58 | | - return { |
59 | | - ...baseOptions, |
60 | | - ...themeOverrides, |
61 | | - plugins: { ...baseOptions.plugins, ...themeOverrides.plugins }, |
62 | | - scales: { ...baseOptions.scales, ...themeOverrides.scales } |
63 | | - }; |
| 105 | + const chartLabel = CHART_TYPE_LABELS[result.chartType] ?? result.chartType; |
64 | 106 |
|
65 | | - }, [result.chartOptions, theme]); |
| 107 | + return ( |
| 108 | + <div |
| 109 | + className="qa-card" |
| 110 | + style={{ padding: 0, overflow: 'hidden', animation: 'fadeIn 0.25s' }} |
| 111 | + > |
| 112 | + {/* Header — matches QueryResult card header */} |
| 113 | + <div style={{ |
| 114 | + display: 'flex', alignItems: 'center', gap: 10, |
| 115 | + padding: '9px 14px', borderBottom: '1px solid var(--border)', flexShrink: 0, |
| 116 | + }}> |
| 117 | + <span style={{ |
| 118 | + width: 24, height: 24, borderRadius: 6, flexShrink: 0, |
| 119 | + background: 'var(--accent-soft)', |
| 120 | + display: 'flex', alignItems: 'center', justifyContent: 'center', |
| 121 | + }}> |
| 122 | + <SparkleIcon /> |
| 123 | + </span> |
| 124 | + <span style={{ fontSize: 13, fontWeight: 500, color: 'var(--fg)', fontFamily: 'var(--font-body)' }}> |
| 125 | + AI Analysis |
| 126 | + </span> |
| 127 | + <span className="qa-chip accent" style={{ textTransform: 'capitalize', marginLeft: 2 }}> |
| 128 | + {chartLabel} |
| 129 | + </span> |
| 130 | + </div> |
| 131 | + |
| 132 | + {/* Body — insight + chart side by side */} |
| 133 | + <div style={{ display: 'flex', minHeight: 320 }}> |
| 134 | + {/* Insight panel */} |
| 135 | + <div style={{ |
| 136 | + width: 220, flexShrink: 0, |
| 137 | + padding: '16px 18px', |
| 138 | + borderRight: '1px solid var(--border)', |
| 139 | + display: 'flex', flexDirection: 'column', gap: 10, |
| 140 | + background: 'var(--soft)', |
| 141 | + }}> |
| 142 | + <div style={{ |
| 143 | + fontSize: 10.5, fontWeight: 500, |
| 144 | + textTransform: 'uppercase', letterSpacing: '0.08em', |
| 145 | + color: 'var(--muted)', fontFamily: 'var(--font-body)', |
| 146 | + }}> |
| 147 | + Insight |
| 148 | + </div> |
| 149 | + <p style={{ |
| 150 | + fontSize: 13, color: 'var(--fg)', lineHeight: 1.65, |
| 151 | + fontFamily: 'var(--font-body)', margin: 0, |
| 152 | + }}> |
| 153 | + {result.insight} |
| 154 | + </p> |
| 155 | + </div> |
66 | 156 |
|
67 | | - return ( |
68 | | - <div className="space-y-4 animate-fade-in"> |
69 | | - <h3 className="text-sm font-semibold text-slate-600 dark:text-slate-400 uppercase tracking-wider">AI Analysis</h3> |
70 | | - <div className="bg-white dark:bg-slate-800/50 border border-slate-200 dark:border-slate-700 rounded-lg p-6 flex flex-col md:flex-row gap-6"> |
71 | | - {/* Insight Text */} |
72 | | - <div className="md:w-1/3 space-y-4"> |
73 | | - <div className="flex items-center gap-3"> |
74 | | - <AiSparkleIcon className="w-7 h-7 text-blue-500 flex-shrink-0" /> |
75 | | - <h4 className="text-lg font-bold text-slate-800 dark:text-slate-200">Insight</h4> |
76 | | - </div> |
77 | | - <p className="text-slate-600 dark:text-slate-300 text-sm leading-relaxed"> |
78 | | - {result.insight} |
79 | | - </p> |
80 | | - </div> |
81 | | - {/* Chart */} |
82 | | - <div className="md:w-2/3 min-h-[300px]"> |
83 | | - <Chart |
84 | | - type={result.chartType} |
85 | | - data={result.chartData} |
86 | | - options={themedChartOptions} |
87 | | - /> |
88 | | - </div> |
89 | | - </div> |
| 157 | + {/* Chart panel */} |
| 158 | + <div style={{ flex: 1, padding: '20px 24px', position: 'relative', minWidth: 0 }}> |
| 159 | + <Chart |
| 160 | + type={result.chartType} |
| 161 | + data={result.chartData} |
| 162 | + options={themedChartOptions} |
| 163 | + /> |
90 | 164 | </div> |
91 | | - ); |
| 165 | + </div> |
| 166 | + </div> |
| 167 | + ); |
92 | 168 | }; |
93 | 169 |
|
94 | 170 | export default AnalysisResultDisplay; |
0 commit comments