--- 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; // ... } ```