---
title: Chart widgets
---
import AutoScreenshot from "@components/AutoScreenshot.astro"
import Aside from "@components/Aside.astro"
## 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:
```bash
php artisan make:filament-widget BlogPostsChart --chart
```
There is a single `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](https://www.chartjs.org/docs) library, which Filament uses to render charts. You may use the [Chart.js documentation](https://www.chartjs.org/docs) to fully understand the possibilities to return from `getData()`, based on the chart type.
```php
[
[
'label' => 'Blog posts created',
'data' => [0, 10, 5, 2, 21, 32, 45, 74, 65, 45, 77, 89],
],
],
'labels' => ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
];
}
protected function getType(): string
{
return 'line';
}
}
```
Now, check out your widget in the dashboard.
## Available chart types
Below is a list of available chart widget classes which you may extend, and their corresponding [Chart.js](https://www.chartjs.org/docs) documentation page, for inspiration on what to return from `getData()`:
- Bar chart - [Chart.js documentation](https://www.chartjs.org/docs/latest/charts/bar)
- Bubble chart - [Chart.js documentation](https://www.chartjs.org/docs/latest/charts/bubble)
- Doughnut chart - [Chart.js documentation](https://www.chartjs.org/docs/latest/charts/doughnut)
- Line chart - [Chart.js documentation](https://www.chartjs.org/docs/latest/charts/line)
- Pie chart - [Chart.js documentation](https://www.chartjs.org/docs/latest/charts/doughnut.html#pie)
- Polar area chart - [Chart.js documentation](https://www.chartjs.org/docs/latest/charts/polar)
- Radar chart - [Chart.js documentation](https://www.chartjs.org/docs/latest/charts/radar)
- Scatter chart - [Chart.js documentation](https://www.chartjs.org/docs/latest/charts/scatter)
For example, you could use a bar chart by returning `'bar'` from the `getType()` method:
Here are examples of the other available chart types:
## Customizing the chart color
You can customize the [color](../styling/colors) of the chart data by setting the `$color` property:
```php
protected string $color = 'info';
```
If you're looking to customize the color further, or use multiple colors across multiple datasets, you can still make use of Chart.js's [color options](https://www.chartjs.org/docs/latest/general/colors.html) in the data:
```php
protected function getData(): array
{
return [
'datasets' => [
[
'label' => 'Blog posts created',
'data' => [0, 10, 5, 2, 21, 32, 45, 74, 65, 45, 77, 89],
'backgroundColor' => '#36A2EB',
'borderColor' => '#9BD0F5',
],
],
'labels' => ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
];
}
```
## Generating chart data from an Eloquent model
To generate chart data from an Eloquent model, Filament recommends that you install the `flowframe/laravel-trend` package. You can view the [documentation](https://github.com/Flowframe/laravel-trend).
Here is an example of generating chart data from a model using the `laravel-trend` package:
```php
use Flowframe\Trend\Trend;
use Flowframe\Trend\TrendValue;
protected function getData(): array
{
$data = Trend::model(BlogPost::class)
->between(
start: now()->startOfYear(),
end: now()->endOfYear(),
)
->perMonth()
->count();
return [
'datasets' => [
[
'label' => 'Blog posts',
'data' => $data->map(fn (TrendValue $value) => $value->aggregate),
],
],
'labels' => $data->map(fn (TrendValue $value) => $value->date),
];
}
```
## 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:
```php
public ?string $filter = 'today';
```
Then, define the `getFilters()` method to return an array of values and labels for your filter:
```php
protected function getFilters(): ?array
{
return [
'today' => 'Today',
'week' => 'Last week',
'month' => 'Last month',
'year' => 'This year',
];
}
```
You can use the active filter value within your `getData()` method:
```php
protected function getData(): array
{
$activeFilter = $this->filter;
// ...
}
```
### Custom filters
You can use [schema components](../schemas) to build custom filters for your chart widget. This approach offers a more flexible way to define filters.
To get started, use the `HasFiltersSchema` trait and implement the `filtersSchema()` method:
```php
use Filament\Forms\Components\DatePicker;
use Filament\Schemas\Schema;
use Filament\Widgets\ChartWidget\Concerns\HasFiltersSchema;
class BlogPostsChart extends ChartWidget
{
use HasFiltersSchema;
// ...
public function filtersSchema(Schema $schema): Schema
{
return $schema->components([
DatePicker::make('startDate')
->default(now()->subDays(30)),
DatePicker::make('endDate')
->default(now()),
]);
}
}
```
The filter values are accessible via the `$this->filters` array. You can use these values inside your `getData()` method:
```php
protected function getData(): array
{
$startDate = $this->filters['startDate'] ?? null;
$endDate = $this->filters['endDate'] ?? null;
return [
// ...
];
}
```
The `$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.
#### Deferring filter updates
By default, filters using the `filtersSchema()` 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`:
```php
use Filament\Widgets\ChartWidget\Concerns\HasFiltersSchema;
class BlogPostsChart extends ChartWidget
{
use HasFiltersSchema;
protected bool $hasDeferredFilters = true;
// ...
}
```
If you need dynamic control over whether filters are deferred, you may override the `hasDeferredFilters()` method:
```php
public function hasDeferredFilters(): bool
{
return auth()->user()->prefersDeferredFilters();
}
```
#### 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 the `filtersSchema()` 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](../actions/overview) can be used:
```php
use Filament\Actions\Action;
public function filtersApplyAction(Action $action): Action
{
return $action
->label('Update Chart')
->color('success');
}
public function filtersResetAction(Action $action): Action
{
return $action
->label('Clear Filters')
->color('danger');
}
```
## Empty state
When the `getData()` method returns an empty array, the chart widget renders an "empty state" instead of the chart.
To customize when the empty state is rendered, override the `isEmpty()` method:
```php
public function isEmpty(): bool
{
$data = $this->getCachedData();
return empty($data['datasets'][0]['data'] ?? []);
}
```
### Setting the empty state heading
To customize the heading of the empty state, set the `$emptyStateHeading` property:
```php
protected ?string $emptyStateHeading = 'No data available';
```
Alternatively, you can override the `getEmptyStateHeading()` method to return a dynamic heading:
```php
use Illuminate\Contracts\Support\Htmlable;
public function getEmptyStateHeading(): string | Htmlable
{
return "No sales yet for {$this->filter}";
}
```
### Setting the empty state description
To customize the description of the empty state, set the `$emptyStateDescription` property:
```php
protected ?string $emptyStateDescription = 'Check back later once data has been collected.';
```
Alternatively, you can override the `getEmptyStateDescription()` method to return a dynamic description:
```php
use Illuminate\Contracts\Support\Htmlable;
public function getEmptyStateDescription(): string | Htmlable | null
{
return 'Sales data will appear here once orders are placed.';
}
```
### Setting the empty state icon
To customize the [icon](../styling/icons) of the empty state, set the `$emptyStateIcon` property:
```php
use Filament\Support\Icons\Heroicon;
protected string | BackedEnum | null $emptyStateIcon = Heroicon::OutlinedChartBar;
```
Alternatively, you can override the `getEmptyStateIcon()` method to return a dynamic icon:
```php
use BackedEnum;
use Filament\Support\Icons\Heroicon;
use Illuminate\Contracts\Support\Htmlable;
public function getEmptyStateIcon(): string | BackedEnum | Htmlable
{
return Heroicon::OutlinedShoppingCart;
}
```
### Adding empty state actions
You can add [actions](../actions/overview) to the empty state to prompt users to take action by overriding the `getEmptyStateActions()` method:
```php
use Filament\Actions\Action;
public function getEmptyStateActions(): array
{
return [
Action::make('refresh')
->label('Refresh')
->action('refresh'),
];
}
```
### Using a custom empty state view
You may use a completely custom empty state view by overriding the `getEmptyState()` method:
```php
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Contracts\View\View;
public function getEmptyState(): View | Htmlable | null
{
return view('widgets.charts.custom-empty-state');
}
```
## 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:
```php
protected ?string $pollingInterval = '10s';
```
Alternatively, you may disable polling altogether:
```php
protected ?string $pollingInterval = null;
```
## 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:
```php
protected ?string $maxHeight = '300px';
```
## 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](https://www.chartjs.org/docs/latest/configuration/legend.html) for a line chart:
```php
protected ?array $options = [
'plugins' => [
'legend' => [
'display' => false,
],
],
];
```
Alternatively, you can override the `getOptions()` method to return a dynamic array of options:
```php
protected function getOptions(): array
{
return [
'plugins' => [
'legend' => [
'display' => false,
],
],
];
}
```
These PHP arrays will get transformed into JSON objects when the chart is rendered. If you want to return raw JavaScript from this method instead, you can return a `RawJs` object. This is useful if you want to use a JavaScript callback function, for example:
```php
use Filament\Support\RawJs;
protected function getOptions(): RawJs
{
return RawJs::make(<< '€' + value,
},
},
},
}
JS);
}
```
## Styling charts in a theme
Chart.js paints a chart onto a `