leandrocfe/filament-apex-charts
Install the Package
composer require leandrocfe/filament-apex-charts:"^5.0"
Ensure compatibility with your Filament version (v3/v4/v5) by checking the README branches.
Register the Plugin
Add to your Panel provider (e.g., app/Providers/Filament/AdminPanelProvider.php):
public function panel(Panel $panel): Panel {
return $panel->plugins([
FilamentApexChartsPlugin::make(),
]);
}
Generate a Basic Chart Create a widget with the Artisan command:
php artisan make:filament-apex-charts BlogPostsChart
This scaffolds a BlogPostsChart class in app/Filament/Widgets/BlogPostsChart.php.
First Render Add the widget to a dashboard or page:
use App\Filament\Widgets\BlogPostsChart;
public function getWidgets(): array {
return [
BlogPostsChart::class,
];
}
Edit the generated widget (BlogPostsChart.php) to define a line chart:
use Leandrocfe\FilamentApexCharts\Contracts\HasApexCharts;
use Leandrocfe\FilamentApexCharts\Contracts\HasApexChartsOptions;
use Leandrocfe\FilamentApexCharts\Contracts\HasApexChartsData;
class BlogPostsChart extends Widget implements HasApexCharts, HasApexChartsOptions, HasApexChartsData {
protected static string $view = 'filament-apex-charts::widget';
public function getChartType(): string {
return 'line'; // Chart type
}
public function getChartOptions(): array {
return [
'chart' => [
'id' => 'blog-posts-chart',
'type' => 'line',
'height' => 350,
],
'stroke' => ['width' => 5],
];
}
public function getChartData(): array {
return [
'labels' => ['Jan', 'Feb', 'Mar', 'Apr'],
'series' => [
['name' => 'Posts', 'data' => [30, 40, 35, 50]],
],
];
}
}
Result: A responsive line chart displaying monthly blog post counts.
Use Eloquent queries or services to fetch real-time data:
public function getChartData(): array {
$posts = Post::query()
->selectRaw('DATE_FORMAT(created_at, "%b") as month')
->selectRaw('COUNT(*) as count')
->groupBy('month')
->orderBy('month')
->get();
return [
'labels' => $posts->pluck('month'),
'series' => [
['name' => 'Posts', 'data' => $posts->pluck('count')],
],
];
}
Extract chart logic into a trait or service for consistency:
// app/Services/ApexChartService.php
class ApexChartService {
public static function buildSalesChart(Collection $data): array {
return [
'labels' => $data->pluck('month'),
'series' => [
['name' => 'Sales', 'data' => $data->pluck('revenue')],
],
];
}
}
// Usage in widget:
public function getChartData(): array {
$sales = SalesReport::fetchMonthlyData();
return ApexChartService::buildSalesChart($sales);
}
Leverage ApexCharts options for tooltips, zoom, and annotations:
public function getChartOptions(): array {
return [
'chart' => [
'zoom' => ['enabled' => true],
'toolbar' => ['show' => true],
],
'tooltip' => ['y' => ['formatter' => 'function(val) { return "$" + val }']],
];
}
Combine multiple data series (e.g., line + column):
public function getChartType(): string {
return 'mixed'; // Mixed chart type
}
public function getChartData(): array {
return [
'labels' => ['Q1', 'Q2', 'Q3'],
'series' => [
['name' => 'Revenue', 'type' => 'column', 'data' => [4500, 5000, 4100]],
['name' => 'Expenses', 'type' => 'line', 'data' => [3900, 4500, 4300]],
],
];
}
Use chart data from a Filament table’s query:
// In a Filament Table's column:
use Leandrocfe\FilamentApexCharts\Widgets\ApexChartsWidget;
public function getTableWidgets(): array {
return [
ApexChartsWidget::make()
->query($this->getTableQuery())
->chartType('bar')
->options([
'chart' => ['height' => 300],
])
->data([
'labels' => $this->getTableQuery()->pluck('month'),
'series' => [
['name' => 'Metrics', 'data' => $this->getTableQuery()->pluck('value')],
],
]),
];
}
Extend default styles via CSS or Filament’s asset pipeline:
// In a widget's view (e.g., resources/views/filament/widgets/blog-posts-chart.blade.php)
@push('filament-apex-charts-styles')
<style>
.apexcharts-dark .apexcharts-xaxis-label {
color: #ccc !important;
}
</style>
@endpush
Version Mismatches
filament-apex-charts@^5.0 with Filament v2.leandrocfe/filament-apex-charts:v2.0.2).Data Format Errors
labels and series in specific formats. Undefined keys or mismatched lengths break rendering.dd($this->getChartData()) to validate structure:
// Correct:
['labels' => ['Jan', 'Feb'], 'series' => [['data' => [10, 20]]]]
// Incorrect:
['labels' => ['Jan'], 'series' => [['data' => [10, 20, 30]]]] // Length mismatch!
Asset Loading Conflicts
// app/Providers/AppServiceProvider.php
public function boot() {
FilamentApexChartsPlugin::make()->register();
}
Dynamic Updates Not Triggering
updateOptions or updateSeries methods via JavaScript:
// In a widget's view
<script>
document.addEventListener('chart-update', function() {
const chart = document.querySelector('#blog-posts-chart').apexChart;
chart.updateSeries([{data: [10, 20, 30]}]);
});
</script>
Inspect Rendered HTML
Open browser dev tools (F12) and check if the chart container exists:
<div id="blog-posts-chart" class="apexcharts-chart"></div>
$view property).Check Console for Errors
Look for apexcharts.min.js errors (e.g., missing dependencies):
Uncaught TypeError: document.querySelector(...).apexChart is undefined
filament-apex-charts::script is included in the layout.Validate Chart Options Use ApexCharts’ online config validator to test options.
class CustomGaugeChart
How can I help you explore Laravel packages today?