File size: 22,758 Bytes
b2ca876
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
/**
 * Advanced Chart Visualizations
 * Provides different chart types for international trade data visualization
 */

const TradeCharts = (function() {
  // Store chart instances to destroy when needed
  const chartInstances = {};
  
  // Color schemes for different chart types
  const colorSchemes = {
    default: [
      'rgba(25, 118, 210, 0.7)',   // Primary blue
      'rgba(229, 57, 53, 0.7)',    // Red
      'rgba(67, 160, 71, 0.7)',    // Green
      'rgba(251, 192, 45, 0.7)',   // Yellow
      'rgba(156, 39, 176, 0.7)',   // Purple
      'rgba(0, 188, 212, 0.7)',    // Cyan
      'rgba(255, 152, 0, 0.7)',    // Orange
      'rgba(121, 85, 72, 0.7)',    // Brown
      'rgba(96, 125, 139, 0.7)',   // Blue Grey
      'rgba(233, 30, 99, 0.7)'     // Pink
    ],
    borders: [
      'rgba(25, 118, 210, 1)',     // Primary blue
      'rgba(229, 57, 53, 1)',      // Red
      'rgba(67, 160, 71, 1)',      // Green
      'rgba(251, 192, 45, 1)',     // Yellow
      'rgba(156, 39, 176, 1)',     // Purple
      'rgba(0, 188, 212, 1)',      // Cyan
      'rgba(255, 152, 0, 1)',      // Orange
      'rgba(121, 85, 72, 1)',      // Brown
      'rgba(96, 125, 139, 1)',     // Blue Grey
      'rgba(233, 30, 99, 1)'       // Pink
    ],
    gradients: function(ctx) {
      return [
        createGradient(ctx, [25, 118, 210]),
        createGradient(ctx, [229, 57, 53]),
        createGradient(ctx, [67, 160, 71]),
        createGradient(ctx, [251, 192, 45]),
        createGradient(ctx, [156, 39, 176]),
        createGradient(ctx, [0, 188, 212]),
        createGradient(ctx, [255, 152, 0]),
        createGradient(ctx, [121, 85, 72]),
        createGradient(ctx, [96, 125, 139]),
        createGradient(ctx, [233, 30, 99])
      ];
    }
  };
  
  // Create a gradient color
  function createGradient(ctx, rgbColor) {
    const gradient = ctx.createLinearGradient(0, 0, 0, 400);
    gradient.addColorStop(0, `rgba(${rgbColor[0]}, ${rgbColor[1]}, ${rgbColor[2]}, 0.8)`);
    gradient.addColorStop(1, `rgba(${rgbColor[0]}, ${rgbColor[1]}, ${rgbColor[2]}, 0.2)`);
    return gradient;
  }
  
  // Load Chart.js if not present
  function ensureChartJsLoaded() {
    return new Promise((resolve, reject) => {
      if (window.Chart) {
        resolve(window.Chart);
        return;
      }
      
      const script = document.createElement('script');
      script.src = 'https://cdn.jsdelivr.net/npm/chart.js@3.9.1/dist/chart.min.js';
      script.onload = () => resolve(window.Chart);
      script.onerror = () => reject(new Error('Failed to load Chart.js'));
      document.body.appendChild(script);
    });
  }
  
  // Helper function to destroy existing chart
  function destroyChart(containerId) {
    if (chartInstances[containerId]) {
      chartInstances[containerId].destroy();
      delete chartInstances[containerId];
    }
  }
  
  // Helper to extract top N items
  function getTopItems(data, valueField, labelField, n = 10) {
    return [...data]
      .sort((a, b) => b[valueField] - a[valueField])
      .slice(0, n)
      .map(item => ({
        value: item[valueField],
        label: item[labelField]
      }));
  }
  
  // Create bar chart
  async function createBarChart(containerId, data, options = {}) {
    await ensureChartJsLoaded();
    
    const container = document.getElementById(containerId);
    if (!container) return null;
    
    // Create or get canvas
    let canvas = container.querySelector('canvas');
    if (!canvas) {
      canvas = document.createElement('canvas');
      container.innerHTML = '';
      container.appendChild(canvas);
    }
    
    destroyChart(containerId);
    
    // Default options
    const defaults = {
      valueField: 'value',
      labelField: 'country',
      title: 'Trade Data',
      horizontal: false,
      limit: 10,
      showLegend: false,
      animation: true
    };
    
    const chartOptions = { ...defaults, ...options };
    
    // Prepare data - limit to top N items and process data
    let chartData;
    if (Array.isArray(data.rows)) {
      chartData = getTopItems(
        data.rows, 
        chartOptions.valueField, 
        chartOptions.labelField, 
        chartOptions.limit
      );
    } else if (Array.isArray(data)) {
      chartData = getTopItems(
        data, 
        chartOptions.valueField, 
        chartOptions.labelField, 
        chartOptions.limit
      );
    } else {
      console.error('Invalid data format for bar chart');
      return null;
    }
    
    // Get context
    const ctx = canvas.getContext('2d');
    
    // Create chart
    chartInstances[containerId] = new Chart(ctx, {
      type: chartOptions.horizontal ? 'horizontalBar' : 'bar',
      data: {
        labels: chartData.map(item => item.label),
        datasets: [{
          label: chartOptions.title,
          data: chartData.map(item => item.value),
          backgroundColor: colorSchemes.default,
          borderColor: colorSchemes.borders,
          borderWidth: 1
        }]
      },
      options: {
        indexAxis: chartOptions.horizontal ? 'y' : 'x',
        responsive: true,
        maintainAspectRatio: false,
        plugins: {
          legend: {
            display: chartOptions.showLegend
          },
          title: {
            display: true,
            text: chartOptions.title,
            font: {
              size: 16
            }
          },
          tooltip: {
            callbacks: {
              label: function(context) {
                let label = context.dataset.label || '';
                if (label) {
                  label += ': ';
                }
                if (context.parsed.y !== null) {
                  label += new Intl.NumberFormat().format(
                    chartOptions.horizontal ? context.parsed.x : context.parsed.y
                  );
                }
                return label;
              }
            }
          }
        },
        animation: chartOptions.animation,
        scales: {
          y: {
            beginAtZero: true,
            ticks: {
              callback: function(value) {
                if (value >= 1000000000) {
                  return (value / 1000000000).toFixed(1) + 'B';
                } else if (value >= 1000000) {
                  return (value / 1000000).toFixed(1) + 'M';
                } else if (value >= 1000) {
                  return (value / 1000).toFixed(1) + 'K';
                }
                return value;
              }
            }
          }
        }
      }
    });
    
    return chartInstances[containerId];
  }
  
  // Create pie chart
  async function createPieChart(containerId, data, options = {}) {
    await ensureChartJsLoaded();
    
    const container = document.getElementById(containerId);
    if (!container) return null;
    
    // Create or get canvas
    let canvas = container.querySelector('canvas');
    if (!canvas) {
      canvas = document.createElement('canvas');
      container.innerHTML = '';
      container.appendChild(canvas);
    }
    
    destroyChart(containerId);
    
    // Default options
    const defaults = {
      valueField: 'value',
      labelField: 'country',
      title: 'Trade Distribution',
      limit: 10,
      showLegend: true,
      animation: true,
      doughnut: false
    };
    
    const chartOptions = { ...defaults, ...options };
    
    // Prepare data - limit to top N items
    let chartData;
    if (Array.isArray(data.rows)) {
      chartData = getTopItems(
        data.rows, 
        chartOptions.valueField, 
        chartOptions.labelField, 
        chartOptions.limit
      );
    } else if (Array.isArray(data)) {
      chartData = getTopItems(
        data, 
        chartOptions.valueField, 
        chartOptions.labelField, 
        chartOptions.limit
      );
    } else {
      console.error('Invalid data format for pie chart');
      return null;
    }
    
    // Get context
    const ctx = canvas.getContext('2d');
    
    // Create chart
    chartInstances[containerId] = new Chart(ctx, {
      type: chartOptions.doughnut ? 'doughnut' : 'pie',
      data: {
        labels: chartData.map(item => item.label),
        datasets: [{
          data: chartData.map(item => item.value),
          backgroundColor: colorSchemes.default,
          borderColor: colorSchemes.borders,
          borderWidth: 1
        }]
      },
      options: {
        responsive: true,
        maintainAspectRatio: false,
        plugins: {
          legend: {
            display: chartOptions.showLegend,
            position: 'right'
          },
          title: {
            display: true,
            text: chartOptions.title,
            font: {
              size: 16
            }
          },
          tooltip: {
            callbacks: {
              label: function(context) {
                const label = context.label || '';
                const value = context.raw;
                const total = context.dataset.data.reduce((a, b) => a + b, 0);
                const percentage = ((value / total) * 100).toFixed(1);
                return `${label}: ${new Intl.NumberFormat().format(value)} (${percentage}%)`;
              }
            }
          }
        },
        animation: chartOptions.animation
      }
    });
    
    return chartInstances[containerId];
  }
  
  // Create line chart
  async function createLineChart(containerId, data, options = {}) {
    await ensureChartJsLoaded();
    
    const container = document.getElementById(containerId);
    if (!container) return null;
    
    // Create or get canvas
    let canvas = container.querySelector('canvas');
    if (!canvas) {
      canvas = document.createElement('canvas');
      container.innerHTML = '';
      container.appendChild(canvas);
    }
    
    destroyChart(containerId);
    
    // Default options
    const defaults = {
      valueField: 'value',
      labelField: 'year',
      title: 'Trade Trends',
      showLegend: true,
      animation: true,
      fill: true,
      seriesField: null, // If provided, creates multiple series based on this field
      timeScale: false
    };
    
    const chartOptions = { ...defaults, ...options };
    
    // Get context
    const ctx = canvas.getContext('2d');
    
    // Prepare datasets
    let datasets = [];
    
    if (chartOptions.seriesField) {
      // Group data by series field
      const seriesData = {};
      const sourceData = Array.isArray(data.rows) ? data.rows : data;
      
      sourceData.forEach(item => {
        const seriesKey = item[chartOptions.seriesField];
        if (!seriesData[seriesKey]) {
          seriesData[seriesKey] = [];
        }
        seriesData[seriesKey].push({
          x: item[chartOptions.labelField],
          y: item[chartOptions.valueField]
        });
      });
      
      // Create a dataset for each series
      let colorIndex = 0;
      for (const seriesKey in seriesData) {
        datasets.push({
          label: seriesKey,
          data: seriesData[seriesKey],
          backgroundColor: chartOptions.fill ? colorSchemes.gradients(ctx)[colorIndex % 10] : colorSchemes.default[colorIndex % 10],
          borderColor: colorSchemes.borders[colorIndex % 10],
          borderWidth: 2,
          fill: chartOptions.fill,
          tension: 0.1
        });
        colorIndex++;
      }
    } else {
      // Single series
      const sourceData = Array.isArray(data.rows) ? data.rows : data;
      
      // Sort data by label (typically year)
      sourceData.sort((a, b) => {
        if (chartOptions.timeScale) {
          return new Date(a[chartOptions.labelField]) - new Date(b[chartOptions.labelField]);
        }
        return a[chartOptions.labelField] - b[chartOptions.labelField];
      });
      
      datasets.push({
        label: chartOptions.title,
        data: sourceData.map(item => ({
          x: item[chartOptions.labelField],
          y: item[chartOptions.valueField]
        })),
        backgroundColor: chartOptions.fill ? colorSchemes.gradients(ctx)[0] : colorSchemes.default[0],
        borderColor: colorSchemes.borders[0],
        borderWidth: 2,
        fill: chartOptions.fill,
        tension: 0.1
      });
    }
    
    // Create chart
    chartInstances[containerId] = new Chart(ctx, {
      type: 'line',
      data: {
        datasets: datasets
      },
      options: {
        responsive: true,
        maintainAspectRatio: false,
        plugins: {
          legend: {
            display: chartOptions.showLegend
          },
          title: {
            display: true,
            text: chartOptions.title,
            font: {
              size: 16
            }
          },
          tooltip: {
            callbacks: {
              label: function(context) {
                let label = context.dataset.label || '';
                if (label) {
                  label += ': ';
                }
                if (context.parsed.y !== null) {
                  label += new Intl.NumberFormat().format(context.parsed.y);
                }
                return label;
              }
            }
          }
        },
        animation: chartOptions.animation,
        scales: {
          x: {
            type: chartOptions.timeScale ? 'time' : 'category',
            time: chartOptions.timeScale ? {
              unit: 'year',
              displayFormats: {
                year: 'yyyy'
              }
            } : undefined
          },
          y: {
            beginAtZero: true,
            ticks: {
              callback: function(value) {
                if (value >= 1000000000) {
                  return (value / 1000000000).toFixed(1) + 'B';
                } else if (value >= 1000000) {
                  return (value / 1000000).toFixed(1) + 'M';
                } else if (value >= 1000) {
                  return (value / 1000).toFixed(1) + 'K';
                }
                return value;
              }
            }
          }
        }
      }
    });
    
    return chartInstances[containerId];
  }
  
  // Create treemap for product/country hierarchies
  async function createTreemap(containerId, data, options = {}) {
    // This is a simplified treemap using divs since Chart.js doesn't have built-in treemap
    const container = document.getElementById(containerId);
    if (!container) return null;
    
    // Default options
    const defaults = {
      valueField: 'value',
      labelField: 'country',
      title: 'Trade Distribution',
      limit: 20
    };
    
    const chartOptions = { ...defaults, ...options };
    
    // Prepare data - limit to top N items
    let chartData;
    if (Array.isArray(data.rows)) {
      chartData = getTopItems(
        data.rows, 
        chartOptions.valueField, 
        chartOptions.labelField, 
        chartOptions.limit
      );
    } else if (Array.isArray(data)) {
      chartData = getTopItems(
        data, 
        chartOptions.valueField, 
        chartOptions.labelField, 
        chartOptions.limit
      );
    } else {
      console.error('Invalid data format for treemap');
      return null;
    }
    
    // Calculate total for percentages
    const total = chartData.reduce((sum, item) => sum + item.value, 0);
    
    // Create treemap container
    container.innerHTML = `
      <div class="treemap-title">${chartOptions.title}</div>
      <div class="treemap-container"></div>
    `;
    
    const treemapContainer = container.querySelector('.treemap-container');
    treemapContainer.style.display = 'flex';
    treemapContainer.style.flexWrap = 'wrap';
    treemapContainer.style.height = '400px';
    treemapContainer.style.position = 'relative';
    
    // Create rectangles
    chartData.forEach((item, index) => {
      const percentage = (item.value / total * 100).toFixed(1);
      const div = document.createElement('div');
      div.className = 'treemap-item';
      div.style.backgroundColor = colorSchemes.default[index % colorSchemes.default.length];
      div.style.color = '#fff';
      div.style.padding = '8px';
      div.style.boxSizing = 'border-box';
      div.style.overflow = 'hidden';
      div.style.fontSize = '12px';
      div.style.position = 'relative';
      div.style.flexGrow = item.value;
      
      // Size must be proportional to value
      div.style.width = `${Math.sqrt(percentage)}%`;
      div.style.height = `${Math.sqrt(percentage) * 2}%`;
      div.style.margin = '2px';
      
      // Text with truncation
      div.innerHTML = `
        <div style="font-weight:bold;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;">
          ${item.label}
        </div>
        <div>${percentage}%</div>
      `;
      
      // Tooltip on hover
      div.title = `${item.label}: ${new Intl.NumberFormat().format(item.value)} (${percentage}%)`;
      
      treemapContainer.appendChild(div);
    });
    
    return treemapContainer;
  }
  
  // Create a world map visualization
  async function createWorldMapChart(containerId, data, options = {}) {
    // Load leaflet script if not already loaded
    if (!window.L) {
      await new Promise((resolve, reject) => {
        // Load CSS
        const leafletCss = document.createElement('link');
        leafletCss.rel = 'stylesheet';
        leafletCss.href = 'https://unpkg.com/leaflet/dist/leaflet.css';
        document.head.appendChild(leafletCss);
        
        // Load script
        const script = document.createElement('script');
        script.src = 'https://unpkg.com/leaflet/dist/leaflet.js';
        script.onload = resolve;
        script.onerror = reject;
        document.body.appendChild(script);
      });
    }
    
    const container = document.getElementById(containerId);
    if (!container) return null;
    
    // Default options
    const defaults = {
      valueField: 'value',
      labelField: 'country',
      countryCodeField: 'code',
      title: 'World Trade Map',
      colorScale: ['#e6f7ff', '#0077be'],
      zoom: 2
    };
    
    const chartOptions = { ...defaults, ...options };
    
    // Set container height if not already set
    if (!container.style.height || container.style.height === 'auto') {
      container.style.height = '400px';
    }
    
    // Clear previous map
    container.innerHTML = '';
    
    // Create map
    const map = L.map(containerId).setView([20, 0], chartOptions.zoom);
    
    // Add tile layer
    L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
      maxZoom: 19,
      attribution: '© OpenStreetMap contributors'
    }).addTo(map);
    
    // Process data
    const sourceData = Array.isArray(data.rows) ? data.rows : data;
    
    // Find min/max for color scaling
    const values = sourceData.map(item => item[chartOptions.valueField]);
    const min = Math.min(...values);
    const max = Math.max(...values);
    
    // Function to compute color based on value
    function getColor(value) {
      const ratio = (value - min) / (max - min || 1);
      
      // Linear interpolation between start and end colors
      const startColor = chartOptions.colorScale[0];
      const endColor = chartOptions.colorScale[1];
      
      // Parse hex colors
      const startRGB = {
        r: parseInt(startColor.slice(1, 3), 16),
        g: parseInt(startColor.slice(3, 5), 16),
        b: parseInt(startColor.slice(5, 7), 16)
      };
      
      const endRGB = {
        r: parseInt(endColor.slice(1, 3), 16),
        g: parseInt(endColor.slice(3, 5), 16),
        b: parseInt(endColor.slice(5, 7), 16)
      };
      
      // Interpolate
      const r = Math.round(startRGB.r + ratio * (endRGB.r - startRGB.r));
      const g = Math.round(startRGB.g + ratio * (endRGB.g - startRGB.g));
      const b = Math.round(startRGB.b + ratio * (endRGB.b - startRGB.b));
      
      return `rgb(${r}, ${g}, ${b})`;
    }
    
    // Add country polygons if country GeoJSON is available
    // For now we'll use circles at country coordinates as a simplified version
    
    // Find coordinates for countries (simplified - real app would use GeoJSON)
    const countryCoordinates = {
      // Sample coordinates for major countries
      '842': [37.0902, -95.7129],  // USA
      '156': [35.8617, 104.1954],  // China
      '276': [51.1657, 10.4515],   // Germany
      '392': [36.2048, 138.2529],  // Japan
      '826': [55.3781, -3.4360],   // UK
      '250': [46.2276, 2.2137],    // France
      '380': [41.8719, 12.5674],   // Italy
      '124': [56.1304, -106.3468], // Canada
      '410': [35.9078, 127.7669],  // South Korea
      '484': [23.6345, -102.5528], // Mexico
      
      // Default coordinates for unknown countries
      'default': [0, 0]
    };
    
    // Add circles for each country
    sourceData.forEach(item => {
      const code = item[chartOptions.countryCodeField];
      const value = item[chartOptions.valueField];
      const coords = countryCoordinates[code] || countryCoordinates.default;
      
      if (coords[0] !== 0 || coords[1] !== 0) {
        // Size circle based on value
        const radius = Math.max(5, Math.min(20, 5 + (value - min) / (max - min || 1) * 15));
        
        L.circleMarker(coords, {
          radius: radius,
          fillColor: getColor(value),
          color: '#fff',
          weight: 1,
          opacity: 1,
          fillOpacity: 0.8
        })
        .addTo(map)
        .bindPopup(`
          <strong>${item[chartOptions.labelField]}</strong><br>
          Value: ${new Intl.NumberFormat().format(value)}
        `);
      }
    });
    
    // Add legend
    const legend = L.control({ position: 'bottomright' });
    
    legend.onAdd = function() {
      const div = L.DomUtil.create('div', 'info legend');
      div.style.backgroundColor = 'white';
      div.style.padding = '10px';
      div.style.borderRadius = '5px';
      div.style.boxShadow = '0 0 5px rgba(0,0,0,0.2)';
      
      div.innerHTML = `
        <div style="font-weight:bold;margin-bottom:5px;">${chartOptions.title}</div>
        <div style="display:flex;align-items:center;margin-bottom:5px;">
          <div style="width:20px;height:20px;background:${chartOptions.colorScale[0]};margin-right:5px;"></div>
          <span>${new Intl.NumberFormat().format(min)}</span>
        </div>
        <div style="display:flex;align-items:center;">
          <div style="width:20px;height:20px;background:${chartOptions.colorScale[1]};margin-right:5px;"></div>
          <span>${new Intl.NumberFormat().format(max)}</span>
        </div>
      `;
      
      return div;
    };
    
    legend.addTo(map);
    
    return map;
  }

  // Public API
  return {
    createBarChart,
    createPieChart,
    createLineChart,
    createTreemap,
    createWorldMapChart,
    destroyChart
  };
})();