Overview
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 static ?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 to either danger, gray, info, primary, success or warning:
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
You can set up chart filters to change the data shown on chart. 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:
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:
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:
filamentChartJsPlugins array as you would like to install, you do not need a separate file to import each plugin.
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: