class CustomSpendingChart extends HTMLElement {
connectedCallback() {
this.attachShadow({ mode: 'open' });
this.shadowRoot.innerHTML = `
Spending Overview
`;
// 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);