File size: 31,287 Bytes
53a4f20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2eb7f97
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>DataCanvas Explorer - CSV Visualization</title>
    <link rel="icon" type="image/x-icon" href="/static/favicon.ico">
    <script src="https://cdn.tailwindcss.com"></script>
    <script src="https://cdn.jsdelivr.net/npm/feather-icons/dist/feather.min.js"></script>
    <script src="https://unpkg.com/feather-icons"></script>
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/papaparse@5.4.1/papaparse.min.js"></script>
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    colors: {
                        primary: {
                            50: '#f0f9ff',
                            100: '#e0f2fe',
                            200: '#bae6fd',
                            300: '#7dd3fc',
                            400: '#38bdf8',
                            500: '#0ea5e9',
                            600: '#0284c7',
                            700: '#0369a1',
                            800: '#075985',
                            900: '#0c4a6e',
                        },
                        secondary: {
                            50: '#fefce8',
                            100: '#fef9c3',
                            200: '#fef08a',
                            300: '#fde047',
                            400: '#facc15',
                            500: '#eab308',
                            600: '#ca8a04',
                            700: '#a16207',
                            800: '#854d0e',
                            900: '#713f12',
                        }
                    }
                }
            }
        }
    </script>
    <style>
        .glass-effect {
            background: rgba(255, 255, 255, 0.1);
            backdrop-filter: blur(10px);
            border: 1px solid rgba(255, 255, 255, 0.2);
        }
        .fade-in {
            animation: fadeIn 0.8s ease-in;
        }
        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(20px); }
            to { opacity: 1; transform: translateY(0); }
        }
        .table-container {
            max-height: 500px;
            overflow-y: auto;
        }
        .chart-container {
            position: relative;
            height: 300px;
            width: 100%;
        }
    </style>
