File size: 10,257 Bytes
9b1fe6c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
document.addEventListener('DOMContentLoaded', function() {
    // Initialize charts with sample data
    initCharts();
    loadNewsFeed();
    setupDarkModeToggle();
});

function initCharts() {
    // Sample data - in a real app, this would come from your API
    const dates = ['2023-01', '2023-02', '2023-03', '2023-04', '2023-05', '2023-06'];
    const inflationData = [5.2, 5.5, 5.8, 6.1, 6.0, 5.9];
    const gdpData = [2.1, 2.3, 2.5, 2.7, 2.9, 3.1];
    const sentimentData = [0.2, -0.1, 0.3, -0.2, 0.1, 0.4];

    // Inflation Chart
    const inflationCtx = document.getElementById('inflationChart').getContext('2d');
    new Chart(inflationCtx, {
        type: 'line',
        data: {
            labels: dates,
            datasets: [{
                label: 'Inflation Rate',
                data: inflationData,
                borderColor: '#ef4444',
                backgroundColor: 'rgba(239, 68, 68, 0.1)',
                tension: 0.3,
                fill: true
            }]
        },
        options: getChartOptions('Inflation Rate (%)')
    });

    // GDP Chart
    const gdpCtx = document.getElementById('gdpChart').getContext('2d');
    new Chart(gdpCtx, {
        type: 'line',
        data: {
            labels: dates,
            datasets: [{
                label: 'GDP Growth',
                data: gdpData,
                borderColor: '#10b981',
                backgroundColor: 'rgba(16, 185, 129, 0.1)',
                tension: 0.3,
                fill: true
            }]
        },
        options: getChartOptions('GDP Growth (%)')
    });

    // Full Chart
    const fullCtx = document.getElementById('fullChart').getContext('2d');
    new Chart(fullCtx, {
        type: 'line',
        data: {
            labels: dates,
            datasets: [
                {
                    label: 'Inflation',
                    data: inflationData,
                    borderColor: '#ef4444',
                    backgroundColor: 'rgba(239, 68, 68, 0.1)',
                    yAxisID: 'y',
                    tension: 0.3
                },
                {
                    label: 'GDP Growth',
                    data: gdpData,
                    borderColor: '#10b981',
                    backgroundColor: 'rgba(16, 185, 129, 0.1)',
                    yAxisID: 'y',
                    tension: 0.3
                },
                {
                    label: 'Sentiment',
                    data: sentimentData,
                    borderColor: '#6366f1',
                    backgroundColor: 'rgba(99, 102, 241, 0.1)',
                    yAxisID: 'y1',
                    tension: 0.3
                }
            ]
        },
        options: {
            responsive: true,
            interaction: {
                mode: 'index',
                intersect: false,
            },
            plugins: {
                title: {
                    display: true,
                    text: 'Economic Indicators Over Time',
                    color: '#6b7280',
                    font: {
                        size: 16
                    }
                },
                legend: {
                    position: 'top',
                    labels: {
                        color: '#6b7280'
                    }
                },
                tooltip: {
                    mode: 'index',
                    intersect: false
                }
            },
            scales: {
                x: {
                    grid: {
                        color: 'rgba(209, 213, 219, 0.2)'
                    },
                    ticks: {
                        color: '#6b7280'
                    }
                },
                y: {
                    type: 'linear',
                    display: true,
                    position: 'left',
                    grid: {
                        color: 'rgba(209, 213, 219, 0.2)'
                    },
                    ticks: {
                        color: '#6b7280'
                    }
                },
                y1: {
                    type: 'linear',
                    display: true,
                    position: 'right',
                    grid: {
                        drawOnChartArea: false,
                        color: 'rgba(209, 213, 219, 0.2)'
                    },
                    ticks: {
                        color: '#6b7280'
                    }
                }
            }
        }
    });

    // Update text elements
    document.getElementById('sentimentText').textContent = `Current sentiment: Positive (0.42)`;
    document.getElementById('inflationText').textContent = `Forecast: 5.9% (↓ 0.1% from last month)`;
    document.getElementById('gdpText').textContent = `Forecast: 3.1% (↑ 0.2% from last quarter)`;

    // Initialize sentiment gauge
    updateSentimentGauge(0.42);
}

