统计插件

Dashboard

开始

Filament 自带"统计概览"插件模板,可以无需自己编写视图文件显示不同的统计结果。

以下命令用来创建插件:

php artisan make:filament-widget StatsOverview --stats-overview

然后在 getCards() 方法中返回 Card 实例:

<?php
 
namespace App\Filament\Widgets;
 
use Filament\Widgets\StatsOverviewWidget as BaseWidget;
use Filament\Widgets\StatsOverviewWidget\Card;
 
class StatsOverviewWidget extends BaseWidget
{
protected function getCards(): array
{
return [
Card::make('Unique views', '192.1k'),
Card::make('Bounce rate', '21%'),
Card::make('Average time on page', '3:12'),
];
}
}

现在你可以在控制面板上查看插件了。

卡片描述及图标

你可以添加 description() 方法,提供额外信息,也可随带 descriptionIcon()

protected function getCards(): array
{
return [
Card::make('Unique views', '192.1k')
->description('32k increase')
->descriptionIcon('heroicon-s-trending-up'),
Card::make('Bounce rate', '21%')
->description('7% increase')
->descriptionIcon('heroicon-s-trending-down'),
Card::make('Average time on page', '3:12')
->description('3% increase')
->descriptionIcon('heroicon-s-trending-up'),
];
}

卡片颜色

你也可以通过 color() (primary, success, warningdanger) 指定卡片颜色:

protected function getCards(): array
{
return [
Card::make('Unique views', '192.1k')
->description('32k increase')
->descriptionIcon('heroicon-s-trending-up')
->color('success'),
Card::make('Bounce rate', '21%')
->description('7% increase')
->descriptionIcon('heroicon-s-trending-down')
->color('danger'),
Card::make('Average time on page', '3:12')
->description('3% increase')
->descriptionIcon('heroicon-s-trending-up')
->color('success'),
];
}

另外的HTML属性

使用 extraAttributes() 方法,你可以将额外的 HTML 属性传入卡片中: You may also pass extra HTML attributes to cards using extraAttributes():

protected function getCards(): array
{
return [
Card::make('Processed', '192.1k')
->color('success')
->extraAttributes([
'class' => 'cursor-pointer',
'wire:click' => '$emitUp("setStatusFilter", "processed")',
]),
// ...
];
}

卡片图表

你也可以添加或者链式调用 chart() 到每个卡片,提供历史数据。chart() 方法接收一组要绘制的点形成的数组:

protected function getCards(): array
{
return [
Card::make('Unique views', '192.1k')
->description('32k increase')
->descriptionIcon('heroicon-s-trending-up')
->chart([7, 2, 10, 3, 15, 4, 17])
->color('success'),
// ...
];
}

实时更新 (轮询)

统计概览插件默认每5秒钟刷新一次数据。

你可以通过重写类上的 $pollingInterval 属性自定义间隔时间:

protected static ?string $pollingInterval = '10s';

此外,你也可以完全禁用轮询:

protected static ?string $pollingInterval = null;

需要帮助? 加入论坛 或者打开 GitHub讨论

喜欢Filament?

Filament 中文文档由 laravel-filament.cn 翻译整理。站长用爱发电,希望为英文阅读不畅的朋友提供快速掌握Filament框架的途径。文档的翻译,社区的运营维护都需要时间精力上的付出。如果文档社区使你受益,如果你想支持站长...

打赏