File size: 3,791 Bytes
8b7b267
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * Chart Component
 * Wrapper for Chart.js with common configurations
 */

// Chart.js will be loaded from CDN in pages that need it

export class ChartComponent {
  constructor(canvasId, type = 'line', options = {}) {
    this.canvasId = canvasId;
    this.canvas = document.getElementById(canvasId);
    this.type = type;
    this.options = options;
    this.chart = null;

    if (!this.canvas) {
      console.error(`[Chart] Canvas not found: ${canvasId}`);
    }
  }

  /**
   * Create chart with data
   */
  async create(data, customOptions = {}) {
    if (!this.canvas) return;

    // Ensure Chart.js is loaded
    if (typeof Chart === 'undefined') {
      console.error('[Chart] Chart.js not loaded');
      return;
    }

    // Destroy existing chart
    this.destroy();

    const config = {
      type: this.type,
      data: data,
      options: {
        responsive: true,
        maintainAspectRatio: false,
        ...this.getDefaultOptions(this.type),
        ...this.options,
        ...customOptions,
      },
    };

    this.chart = new Chart(this.canvas, config);
  }

  /**
   * Update chart data
   */
  update(data) {
    if (!this.chart) {
      console.warn('[Chart] Chart not initialized');
      return;
    }

    this.chart.data = data;
    this.chart.update();
  }

  /**
   * Destroy chart
   */
  destroy() {
    if (this.chart) {
      this.chart.destroy();
      this.chart = null;
    }
  }

  /**
   * Get default options by chart type
   */
  getDefaultOptions(type) {
    const common = {
      plugins: {
        legend: {
          display: true,
          position: 'top',
          labels: {
            color: 'var(--text-normal)',
            font: {
              family: 'var(--font-family-base)',
            },
          },
        },
        tooltip: {
          backgroundColor: 'var(--surface-glass)',
          titleColor: 'var(--text-strong)',
          bodyColor: 'var(--text-normal)',
          borderColor: 'var(--border-default)',
          borderWidth: 1,
        },
      },
    };

    const typeDefaults = {
      line: {
        scales: {
          x: {
            grid: {
              color: 'var(--border-subtle)',
            },
            ticks: {
              color: 'var(--text-soft)',
            },
          },
          y: {
            grid: {
              color: 'var(--border-subtle)',
            },
            ticks: {
              color: 'var(--text-soft)',
            },
          },
        },
      },
      bar: {
        scales: {
          x: {
            grid: {
              display: false,
            },
            ticks: {
              color: 'var(--text-soft)',
            },
          },
          y: {
            grid: {
              color: 'var(--border-subtle)',
            },
            ticks: {
              color: 'var(--text-soft)',
            },
          },
        },
      },
      doughnut: {
        plugins: {
          legend: {
            position: 'right',
          },
        },
      },
    };

    return {
      ...common,
      ...(typeDefaults[type] || {}),
    };
  }
}

/**
 * Load Chart.js from CDN if not already loaded
 */
export async function loadChartJS() {
  if (typeof Chart !== 'undefined') {
    return Promise.resolve();
  }

  return new Promise((resolve, reject) => {
    const script = document.createElement('script');
    script.src = 'https://cdn.jsdelivr.net/npm/chart.js@4/dist/chart.umd.min.js';
    script.onload = () => {
      console.log('[Chart] Chart.js loaded from CDN');
      resolve();
    };
    script.onerror = () => {
      console.error('[Chart] Failed to load Chart.js');
      reject(new Error('Failed to load Chart.js'));
    };
    document.head.appendChild(script);
  });
}

export default ChartComponent;