StatsOverviewWidget.php
TLDR
This file defines a class called StatsOverviewWidget
that extends another class called BaseWidget
. The class contains a method called getStats()
that returns an array of statistics.
Methods
getStats
This method retrieves statistics based on a set of filters. It calculates the revenue, new customers, and new orders based on the start and end date filters, as well as the business customers filter. It uses a number formatting function to format the numbers. The method returns an array of Stat
objects, each representing a statistic.
Classes
None
<?php
namespace App\Filament\Widgets;
use Carbon\Carbon;
use Filament\Widgets\Concerns\InteractsWithPageFilters;
use Filament\Widgets\StatsOverviewWidget as BaseWidget;
use Filament\Widgets\StatsOverviewWidget\Stat;
use Illuminate\Support\Number;
class StatsOverviewWidget extends BaseWidget
{
use InteractsWithPageFilters;
protected static ?int $sort = 0;
protected function getStats(): array
{
$startDate = filled($this->filters['startDate'] ?? null) ?
Carbon::parse($this->filters['startDate']) :
null;
$endDate = filled($this->filters['endDate'] ?? null) ?
Carbon::parse($this->filters['endDate']) :
now();
$isBusinessCustomersOnly = $this->filters['businessCustomersOnly'] ?? null;
$businessCustomerMultiplier = match (true) {
boolval($isBusinessCustomersOnly) => 2 / 3,
blank($isBusinessCustomersOnly) => 1,
default => 1 / 3,
};
$diffInDays = $startDate ? $startDate->diffInDays($endDate) : 0;
$revenue = ($startDate ? ($diffInDays * 137) : 192100) * $businessCustomerMultiplier;
$newCustomers = ($startDate ? ($diffInDays * 7) : 1340) * $businessCustomerMultiplier;
$newOrders = ($startDate ? ($diffInDays * 13) : 3543) * $businessCustomerMultiplier;
$formatNumber = function (int $number): string {
if ($number < 1000) {
return Number::format($number, 0);
}
if ($number < 1000000) {
return Number::format($number / 1000, 2) . 'k';
}
return Number::format($number / 1000000, 2) . 'm';
};
return [
Stat::make('Revenue', '$' . $formatNumber($revenue))
->description('32k increase')
->descriptionIcon('heroicon-m-arrow-trending-up')
->chart([7, 2, 10, 3, 15, 4, 17])
->color('success'),
Stat::make('New customers', $formatNumber($newCustomers))
->description('3% decrease')
->descriptionIcon('heroicon-m-arrow-trending-down')
->chart([17, 16, 14, 15, 14, 13, 12])
->color('danger'),
Stat::make('New orders', $formatNumber($newOrders))
->description('7% increase')
->descriptionIcon('heroicon-m-arrow-trending-up')
->chart([15, 4, 10, 2, 12, 4, 12])
->color('success'),
];
}
}