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:ChartWidget:
Available Chart Types
Filament supports all Chart.js chart types. Set the chart type by returning the appropriate string from thegetType() method:
Line Chart
'line' - Display data as connected pointsBar Chart
'bar' - Display data as vertical barsPie Chart
'pie' - Display data as circular segmentsDoughnut Chart
'doughnut' - Display data as a ring chartRadar Chart
'radar' - Display multivariate data on axesPolar Area Chart
'polarArea' - Display data as circular sectorsBubble Chart
'bubble' - Display three-dimensional dataScatter Chart
'scatter' - Display data as points on x/y axesChart Type Examples
Chart Data Structure
ThegetData() 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 theflowframe/laravel-trend package to generate chart data from Eloquent models:
Advanced Trend Queries
Chart Colors
Customize chart colors using the$color property:
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”:- 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:Chart Options
Customize Chart.js configuration using the$options property:
Dynamic Options
Use thegetOptions() method for dynamic configuration:
JavaScript Callbacks
UseRawJs for JavaScript callbacks:
Chart Heading and Description
Set the heading and description: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
Createresources/js/filament-chart-js-plugins.js:
Step 3: Build with Vite
Updatevite.config.js:
Step 4: Register in Filament
In a service provider:Best Practices
- Choose Appropriate Chart Types: Use line charts for trends, bar charts for comparisons, pie charts for proportions
- Keep Data Manageable: Limit data points to prevent overcrowded charts (typically 12-20 points max)
- Use Meaningful Labels: Provide clear axis labels and legends
- Color Consistency: Use consistent colors across dashboards for related data
- Optimize Queries: Cache expensive database queries, especially with polling enabled
- Responsive Design: Test charts on mobile devices
- Filter Validation: When using filters, ensure data is valid before querying
- Tooltip Enhancement: Customize tooltips to show relevant information
- Accessibility: Ensure charts have appropriate labels and descriptions
- Performance: Use deferred filters for complex queries to prevent excessive reloads
Common Chart Patterns
Revenue Over Time
Status Distribution
Comparison Chart
Next Steps
- Explore Stats Overview Widgets for displaying key metrics
- Learn about Widget Filters for dashboard-wide filtering
- Review Chart.js Documentation for advanced customization