|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace App\Filament\Resources\UserResource\Widgets; |
| 6 | + |
| 7 | +use App\Models\User; |
| 8 | +use Filament\Widgets\ChartWidget; |
| 9 | +use Flowframe\Trend\Trend; |
| 10 | +use Flowframe\Trend\TrendValue; |
| 11 | + |
| 12 | +final class UserChartWidget extends ChartWidget |
| 13 | +{ |
| 14 | + protected static ?string $heading = 'Création de compte'; |
| 15 | + |
| 16 | + protected int|string|array $columnSpan = 'full'; |
| 17 | + |
| 18 | + protected static ?string $maxHeight = '200px'; |
| 19 | + |
| 20 | + public ?string $filter = 'week'; |
| 21 | + |
| 22 | + protected function getColumns(): int |
| 23 | + { |
| 24 | + return 2; |
| 25 | + } |
| 26 | + |
| 27 | + protected function getFilters(): array |
| 28 | + { |
| 29 | + return [ |
| 30 | + 'week' => 'Last Week', |
| 31 | + 'month' => 'Last Month', |
| 32 | + '3months' => 'Last 3 Months', |
| 33 | + ]; |
| 34 | + } |
| 35 | + |
| 36 | + protected function getData(): array |
| 37 | + { |
| 38 | + match ($this->filter) { // @phpstan-ignore-line |
| 39 | + 'week' => $data = Trend::model(User::class) |
| 40 | + ->between( |
| 41 | + start: now()->subWeeks(), |
| 42 | + end: now() |
| 43 | + ) |
| 44 | + ->perDay() |
| 45 | + ->count(), |
| 46 | + |
| 47 | + 'month' => $data = Trend::model(User::class) |
| 48 | + ->between( |
| 49 | + start: now()->subMonth(), |
| 50 | + end: now() |
| 51 | + ) |
| 52 | + ->perDay() |
| 53 | + ->count(), |
| 54 | + |
| 55 | + '3months' => $data = Trend::model(User::class) |
| 56 | + ->between( |
| 57 | + start: now()->subMonths(3), |
| 58 | + end: now() |
| 59 | + ) |
| 60 | + ->perMonth() |
| 61 | + ->count(), |
| 62 | + }; |
| 63 | + |
| 64 | + return [ |
| 65 | + 'datasets' => [ |
| 66 | + [ |
| 67 | + 'label' => 'Compte créé', |
| 68 | + 'data' => $data->map(fn (TrendValue $value) => $value->aggregate), // @phpstan-ignore-line |
| 69 | + ], |
| 70 | + ], |
| 71 | + 'labels' => $data->map(fn (TrendValue $value) => $value->date), // @phpstan-ignore-line |
| 72 | + ]; |
| 73 | + } |
| 74 | + |
| 75 | + protected function getType(): string |
| 76 | + { |
| 77 | + return 'line'; |
| 78 | + } |
| 79 | +} |
0 commit comments