统计概览 Widget
简介
Filament 自带一个 "统计概览" Widget 模板,用来在单个 Widget 中展示许多不同状态,而无需编写自定义视图。
使用以下命令创建 Widget:
php artisan make:filament-widget StatsOverview --stats-overview
该命令将会创建一个新的 StatsOverview.php 文件。打开该文件,然后在 getStats() 方法中返回 Stat 实例:
<?php
namespace App\Filament\Widgets;
use Filament\Widgets\StatsOverviewWidget as BaseWidget;
use Filament\Widgets\StatsOverviewWidget\Stat;
class StatsOverview extends BaseWidget
{
protected function getStats(): array
{
return [
Stat::make('Unique views', '192.1k'),
Stat::make('Bounce rate', '21%'),
Stat::make('Average time on page', '3:12'),
];
}
}
现在,你可以到仪表盘上去查看 Widget 了。

添加描述及图标
你可以使用 description() 方法提供额外的描述信息,同时也可以通过 descriptionIcon() 添加描述图标:
use Filament\Widgets\StatsOverviewWidget\Stat;
protected function getStats(): array
{
return [
Stat::make('Unique views', '192.1k')
->description('32k increase')
->descriptionIcon('heroicon-m-arrow-trending-up'),
Stat::make('Bounce rate', '21%')
->description('7% decrease')
->descriptionIcon('heroicon-m-arrow-trending-down'),
Stat::make('Average time on page', '3:12')
->description('3% increase')
->descriptionIcon('heroicon-m-arrow-trending-up'),
];
}
descriptionIcon() 方法也接收第二个参数,以将图标放到描述之前而非之后:
use Filament\Support\Enums\IconPosition;
use Filament\Widgets\StatsOverviewWidget\Stat;
Stat::make('Unique views', '192.1k')
->description('32k increase')
->descriptionIcon('heroicon-m-arrow-trending-up', IconPosition::Before)

修改 Stat 颜色
你也可以给状态一个颜色:
use Filament\Widgets\StatsOverviewWidget\Stat;
protected function getStats(): array
{
return [
Stat::make('Unique views', '192.1k')
->description('32k increase')
->descriptionIcon('heroicon-m-arrow-trending-up')
->color('success'),
Stat::make('Bounce rate', '21%')
->description('7% increase')
->descriptionIcon('heroicon-m-arrow-trending-down')
->color('danger'),
Stat::make('Average time on page', '3:12')
->description('3% increase')
->descriptionIcon('heroicon-m-arrow-trending-up')
->color('success'),
];
}

添加额外 HTML 属性到 Stat
使用 extraAttributes() 方法,你可以将额外的 HTML 属性传入 Stat 中:
use Filament\Widgets\StatsOverviewWidget\Stat;
protected function getStats(): array
{
return [
Stat::make('Processed', '192.1k')
->color('success')
->extraAttributes([
'class' => 'cursor-pointer',
'wire:click' => "\$dispatch('setStatusFilter', { filter: 'processed' })",
]),
// ...
];
}
本例中,我们刻意转义了 $dispatch() 中的 $,因为它不是 PHP 变量,而要将其直接传入到 HTML 中。
设置统计信息的占位符
有时,统计信息的值可能不可用 —— 例如,当仪表板在特定时间段内没有任何数据可显示时。你可以使用 placeholder() 函数来定义当值为空时应该显示的内容:
use Filament\Widgets\StatsOverviewWidget\Stat;
Stat::make('Unique views', $uniqueViews)
->placeholder('-')
根据 Laravel 的 blank() 辅助函数,值被视为空白。例如,null 和空字符串被视为空白,而 0 和 '0' 则不是。
设置全局默认占位符
如果你希望应用中的所有统计信息默认都使用相同的占位符,可以在服务提供者的 boot() 方法中使用静态的 configureUsing() 方法:
use Filament\Widgets\StatsOverviewWidget\Stat;
Stat::configureUsing(function (Stat $component): void {
$component->placeholder('-');
});
个别统计数据仍然可以通过调用 placeholder() 来重写此设置。
添加图表到 Stat
你也可以添加或者链式调用 chart() 到每个 Stat,以提供历史数据。chart() 方法接收一组要绘制的点形成的数组:
use Filament\Widgets\StatsOverviewWidget\Stat;
protected function getStats(): array
{
return [
Stat::make('Unique views', '192.1k')
->description('32k increase')
->descriptionIcon('heroicon-m-arrow-trending-up')
->chart([7, 2, 10, 3, 15, 4, 17])
->color('success'),
// ...
];
}

设置主题中统计图表的样式
Chart.js 将统计图表绘制到 <canvas> 上,因此无法通过样式表访问其线条。自定义主题 仅使用 CSS,因此 Filament 将该行的形状公开为 CSS 自定义属性,你可以将其设置在 .fi-wi-stats-overview-stat 元素上,或设置在它上面的任何元素上,以便一次性覆盖面板中的所有统 计信息:
.fi-wi-stats-overview-stat {
--stat-chart-border-width: 1;
--stat-chart-line-tension: 0;
--stat-chart-fill: none;
}
--stat-chart-border-width 参数用于加粗线条,--stat-chart-line-tension 参数用于 调整线条的弯曲程度,数值从 0(直线段)到 1 不等,--stat-chart-fill 参数用于填充线条下方的区域,可接受的参数包括 start、end、origin 和 stack,也可以设置为 none 以保持线条裸露。
这些参数值会传递给 Chart.js,而不是由浏览器使用,因此它们是纯数字和关键字,没有单位。如果你将某个参数设置为 Chart.js 无法使用的值,则会被忽略,图表将保持默认设置。此外,当颜色方案更改时,这些参数也会被重新读取,因此您可以为浅色模式和深色模式设置不同的值。
图表的颜色取自统计信息的颜色。要在主题中更改它们,请使用普通的 color 声明来设置 .fi-wi-stats-overview-stat-chart-bg-color 和 .fi-wi-stats-overview-stat-chart-border-color 元素的样式:
.fi-wi-stats-overview-stat {
& .fi-wi-stats-overview-stat-chart-border-color {
@apply text-gray-400 dark:text-gray-500;
}
}
NOTE
这些属性仅影响统计信息中的图表。图表 Widget使用它们自己的一组样式,以 --chart- 为前缀。
实时更新统计数据(轮询)
默认情况下,统计概述 Widget 每 5 秒钟刷新一次数据。
你可以通过重写类上的 $pollingInterval 属性自定义间隔时间:
protected ?string $pollingInterval = '10s';
此外,你也可以完全禁用轮询:
protected ?string $pollingInterval = null;
禁用懒加载
默认情况下,Widget 使用懒加载。这意味着只有当它们在页面中可见时才会加载。
要禁用该行为,你可以在 Widget 类中重写 $isLazy 属性:
protected static bool $isLazy = false;
添加标题和描述
你也可以通过重写 $heading 和 $description 属性在 Widget 上方添加标题和描述文本:
protected ?string $heading = 'Analytics';
protected ?string $description = 'An overview of some analytics.';
如果你需要动态生成标题或描述文本,你可以 重写 getHeading() 和 getDescription() 方法:
protected function getHeading(): ?string
{
return 'Analytics';
}
protected function getDescription(): ?string
{
return 'An overview of some analytics.';
}





