Skip to main content

Introduction

Filament provides powerful chart widgets that allow you to display real-time, interactive charts using Chart.js. These widgets are perfect for visualizing trends, comparing data sets, and presenting analytics in an engaging way. Chart widgets support multiple chart types including line, bar, pie, doughnut, radar, polar area, bubble, and scatter charts.

Creating a Chart Widget

Generate a chart widget using the Artisan command:
This creates a widget class that extends ChartWidget:

Available Chart Types

Filament supports all Chart.js chart types. Set the chart type by returning the appropriate string from the getType() method:

Line Chart

'line' - Display data as connected points

Bar Chart

'bar' - Display data as vertical bars

Pie Chart

'pie' - Display data as circular segments

Doughnut Chart

'doughnut' - Display data as a ring chart

Radar Chart

'radar' - Display multivariate data on axes

Polar Area Chart

'polarArea' - Display data as circular sectors

Bubble Chart

'bubble' - Display three-dimensional data

Scatter Chart

'scatter' - Display data as points on x/y axes

Chart Type Examples

Refer to the Chart.js documentation for detailed information about each chart type and data format requirements.

Chart Data Structure

The getData() method returns an array with datasets and labels:

Multiple Datasets

Display multiple datasets on the same chart:

Generating Data from Eloquent Models

Filament recommends using the flowframe/laravel-trend package to generate chart data from Eloquent models:
Then use it in your widget:

Advanced Trend Queries

Chart Colors

Customize chart colors using the $color property:
Available colors match Filament’s color system: primary, success, warning, danger, info, gray.

Custom Dataset Colors

For more control, set colors directly in the dataset:

Chart Filters

Basic Select Filter

Add a filter dropdown to change the chart data:

Custom Filter Schema

Build more complex filters using form components:

Deferred Filters

Prevent chart updates until the user clicks “Apply”:
With deferred filters:
  • Chart displays using default filter values on page load
  • Changes only apply when the user clicks “Apply”
  • A “Reset” link restores default values
  • Better performance for complex queries

Customizing Filter Actions

Live Updating (Polling)

Charts refresh every 5 seconds by default. Customize the polling interval:

Chart Height

Limit the maximum height of charts:
This prevents charts from becoming too large on larger screens.

Chart Options

Customize Chart.js configuration using the $options property:

Dynamic Options

Use the getOptions() method for dynamic configuration:

JavaScript Callbacks

Use RawJs for JavaScript callbacks:

Chart Heading and Description

Set the heading and description:
Or use methods for dynamic values:

Making Charts Collapsible

Allow users to collapse charts to save space:

Lazy Loading

By default, charts are lazy-loaded. Disable if needed:

Using Chart.js Plugins

Extend Chart.js functionality with plugins.

Step 1: Install the Plugin

Step 2: Create a JavaScript File

Create resources/js/filament-chart-js-plugins.js:
For global plugins:

Step 3: Build with Vite

Update vite.config.js:
Build:

Step 4: Register in Filament

In a service provider:

Best Practices

  1. Choose Appropriate Chart Types: Use line charts for trends, bar charts for comparisons, pie charts for proportions
  2. Keep Data Manageable: Limit data points to prevent overcrowded charts (typically 12-20 points max)
  3. Use Meaningful Labels: Provide clear axis labels and legends
  4. Color Consistency: Use consistent colors across dashboards for related data
  5. Optimize Queries: Cache expensive database queries, especially with polling enabled
  6. Responsive Design: Test charts on mobile devices
  7. Filter Validation: When using filters, ensure data is valid before querying
  8. Tooltip Enhancement: Customize tooltips to show relevant information
  9. Accessibility: Ensure charts have appropriate labels and descriptions
  10. Performance: Use deferred filters for complex queries to prevent excessive reloads

Common Chart Patterns

Revenue Over Time

Status Distribution

Comparison Chart

Next Steps