Introduction
Filament comes with many “chart” widget templates, which you can use to display real-time, interactive charts. Start by creating a widget with the command:ChartWidget class that is used for all charts. The type of chart is set by the getType() method. In this example, that method returns the string 'line'.
The protected ?string $heading variable is used to set the heading that describes the chart. If you need to set the heading dynamically, you can override the getHeading() method.
The getData() method is used to return an array of datasets and labels. Each dataset is a labeled array of points to plot on the chart, and each label is a string. This structure is identical to the Chart.js library, which Filament uses to render charts. You may use the Chart.js documentation to fully understand the possibilities to return from getData(), based on the chart type.
Available chart types
Below is a list of available chart widget classes which you may extend, and their corresponding Chart.js documentation page, for inspiration on what to return fromgetData():
- Bar chart - Chart.js documentation
- Bubble chart - Chart.js documentation
- Doughnut chart - Chart.js documentation
- Line chart - Chart.js documentation
- Pie chart - Chart.js documentation
- Polar area chart - Chart.js documentation
- Radar chart - Chart.js documentation
- Scatter chart - Chart.js documentation
Customizing the chart color
You can customize the color of the chart data by setting the$color property:
Generating chart data from an Eloquent model
To generate chart data from an Eloquent model, Filament recommends that you install theflowframe/laravel-trend package. You can view the documentation.
Here is an example of generating chart data from a model using the laravel-trend package:
Filtering chart data
Basic Select filter
You can set up chart filters to change the data that is presented. Commonly, this is used to change the time period that chart data is rendered for. To set a default filter value, set the$filter property:
getFilters() method to return an array of values and labels for your filter:
getData() method:
Custom filters
You can use schema components to build custom filters for your chart widget. This approach offers a more flexible way to define filters. To get started, use theHasFiltersSchema trait and implement the filtersSchema() method:
$this->filters array. You can use these values inside your getData() method:
$this->filters array will always reflect the current form data. Please note that this data is not validated, as it is available live and not intended to be used for anything other than querying the database. You must ensure that the data is valid before using it.
If you want to add filters that apply to multiple widgets at once, see filtering widget data in the dashboard.
Deferring filter updates
By default, filters using thefiltersSchema() method update the chart data immediately as they are changed. However, for complex queries or better user experience, you may want to defer filter updates until the user clicks an “Apply” button.
When deferred, filter changes are only applied when the user clicks the “Apply” button. This ensures that the chart only re-renders when the user has finished adjusting all of their filters.
The chart will display data using the default filter values when the page first loads, ensuring users see meaningful data immediately without needing to take action.
To enable deferred filters, set the $hasDeferredFilters property to true:
hasDeferredFilters() method:
Resetting filters to defaults
When using deferred filters, a “Reset” link appears in the filter dropdown footer alongside the “Apply” button. Clicking this link restores all filters to their default values as defined in thefiltersSchema() method. For example, if you set ->default(now()->subDays(30)) on a DatePicker, the reset action will restore that default date, not an empty value.
Customizing filter actions
You may customize the apply and reset actions that appear when using deferred filters. All methods that are available to customize action trigger buttons can be used:Live updating chart data (polling)
By default, chart widgets refresh their data every 5 seconds. To customize this, you may override the$pollingInterval property on the class to a new interval:
Setting a maximum chart height
You may place a maximum height on the chart to ensure that it doesn’t get too big, using the$maxHeight property:
Setting chart configuration options
You may specify an$options variable on the chart class to control the many configuration options that the Chart.js library provides. For instance, you could turn off the legend for a line chart:
getOptions() method to return a dynamic array of options:
RawJs object. This is useful if you want to use a JavaScript callback function, for example:
Adding a description
You may add a description, below the heading of the chart, using thegetDescription() method:
Disabling lazy loading
By default, widgets are lazy-loaded. This means that they will only be loaded when they are visible on the page. To disable this behavior, you may override the$isLazy property on the widget class:
Making the chart collapsible
You may allow the chart to be collapsible by setting the$isCollapsible property on the widget class to be true:
Using custom Chart.js plugins
Chart.js offers a powerful plugin system that allows you to extend its functionality and create custom chart behaviors. This guide details how to use them in a chart widget.Step 1: Install the plugin with NPM
To start with, install the plugin using NPM into your project. In this guide, we will installchartjs-plugin-datalabels:
Step 2: Create a JavaScript file importing the plugin
Create a new JavaScript file where you will define your custom plugin. In this guide, we’ll call itfilament-chart-js-plugins.js. Import the plugin, and add it to the window.filamentChartJsPlugins array:
new Chart(..., { plugins: [...] }) when instantiating a Chart.js chart.
It’s important to initialise the array if it has not been already, before pushing onto it. This ensures that multiple JavaScript files (especially those from Filament plugins) that register Chart.js plugins do not overwrite each other, regardless of the order they are booted in.
You can push as many plugins to the array as you would like to install, you do not need a separate file to import each plugin.
Additionally, you can also register any “global plugins” which will use Chart.register([...]) in the window.filamentChartJsGlobalPlugins array:
Step 3: Compile the JavaScript file with Vite
Now, you need to build the JavaScript file with Vite, or your bundler of choice. Include the file in your Vite configuration (usuallyvite.config.js). For example:
npm run build.
Step 4: Register the JavaScript file in Filament
Filament needs to know to include this JavaScript file when rendering chart widgets. You can do this in theboot() method of a service provider like AppServiceProvider: