Filament Apex Charts

repository·5.x·Indexed 19 days ago

https://github.com/leandrocfe/filament-apex-charts

A plugin for the Filament PHP framework that integrates the Apex Charts JavaScript library. It allows developers to create interactive, customizable charts within Filament dashboards and panels using a dedicated ApexChartWidget class and an Artisan command for scaffolding. Supported chart types include Area, Bar, Boxplot, Bubble, Candlestick, Column, Heatmap, Line, Pie, Radar, and more.

Tokens
3.9K
Snippets
16
Records
17
Agent score
66%

What's inside filament-apex-charts

  1. Create a new Apex Chart widget

    5.x

    Use the Artisan command to generate a new chart widget class. You can choose from various chart types (Area, Bar, Boxplot, Bubble, Candlestick, Column, Donut, Heatmap, Line, Mixed-LineAndColumn, Pie, PolarArea, Radar, Radialbar, RangeArea, Scatter, TimelineRangeBars, Treemap, Funnel) or select Empty to define your own configuration from scratch.

    php artisan make:filament-apex-charts BlogPostsChart
  2. Use example widgets in your Filament project

    5.x

    To use the provided example charts in your application, copy the desired widget file from the examples/ directory and paste it into your project's app\Filament\Widgets folder. After adding the file, the widget will automatically appear on your Filament dashboard page.

    # Example: Copying a widget
    # From: examples/Bar/BasicBarChart.php
    # To: app/Filament/Widgets/BasicBarChart.php
  3. Filter chart data using Schemas

    5.x

    To create complex filters (like date ranges), use the HasFiltersSchema trait and implement filtersSchema(Schema $schema). When the schema is updated, call $this->updateOptions() in the updatedInteractsWithSchemas() method to refresh the chart.

    Filter values are accessible via the $this->filters array inside getOptions().

    use Filament\Forms\Components\DatePicker;
    use Filament\Schemas\Schema;
    use Filament\Widgets\ChartWidget\Concerns\HasFiltersSchema;
    use Leandrocfe\FilamentApexCharts\Widgets\ApexChartWidget;
    
    class BlogPostsChart extends ApexChartWidget
    {
        use HasFiltersSchema;
    
        public function filtersSchema(Schema $schema): Schema
        {
            return $schema->components([
                DatePicker::make('date_start'),
                DatePicker::make('date_end'),
            ]);
        }
    
        public function updatedInteractsWithSchemas(string $statePath): void
        {
            $this->updateOptions();
        }
    
        protected function getOptions(): array
        {
            $dateStart = $this->filters['date_start'];
            // ... use $dateStart in your chart options
            return [];
        }
    }
  4. Implement Single Select filters

    5.x

    For simple dropdown filters, define a $filter property for the default value and implement getFilters() to return an associative array of ['value' => 'Label'].

    public ?string $filter = 'today';
    
    protected function getFilters(): ?array
    {
        return [
            'today' => 'Today',
            'week' => 'Last week',
            'month' => 'Last month',
        ];
    }
    
    protected function getOptions(): array
    {
        $activeFilter = $this->filter;
        // ... use $activeFilter in your chart options
        return [];
    }
  5. Install Filament Apex Charts

    5.x

    Install the package via Composer and register the plugin in your Filament Panel provider.

    composer require leandrocfe/filament-apex-charts:"^5.0"
    use Leandrocfeilament-apex-charts\FilamentApexChartsPlugin;
    
    public function panel(Panel $panel): Panel
    {
        return $panel
            ->plugins([
                FilamentApexChartsPlugin::make()
            ]);
    }
  6. Configure polling and deferred loading

    5.x

    Optimize performance and data freshness:

    • Polling: Set protected static ?string $pollingInterval = '10s'; to refresh data automatically, or set to null to disable.
    • Deferred Loading: Set protected static bool $deferLoading = true; to prevent slow queries from blocking the initial page load. In getOptions(), check $this->readyToLoad before returning data to show a loading state.
    protected static ?string $pollingInterval = '10s';
    protected static bool $deferLoading = true;
    
    protected function getOptions(): array
    {
        if (!$this->readyToLoad) {
            return [];
        }
        return [...];
    }
  7. Register the Filament Apex Charts plugin in a Filament Panel

    5.x

    To use Filament Apex Charts within your Filament application, you must register the FilamentApexChartsPlugin in your Panel provider. Use the make() method to instantiate the plugin and pass it to the plugins() method of your Panel configuration.

    use Leandrocfe\FilamentApexCharts\FilamentApexChartsPlugin;
    use Filament\Panel;
    
    public function panel(Panel $panel): Panel
    {
        return $panel
            ->plugins([
                FilamentApexChartsPlugin::make(),
            ]);
    }
  8. Make a widget collapsible and set height

    5.x

    Control the widget's layout behavior:

    • Collapsible: Enable via protected static bool $isCollapsible = true; or the isCollapsible() method.
    • Height: Set the content height in pixels via protected static ?int $contentHeight = 300; or the getContentHeight() method.
    protected static bool $isCollapsible = true;
    protected static ?int $contentHeight = 300;
  9. Configure widget metadata (Title, Subheading, ID, Footer)

    5.x

    You can customize the appearance and identity of the widget using several properties or methods:

    • Title: Set via protected static ?string $heading or getHeading().
    • Subheading: Set via protected static ?string $subheading or getSubheading().
    • Chart ID: Set via protected static string $chartId.
    • Footer: Set via protected static ?string $footer or getFooter(). The footer can return a string, an Htmlable object, or a Blade View.
    // Example of a custom footer using a view
    protected function getFooter(): null|string|Htmlable|View
    {
        return view('custom-footer', ['text' => 'My custom footer text']);
    }
  10. Configure chart options via getOptions()

    5.x

    The getOptions() method is the core of the widget. It must return an array that follows the Apex Charts documentation structure. This allows you to define series, labels, axes, colors, and animations.

    use Leandrocfe\FilamentApexCharts\Widgets\ApexChartWidget;
    use Leandrocfe\FilamentApexCharts\Enums\ApexChartTypeEnum;
    
    class BlogPostsChart extends ApexChartWidget
    {
        protected function getOptions(): array
        {
            return [
                'chart' => [
                    'type' => ApexChartTypeEnum::Bar,
                    'height' => 300,
                ],
                'series' => [
                    [
                        'name' => 'BlogPostsChart',
                        'data' => [7, 4, 6, 10, 14, 7, 5, 9, 10, 15, 13, 18],
                    ],
                ],
                'xaxis' => [
                    'categories' => ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
                ],
                'colors' => ['#f59e0b'],
            ];
        }
    }
  11. Add custom JavaScript via extraJsOptions()

    5.x

    To use advanced Apex Charts features like formatters (e.g., adding currency symbols or custom date formats), use the extraJsOptions() method. This method returns a RawJs object containing a JavaScript object literal.

    use Leandrocfe\FilamentApexCharts\Widgets\ApexChartWidget;
    use Filament\Support\RawJs;
    
    protected function extraJsOptions(): ?RawJs
    {
        return RawJs::make(<<<'JS' 
        {
            yaxis: {
                labels: {
                    formatter: function (val) {
                        return '$' + val
                    }
                }
            }
        } 
        JS);
    }
  12. Browse available chart examples

    5.x

    The repository provides a wide variety of pre-configured Apex Chart widgets categorized by chart type. You can use these as templates for your own data visualizations:

    • Area: Basic Area Chart
    • Bar: Basic Bar Chart
    • Boxplot: Basic Boxplot Chart
    • Bubble: Basic Bubble Chart
    • Candlestick: Basic Candlestick Chart
    • Column: Basic Column Chart, Column + Line Chart (Mixed), Gradient Column Chart, Column Chart with Annotations
    • Heatmap: Basic Heatmap Chart
    • Line: Basic Line Chart, Line + Column Chart (Mixed)
    • Mixed: Line + Column Chart
    • Pie: Basic Pie Chart, Donut Chart
    • Polar Area: Basic Polar Area Chart
    • Radar: Basic Radar Chart
    • Radialbar: Basic Radialbar Chart, Gradient Circle Chart
    • Range Area: Range Area Chart
    • Scatter: Basic Scatter Chart
    • Timeline Range-bars: Timeline Bar Chart
    • Treemap: Basic Treemap Chart