File size: 3,483 Bytes
7b78975
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
class CustomSpendingChart extends HTMLElement {
    connectedCallback() {
        this.attachShadow({ mode: 'open' });
        this.shadowRoot.innerHTML = `
            <style>
                .chart-container {
                    position: relative;
                    height: 300px;
                }
            </style>
            <div>
                <div class="flex justify-between items-center mb-4">
                    <h2 class="text-xl font-bold text-gray-800">Spending Overview</h2>
                    <select class="px-3 py-1 border rounded-lg text-sm focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500">
                        <option>Last 6 Months</option>
                        <option>This Year</option>
                        <option>Last Year</option>
                    </select>
                </div>
                <div class="chart-container">
                    <canvas id="spendingChart"></canvas>
                </div>
            </div>
        `;

        // Initialize chart after the element is rendered
        setTimeout(() => {
            this.initChart();
        }, 0);
    }

    initChart() {
        const canvas = this.shadowRoot.getElementById('spendingChart');
        if (!canvas) return;

        const ctx = canvas.getContext('2d');
        
        // Chart data (would normally come from props or API)
        const data = {
            labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'],
            datasets: [
                {
                    label: 'Food',
                    data: [120, 190, 150, 200, 180, 220],
                    backgroundColor: '#6366F1',
                    borderRadius: 4
                },
                {
                    label: 'Transport',
                    data: [80, 90, 70, 85, 75, 95],
                    backgroundColor: '#10B981',
                    borderRadius: 4
                },
                {
                    label: 'Shopping',
                    data: [200, 150, 180, 220, 250, 200],
                    backgroundColor: '#8B5CF6',
                    borderRadius: 4
                },
                {
                    label: 'Entertainment',
                    data: [50, 60, 70, 65, 80, 75],
                    backgroundColor: '#F59E0B',
                    borderRadius: 4
                }
            ]
        };

        new Chart(ctx, {
            type: 'bar',
            data: data,
            options: {
                responsive: true,
                maintainAspectRatio: false,
                plugins: {
                    legend: {
                        position: 'bottom',
                    },
                    tooltip: {
                        mode: 'index',
                        intersect: false,
                    }
                },
                scales: {
                    x: {
                        stacked: true,
                        grid: {
                            display: false
                        }
                    },
                    y: {
                        stacked: true,
                        beginAtZero: true,
                        ticks: {
                            callback: function(value) {
                                return '$' + value;
                            }
                        }
                    }
                }
            }
        });
    }
}

customElements.define('custom-spending-chart', CustomSpendingChart);