function getChartOptions(title) {
    return {
        responsive: true,
        maintainAspectRatio: false,
        plugins: {
            legend: {
                display: false
            },
            title: {
                display: false
            },
            tooltip: {
                enabled: true,
                mode: 'index',
                intersect: false
            }
        },
        scales: {
            x: {
                grid: {
                    display: false
                },
                ticks: {
                    display: false
                }
            },
            y: {
                grid: {
                    display: false
                },
                ticks: {
                    display: false
                }
            }
        },
        elements: {
            point: {
                radius: 0
            }
        }
    };
}

function updateSentimentGauge(value) {
    // Value between -1 and 1
    const gaugeFill = document.createElement('div');
    gaugeFill.className = 'gauge-fill';
    gaugeFill.style.transform = `rotate(${value * 0.5}turn)`;
    
    const gaugeCover = document.createElement('div');
    gaugeCover.className = 'gauge-cover';
    gaugeCover.textContent = value > 0 ? '+' + value.toFixed(2) : value.toFixed(2);
    
    const gaugeBody = document.createElement('div');
    gaugeBody.className = 'gauge-body';
    gaugeBody.appendChild(gaugeFill);
    
    const gaugeContainer = document.createElement('div');
    gaugeContainer.className = 'gauge-container';
    gaugeContainer.appendChild(gaugeBody);
    gaugeContainer.appendChild(gaugeCover);
    
    document.getElementById('sentimentGauge').innerHTML = '';
    document.getElementById('sentimentGauge').appendChild(gaugeContainer);
}

async function loadNewsFeed() {
    try {
        // In a real app, you would fetch this from your API
        const mockNews = [
            {
                title: "Central Bank Announces Interest Rate Hike to Combat Inflation",
                source: "Financial Times",
                date: "2023-06-15",
                sentiment: 0.42
            },
            {
                title: "GDP Growth Exceeds Expectations in Q2",
                source: "Bloomberg",
                date: "2023-06-10",
                sentiment: 0.78
            },
            {
                title: "Unemployment Rates Rise Slightly Amid Economic Slowdown",
                source: "Reuters",
                date: "2023-06-05",
                sentiment: -0.35
            },
            {
                title: "Government Announces New Stimulus Package to Boost Economy",
                source: "Wall Street Journal",
                date: "2023-05-28",
                sentiment: 0.65
            }
        ];

        const newsFeed = document.getElementById('newsFeed');
        newsFeed.innerHTML = '';

        mockNews.forEach(item => {
            const sentimentColor = item.sentiment > 0.5 ? 'text-green-500' : 
                                 item.sentiment < -0.5 ? 'text-red-500' : 'text-yellow-500';
            const sentimentIcon = item.sentiment > 0.5 ? 'trending-up' : 
                                 item.sentiment < -0.5 ? 'trending-down' : 'minus';

            const newsItem = document.createElement('div');
            newsItem.className = 'news-item bg-white dark:bg-gray-800 rounded-lg shadow p-4';
            newsItem.innerHTML = `
                <div class="flex justify-between items-start">
                    <div class="flex-1">
                        <h3 class="font-medium text-gray-800 dark:text-gray-200">${item.title}</h3>
                        <div class="flex items-center mt-2 text-sm text-gray-500 dark:text-gray-400">
                            <span>${item.source}</span>
                            <span class="mx-2">•</span>
                            <span>${item.date}</span>
                        </div>
                    </div>
                    <div class="flex items-center ml-4 ${sentimentColor}">
                        <i data-feather="${sentimentIcon}" class="w-5 h-5 mr-1"></i>
                        <span>${item.sentiment > 0 ? '+' : ''}${item.sentiment.toFixed(2)}</span>
                    </div>
                </div>
            `;
            newsFeed.appendChild(newsItem);
        });

        feather.replace();
    } catch (error) {
        console.error('Error loading news feed:', error);
        document.getElementById('newsFeed').innerHTML = `
            <div class="text-center py-8 text-gray-500 dark:text-gray-400">
                <i data-feather="alert-circle" class="w-12 h-12 mx-auto mb-4"></i>
                <p>Failed to load news feed. Please try again later.</p>
            </div>
        `;
        feather.replace();
    }
}

function setupDarkModeToggle() {
    const darkModeToggle = document.createElement('button');
    darkModeToggle.className = 'flex items-center justify-center w-10 h-10 rounded-full bg-gray-200 dark:bg-gray-700 text-gray-700 dark:text-gray-200';
    darkModeToggle.innerHTML = '<i data-feather="moon"></i>';
    darkModeToggle.addEventListener('click', () => {
        document.documentElement.classList.toggle('dark');
        localStorage.setItem('darkMode', document.documentElement.classList.contains('dark'));
        feather.replace();