matheusmarnt/livecharts
LiveCharts is a reactive chart abstraction for Laravel using a pure PHP fluent API. Build 18 chart types and render via a single Livewire component. Supports ApexCharts and Chart.js with pluggable engines, enabling easy updates without JS boilerplate.
Install the package:
composer require matheusmarnt/livecharts
php artisan livecharts:install
Define a chart (e.g., in a controller or Livewire component):
use Matheusmarnt\LiveCharts\Facades\LiveCharts;
$chart = LiveCharts::line()
->title('Monthly Revenue')
->labels(['Jan', 'Feb', 'Mar'])
->dataset('2026', [100, 200, 150])
->colors(['#3B82F6']);
Render the chart in a Blade view:
<livewire:livecharts :chart="$chart" />
@liveChartsScripts is placed before @livewireScripts in your layout.Dynamic Livewire Dashboard:
public $chartData = [100, 200, 150];
public function mount() {
$this->chart = LiveCharts::line()
->title('Dynamic Data')
->labels(['Jan', 'Feb', 'Mar'])
->dataset('Values', $this->chartData);
}
public function updateData() {
$this->chartData = [200, 300, 400];
}
Fluent Builder Pattern:
LiveCharts::bar()
->title('Sales')
->labels(['Q1', 'Q2'])
->dataset('North', [100, 200])
->dataset('South', [150, 250])
->stacked()
->height(400);
->palette(TwPalette::Vibrant) for auto-colored datasets.Class-Based Charts:
make:chart:
php artisan make:chart SalesChart --type=bar
Matheusmarnt\LiveCharts\Charts\Chart and override methods like build():
class SalesChart extends Chart {
protected string $type = 'bar';
public function build() {
$this->title('Sales Overview');
$this->labels(['Jan', 'Feb']);
$this->datasets([
Dataset::make('North')->data([100, 200]),
Dataset::make('South')->data([150, 250]),
]);
}
}
Livewire Integration:
public $chart;
public function mount() {
$this->chart = new SalesChart();
}
public function fetchData() {
$data = Api::fetchSales();
$this->chart->datasets()->first()->data($data['north']);
}
Polling for Real-Time Updates:
$chart->poll(5000); // Refresh every 5 seconds
public function refresh() {
$this->chart->datasets()->first()->data($this->fetchFreshData());
}
<script>
window.addEventListener('livecharts:refreshed', (e) => {
console.log('Chart refreshed:', e.detail.id);
});
</script>
Event Handling:
$chart
->onDataPointClick('chartClicked')
->onZoom('chartZoomed');
#[On('chartClicked')]
public function handleClick(array $data) {
$this->emit('alert', 'Clicked: ' . $data['label']);
}
Broadcasting:
$chart->broadcastOn('private-charts.user123')->broadcastAs('chart.update');
Echo.channel('private-charts.user123')
.listen('chart.update', (data) => {
// Update chart or trigger Livewire method
});
Dark Mode Support: Use theme-aware colors:
$chart->titleColor(dark: TwColor::Amber300, light: TwColor::Amber600);
<html class="dark"> toggles.Multi-Engine: Switch engines globally or per chart:
LiveCharts::line()->engine('chartjs')->labels(['Jan', 'Feb']);
LiveCharts::registerEngine('highcharts', HighchartsAdapter::class);
Asset Management:
both mode) serves local files with CDN fallback.LIVECHARTS_ASSETS_MODE=cdn in .env to skip asset publishing.livecharts.js IIFE in resources/js.Stub Customization:
Edit stubs/livecharts/chart.stub to modify the boilerplate for make:chart.
Missing Assets:
ApexCharts is not defined or Chart is not defined.php artisan vendor:publish --tag=livecharts-assets --force to republish assets.livecharts:install after composer install.Asset Strategy Mismatch:
wire:navigate) with livecharts is not defined.LIVECHARTS_ASSETS_STRATEGY=navigate (default) or manually include @liveChartsScripts before @livewireScripts for stack mode.Livewire Hydration Issues:
wire:key on the Livewire component to force remount:
<livewire:livecharts :chart="$chart" wire:key="chart-{{ $key }}" />
Polling Conflicts:
wire:poll keys or disable polling for non-critical charts.Dark Mode Lag:
theme('auto') and ensure the dark:/light: color pairs are defined for all critical elements.Custom Engine Registration:
Matheusmarnt\LiveCharts\Contracts\EngineAdapter and is registered before use:
LiveCharts::registerEngine('my-engine', MyAdapter::class);
Inspect Rendered JSON:
Add {{ dd($chart->toJson()) }} to debug chart configuration before rendering.
Check Console for Errors:
Open browser dev tools to catch missing assets or JS errors (e.g., livecharts is not defined).
Preview All Chart Types:
Use php artisan livecharts:preview to test every chart type in isolation.
Livewire Logs:
Enable Livewire logging in config/livewire.php to debug hydration issues:
'log' => env('LIVEWIRE_LOG', false),
Custom Chart Types:
LiveCharts::make('custom')
->engine('apexcharts')
->config(['chart' => ['type' => 'custom']]);
Theme Extensions:
TwColor or TwPalette values by extending the enums in config/livecharts.php:
'colors' => [
'custom' => ['#FF00FF', '#00FFFF'],
],
Event Augmentation:
handleEvent() method in a custom chart class.Asset Overrides:
How can I help you explore Laravel packages today?