Mahmoud Al-HabashOpen to new projects
  • WorkWork
  • ServicesServices
  • LabLab
  • WritingWriting
  • ContactContact
Amman--:--عربي
Writing/Laravel

Livewire Charts and Graphs

Integrate charts and graphs with Livewire components.

16 January 20261 min readLaravelاقرأ بالعربية

On this page

  1. Component Data
  2. View with Chart.js
  3. Reactive Updates

Charts with Livewire:

Component Data

public function getChartData()
{
    return [
        'labels' => ['Jan', 'Feb', 'Mar'],
        'data' => [10, 20, 30],
    ];
}

View with Chart.js

blade
<canvas x-data x-init="
    new Chart($el, {
        type: 'line',
        data: {
            labels: @js($this->getChartData()['labels']),
            datasets: [{
                data: @js($this->getChartData()['data'])
            }]
        }
    })
"></canvas>

Reactive Updates

blade
<div wire:ignore>
    <canvas x-ref="chart"></canvas>
</div>

@script
<script>
    $wire.on('chart-updated', (data) => {
        chart.data.datasets[0].data = data;
        chart.update();
    });
</script>
@endscript
All articles

Keep reading

  • Laravel Action Classes Pattern16 January 20261 min read
  • Laravel Action Classes: Single Responsibility16 January 20261 min read
  • Laravel API Rate Limiting16 January 20261 min read