</head>
<body class="bg-gradient-to-br from-primary-50 to-primary-100 min-h-screen">
    <!-- Header -->
    <header class="bg-white shadow-sm sticky top-0 z-50">
        <div class="container mx-auto px-4 py-4">
            <div class="flex items-center justify-between">
                <div class="flex items-center space-x-3">
                    <div class="bg-primary-500 p-2 rounded-lg">
                        <i data-feather="bar-chart-2" class="text-white w-6 h-6"></i>
                    </div>
                    <h1 class="text-2xl font-bold text-gray-800">DataCanvas Explorer</h1>
                </div>
                <div class="flex items-center space-x-4">
                    <button id="themeToggle" class="p-2 rounded-full hover:bg-gray-100 transition-colors">
                        <i data-feather="moon" class="text-gray-600 w-5 h-5"></i>
                    </button>
                </div>
            </div>
        </div>
    </header>

    <!-- Main Content -->
    <main class="container mx-auto px-4 py-8">
        <!-- Upload Section -->
        <section id="uploadSection" class="fade-in">
            <div class="max-w-4xl mx-auto text-center mb-12">
                <div class="glass-effect rounded-2xl p-8 mb-8">
                    <div class="bg-primary-100 w-20 h-20 rounded-full flex items-center justify-center mx-auto mb-6">
                        <i data-feather="upload-cloud" class="text-primary-600 w-10 h-10"></i>
                    </div>
                    <h2 class="text-3xl font-bold text-gray-800 mb-4">Upload Your CSV File</h2>
                    <p class="text-gray-600 mb-6 max-w-2xl mx-auto">
                        Transform your raw data into beautiful, interactive visualizations. 
                        Simply upload your CSV file and let DataCanvas Explorer work its magic.
                    </p>
                    
                    <div class="border-2 border-dashed border-primary-300 rounded-xl p-8 mb-6 bg-white">
                        <input type="file" id="csvFile" accept=".csv" class="hidden">
                        <div id="dropZone" class="cursor-pointer">
                            <i data-feather="file" class="text-primary-400 w-12 h-12 mx-auto mb-4"></i>
                            <p class="text-gray-600 mb-2">Drag & drop your CSV file here</p>
                            <p class="text-sm text-gray-500 mb-4">or</p>
                            <button id="browseButton" class="bg-primary-500 hover:bg-primary-600 text-white px-6 py-3 rounded-lg font-medium transition-colors">
                                Browse Files
                            </button>
                        </div>
                        <p id="fileName" class="text-sm text-gray-500 mt-4 hidden"></p>
                    </div>
                    
                    <div class="grid grid-cols-1 md:grid-cols-3 gap-4 text-left max-w-2xl mx-auto">
                        <div class="flex items-start space-x-3">
                            <div class="bg-secondary-100 p-2 rounded-lg mt-1">
                                <i data-feather="check" class="text-secondary-600 w-4 h-4"></i>
                            </div>
                            <div>
                                <h4 class="font-semibold text-gray-800">Smart Analysis</h4>
                                <p class="text-sm text-gray-600">Automatic data type detection and cleaning</p>
                            </div>
                        </div>
                        <div class="flex items-start space-x-3">
                            <div class="bg-secondary-100 p-2 rounded-lg mt-1">
                                <i data-feather="bar-chart" class="text-secondary-600 w-4 h-4"></i>
                            </div>
                            <div>
                                <h4 class="font-semibold text-gray-800">Multiple Visualizations</h4>
                                <p class="text-sm text-gray-600">Choose from various chart types and styles</p>
                            </div>
                        </div>
                        <div class="flex items-start space-x-3">
                            <div class="bg-secondary-100 p-2 rounded-lg mt-1">
                                <i data-feather="download" class="text-secondary-600 w-4 h-4"></i>
                            </div>
                            <div>
                                <h4 class="font-semibold text-gray-800">Export Ready</h4>
                                <p class="text-sm text-gray-600">Download your visualizations in high quality</p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Results Section (Hidden initially) -->
        <section id="resultsSection" class="hidden fade-in">
            <!-- Data Overview Cards -->
            <div class="grid grid-cols-1 md:grid-cols-4 gap-6 mb-8">
                <div class="bg-white rounded-xl p-6 shadow-sm border border-gray-100">
                    <div class="flex items-center justify-between mb-4">
                        <h3 class="text-sm font-medium text-gray-500">Total Rows</h3>
                        <i data-feather="database" class="text-primary-500 w-5 h-5"></i>
                    </div>
                    <p id="totalRows" class="text-3xl font-bold text-gray-800">0</p>
                </div>
                <div class="bg-white rounded-xl p-6 shadow-sm border border-gray-100">
                    <div class="flex items-center justify-between mb-4">
                        <h3 class="text-sm font-medium text-gray-500">Total Columns</h3>
                        <i data-feather="columns" class="text-primary-500 w-5 h-5"></i>
                    </div>
                    <p id="totalColumns" class="text-3xl font-bold text-gray-800">0</p>
                </div>
                <div class="bg-white rounded-xl p-6 shadow-sm border border-gray-100">
                    <div class="flex items-center justify-between mb-4">
                        <h3 class="text-sm font-medium text-gray-500">Numeric Columns</h3>
                        <i data-feather="hash" class="text-primary-500 w-5 h-5"></i>
                    </div>
                    <p id="numericColumns" class="text-3xl font-bold text-gray-800">0</p>
                </div>
                <div class="bg-white rounded-xl p-6 shadow-sm border border-gray-100">
                    <div class="flex items-center justify-between mb-4">
                        <h3 class="text-sm font-medium text-gray-500">Text Columns</h3>
                        <i data-feather="type" class="text-primary-500 w-5 h-5"></i>
                    </div>
                    <p id="textColumns" class="text-3xl font-bold text-gray-800">0</p>
                </div>
            </div>

            <div class="grid grid-cols-1 lg:grid-cols-2 gap-8 mb-8">
                <!-- Data Table -->
                <div class="bg-white rounded-xl shadow-sm border border-gray-100">
                    <div class="p-6 border-b border-gray-100">
                        <div class="flex items-center justify-between">
                            <h2 class="text-xl font-semibold text-gray-800">Data Table</h2>
                            <div class="flex space-x-2">
                                <button id="exportTable" class="bg-primary-500 hover:bg-primary-600 text-white px-4 py-2 rounded-lg text-sm font-medium transition-colors">
                                <i data-feather="download" class="w-4 h-4 inline mr-2"></i>
                                Export
                            </button>
                            <button id="toggleFilters" class="bg-gray-100 hover:bg-gray-200 text-gray-700 px-4 py-2 rounded-lg text-sm font-medium transition-colors">
                                <i data-feather="filter" class="w-4 h-4 inline mr-2"></i>
                                Filters
                            </button>
                        </div>
                    </div>
                    <div class="table-container p-4">
                        <table id="dataTable" class="min-w-full divide-y divide-gray-200">
                            <thead class="bg-gray-50">
                                <tr id="tableHeader"></tr>
                            </thead>
                            <tbody id="tableBody" class="bg-white divide-y divide-gray-200"></tbody>
                        </table>
                    </div>
                </div>

                <!-- Chart Selection & Controls -->
                <div class="bg-white rounded-xl shadow-sm border border-gray-100">
                    <div class="p-6 border-b border-gray-100">
                        <h2 class="text-xl font-semibold text-gray-800">Visualizations</h2>
                    </div>
                    <div class="p-6">
                        <div class="mb-6">
                            <label class="block text-sm font-medium text-gray-700 mb-2">Chart Type</label>
                            <select id="chartType" class="w-full border border-gray-300 rounded-lg px-4 py-2 focus:ring-2 focus:ring-primary-500 focus:border-primary-500">
                                <option value="bar">Bar Chart</option>
                                <option value="line">Line Chart</option>
                                <option value="pie">Pie Chart</option>
                                <option value="scatter">Scatter Plot</option>
                                <option value="area">Area Chart</option>
                            </select>
                        </div>
                        
                        <div class="grid grid-cols-2 gap-4 mb-6">
                            <div>
                                <label class="block text-sm font-medium text-gray-700 mb-2">X-Axis</label>
                                <select id="xAxis" class="w-full border border-gray-300 rounded-lg px-4 py-2 focus:ring-2 focus:ring-primary-500 focus:border-primary-500"></select>
                            </div>
                            <div>
                                <label class="block text-sm font-medium text-gray-700 mb-2">Y-Axis</label>
                                <select id="yAxis" class="w-full border border-gray-300 rounded-lg px-4 py-2 focus:ring-2 focus:ring-primary-500 focus:border-primary-500"></select>
                            </div>
                        </div>

                        <div class="chart-container">
                            <canvas id="dataChart"></canvas>
                        </div>

                        <div class="flex justify-between items-center mt-6">
                            <button id="downloadChart" class="bg-secondary-500 hover:bg-secondary-600 text-white px-4 py-2 rounded-lg text-sm font-medium transition-colors">
                                <i data-feather="download" class="w-4 h-4 inline mr-2"></i>
                                Download Chart
                            </button>
                            <button id="newAnalysis" class="bg-gray-100 hover:bg-gray-200 text-gray-700 px-4 py-2 rounded-lg text-sm font-medium transition-colors">
                                <i data-feather="refresh-cw" class="w-4 h-4 inline mr-2"></i>
                                New Analysis
                            </button>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Statistics & Insights -->
            <div class="bg-white rounded-xl shadow-sm border border-gray-100 mb-8">
                <div class="p-6 border-b border-gray-100">
                    <h2 class="text-xl font-semibold text-gray-800">Data Insights</h2>
                </div>
                <div class="p-6">
                    <div id="insightsContent" class="space-y-4">
                        <!-- Insights will be populated here -->
                    </div>
                </div>
            </div>
        </section>
    </main>

    <!-- Footer -->
    <footer class="bg-white border-t border-gray-200 mt-16">
        <div class="container mx-auto px-4 py-8">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="flex items-center space-x-3 mb-4 md:mb-0">
                <div class="bg-primary-500 p-2 rounded-lg">
                    <i data-feather="bar-chart-2" class="text-white w-5 h-5"></i>
            </div>
            <p class="text-gray-600 text-sm">DataCanvas Explorer - Transform your data into beautiful stories</p>
        </div>
    </footer>

    <script>
        // Initialize Feather Icons
        feather.replace();

        // DOM Elements
        const csvFileInput = document.getElementById('csvFile');
        const dropZone = document.getElementById('dropZone');
        const browseButton = document.getElementById('browseButton');
        const fileName = document.getElementById('fileName');
        const uploadSection = document.getElementById('uploadSection');
        const resultsSection = document.getElementById('resultsSection');
        const dataTable = document.getElementById('dataTable');
        const tableHeader = document.getElementById('tableHeader');
        const tableBody = document.getElementById('tableBody');
        const totalRows = document.getElementById('totalRows');
        const totalColumns = document.getElementById('totalColumns');
        const numericColumns = document.getElementById('numericColumns');
        const textColumns = document.getElementById('textColumns');
        const chartType = document.getElementById('chartType');
        const xAxis = document.getElementById('xAxis');
        const yAxis = document.getElementById('yAxis');
        const dataChart = document.getElementById('dataChart');
        const downloadChart = document.getElementById('downloadChart');
        const newAnalysis = document.getElementById('newAnalysis');
        const exportTable = document.getElementById('exportTable');
        const toggleFilters = document.getElementById('toggleFilters');
        const insightsContent = document.getElementById('insightsContent');
        const themeToggle = document.getElementById('themeToggle');

        let parsedData = null;
        let currentChart = null;

        // Event Listeners
        browseButton.addEventListener('click', () => csvFileInput.click());
        csvFileInput.addEventListener('change', handleFileSelect);
        dropZone.addEventListener('dragover', handleDragOver);
        dropZone.addEventListener('drop', handleFileDrop);
        chartType.addEventListener('change', updateChart);
        xAxis.addEventListener('change', updateChart);
        yAxis.addEventListener('change', updateChart);
        downloadChart.addEventListener('click', downloadChartImage);
        newAnalysis.addEventListener('click', resetAnalysis);
        exportTable.addEventListener('click', exportTableData);
        themeToggle.addEventListener('click', toggleTheme);

        function handleDragOver(e) {
            e.preventDefault();
            dropZone.classList.add('border-primary-500', 'bg-primary-50');
        }

        function handleFileDrop(e) {
            e.preventDefault();
            dropZone.classList.remove('border-primary-500', 'bg-primary-50');
            const files = e.dataTransfer.files;
            if (files.length > 0 && files[0].type === 'text/csv') {
                processCSVFile(files[0]);
            }
        }

        function handleFileSelect(e) {
            const file = e.target.files[0];
            if (file && file.type === 'text/csv') {
                processCSVFile(file);
            }
        }

        function processCSVFile(file) {
            fileName.textContent = `Selected: ${file.name}`;
            fileName.classList.remove('hidden');

            Papa.parse(file, {
                header: true,
                dynamicTyping: true,
                complete: function(results) {
                    if (results.errors.length > 0) {
                        alert('Error parsing CSV file: ' + results.errors[0].message);
                        return;
                    }
                    
                    parsedData = results;
                    displayResults();
                }
            });
        }

        function displayResults() {
            // Hide upload section, show results
            uploadSection.classList.add('hidden');
            resultsSection.classList.remove('hidden');

            // Update overview cards
            totalRows.textContent = parsedData.data.length;
            totalColumns.textContent = parsedData.meta.fields.length;
            
            const numericCols = parsedData.meta.fields.filter(field => 
                parsedData.data.some(row => typeof row[field] === 'number')
            ).length;
            
            numericColumns.textContent = numericCols;
            textColumns.textContent = parsedData.meta.fields.length - numericCols;

            // Populate table
            populateTable();

            // Populate chart controls
            populateChartControls();

            // Generate insights
            generateInsights();

            // Update chart
            updateChart();
        }

        function populateTable() {
            // Clear existing content
            tableHeader.innerHTML = '';
            tableBody.innerHTML = '';

            // Create header
            parsedData.meta.fields.forEach(field => {
                const th = document.createElement('th');
                th.className = 'px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider';
                th.textContent = field;
                tableHeader.appendChild(th);
            });

            // Create rows (limit to first 100 for performance)
            const displayData = parsedData.data.slice(0, 100);
            displayData.forEach((row, index) => {
                const tr = document.createElement('tr');
                tr.className = index % 2 === 0 ? 'bg-white' : 'bg-gray-50';

                parsedData.meta.fields.forEach(field => {
                    const td = document.createElement('td');
                    td.className = 'px-6 py-4 whitespace-nowrap text-sm text-gray-900';
                    td.textContent = row[field] !== null && row[field] !== undefined ? row[field] : '';
                    tr.appendChild(td);
                });

                tableBody.appendChild(tr);
            });
        }

        function populateChartControls() {
            // Clear existing options
            xAxis.innerHTML = '';
            yAxis.innerHTML = '';

            // Add options for both selects
            parsedData.meta.fields.forEach(field => {
                const optionX = document.createElement('option');
                optionX.value = field;
                optionX.textContent = field;
                xAxis.appendChild(optionX);

                const optionY = document.createElement('option');
                optionY.value = field;
                optionY.textContent = field;
                yAxis.appendChild(optionY);
            });

            // Auto-select first numeric column for Y-axis if available
            const numericFields = parsedData.meta.fields.filter(field => 
                parsedData.data.some(row => typeof row[field] === 'number')
            );

            if (numericFields.length > 0) {
                yAxis.value = numericFields[0];
            }
        }

        function updateChart() {
            if (!parsedData) return;

            const xField = xAxis.value;
            const yField = yAxis.value;
            const type = chartType.value;

            // Prepare data for chart
            const labels = parsedData.data.map(row => row[xField]).slice(0, 50);
            const data = parsedData.data.map(row => row[yField]).slice(0, 50);

            // Destroy existing chart
            if (currentChart) {
                currentChart.destroy();
            }

            // Create new chart
            const ctx = dataChart.getContext('2d');
            currentChart = new Chart(ctx, {
                type: type,
                data: {
                    labels: labels,
                    datasets: [{
                        label: `${yField} by ${xField}`,
                        data: data,
                        backgroundColor: type === 'pie' ? [
                            '#0ea5e9', '#eab308', '#10b981', '#ef4444', '#8b5cf6',
                            '#f97316', '#06b6d4', '#84cc16', '#f59e0b', '#ec4899'
                        ] : '#0ea5e9',
                        borderColor: '#0ea5e9',
                        borderWidth: 2,
                        fill: type === 'area'
                    }]
                },
                options: {
                    responsive: true,
                    maintainAspectRatio: false,
                    plugins: {
                        legend: {
                            position: 'top',
                        },
                        tooltip: {
                            mode: 'index',
                            intersect: false,
                        }
                    },
                    scales: type !== 'pie' ? {
                        x: {
                            display: true,
                            title: {
                                display: true,
                                text: xField
                            }
                        },
                        y: {
                            display: true,
                            title: {
                                display: true,
                                text: yField
                            }
                        }
                    } : {}
                }
            });
        }

        function generateInsights() {
            insightsContent.innerHTML = '';

            // Basic statistics for numeric columns
            const numericFields = parsedData.meta.fields.filter(field => 
                parsedData.data.some(row => typeof row[field] === 'number')
            );

            numericFields.forEach(field => {
                const values = parsedData.data.map(row => row[field]).filter(val => val !== null && val !== undefined);
                
                if (values.length > 0) {
                    const sum = values.reduce((a, b) => a + b, 0);
                    const avg = sum / values.length;
                    const max = Math.max(...values);
                    const min = Math.min(...values);

                    const insightDiv = document.createElement('div');
                    insightDiv.className = 'bg-primary-50 rounded-lg p-4';
                    insightDiv.innerHTML = `
                        <div class="flex items-center justify-between mb-2">
                            <h4 class="font-semibold text-gray-800">${field}</h4>
                            <span class="text-sm text-primary-600 font-medium">Numeric Column</span>
                    </div>
                    <div class="grid grid-cols-2 md:grid-cols-4 gap-4 text-sm">
                            <div>
                                <span class="text-gray-500">Average:</span>
                                <p class="font-semibold">${avg.toFixed(2)}</p>
                            </div>
                            <div>
                                <span class="text-gray-500">Min/Max:</span>
                                <p class="font-semibold">${min.toFixed(2)} / ${max.toFixed(2)}</p>
                            </div>
                            <div>
                                <span class="text-gray-500">Range:</span>
                                <p class="font-semibold">${(max - min).toFixed(2)}</p>
                            </div>
                            <div>
                                <span class="text-gray-500">Count:</span>
                                <p class="font-semibold">${values.length}</p>
                            </div>
                        </div>
                    `;
                    insightsContent.appendChild(insightDiv);
                }
            });

            // Add data quality insights
            const qualityDiv = document.createElement('div');
            qualityDiv.className = 'bg-secondary-50 rounded-lg p-4';
            qualityDiv.innerHTML = `
                <div class="flex items-center justify-between mb-2">
                    <h4 class="font-semibold text-gray-800">Data Quality</h4>
                    <span class="text-sm text-secondary-600 font-medium">Analysis</span>
                </div>
                <div class="space-y-2 text-sm">
                    <div class="flex justify-between">
                        <span class="text-gray-500">Total Records:</span>
                        <span class="font-semibold">${parsedData.data.length}</span>
                    </div>
                    <div class="flex justify-between">
                        <span class="text-gray-500">Complete Records:</span>
                        <span class="font-semibold">${calculateCompleteRecords()}%</span>
                    </div>
                </div>
            `;
            insightsContent.appendChild(qualityDiv);
        }

        function calculateCompleteRecords() {
            if (!parsedData) return 0;
            
            const completeRecords = parsedData.data.filter(row => 
                parsedData.meta.fields.every(field => row[field] !== null && row[field] !== undefined)
            );
            
            return Math.round((completeRecords.length / parsedData.data.length) * 100);
        }

        function downloadChartImage() {
            if (currentChart) {
                const link = document.createElement('a');
                link.download = 'datacanvas-chart.png';
                link.href = dataChart.toDataURL();
                link.click();
            }
        }

        function resetAnalysis() {
            uploadSection.classList.remove('hidden');
            resultsSection.classList.add('hidden');
            csvFileInput.value = '';
            fileName.classList.add('hidden');
            parsedData = null;
        }

        function exportTableData() {
            if (!parsedData) return;

            const csv = Papa.unparse(parsedData.data);
            const blob = new Blob([csv], { type: 'text/csv' });
            const url = window.URL.createObjectURL(blob);
            const link = document.createElement('a');
            link.download = 'datacanvas-export.csv';
            link.href = url;
            link.click();
            window.URL.revokeObjectURL(url);
        }

        function toggleTheme() {
            const body = document.body;
            const isDark = body.classList.contains('dark');
            
            if (isDark) {
                body.classList.remove('dark');
                body.classList.add('bg-gradient-to-br', 'from-primary-50', 'to-primary-100');
                themeToggle.innerHTML = '<i data-feather="moon" class="text-gray-600 w-5 h-5"></i>';
            } else {
                body.classList.add('dark');
                body.classList.remove('bg-gradient-to-br', 'from-primary-50', 'to-primary-100');
                themeToggle.innerHTML = '<i data-feather="sun" class="text-gray-600 w-5 h-5"></i>';
            }
            feather.replace();
        }

        // Add dark mode styles
        const darkModeStyles = `
            <style>
                .dark body {
                    background: #1f2937;
                    color: #f9fafb;
                }
                .dark .bg-white {
                    background-color: #374151;
                    border-color: #4b5563;
                }
                .dark .text-gray-800 {
                    color: #f9fafb;
                }
                .dark .text-gray-600 {
                    color: #d1d5db;
                }
                .dark .text-gray-500 {
                    color: #9ca3af;
                }
                .dark .bg-gray-100 {
                    background-color: #4b5563;
                }
                .dark .border-gray-200,
                .dark .border-gray-100 {
                    border-color: #4b5563;
                }
                .dark .divide-gray-200 > * + * {
                    border-color: #4b5563;
                }
            </style>
        `;
        document.head.insertAdjacentHTML('beforeend', darkModeStyles);
    </script>
</body>
</html>