@props([
'chartId' => 'chart-' . uniqid(),
'type' => 'line',
'height' => 300,
'data' => [],
'title' => null,
'subtitle' => null,
])
merge(['class' => 'chart-container card border-0 shadow-sm rounded-4']) }}
x-data="{
chart: null,
init() {
this.$nextTick(() => {
this.initChart();
});
},
initChart() {
const canvas = this.$refs.canvas;
if (!canvas) return;
const ctx = canvas.getContext('2d');
if (!ctx) return;
const chartData = @js($data);
// Destroy existing chart if any
if (this.chart) {
this.chart.destroy();
this.chart = null;
}
if (typeof Chart !== 'undefined') {
this.chart = new Chart(ctx, {
type: @js($type),
data: chartData,
options: {
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: {
position: 'top',
}
},
scales: {
y: {
beginAtZero: true
}
}
}
});
}
},
destroy() {
if (this.chart) {
this.chart.destroy();
this.chart = null;
}
}
}"
>
@if($title || $subtitle)
@endif
{{ $slot }}