60 lines
1.8 KiB
PHP
60 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Widgets\Charts;
|
|
|
|
use App\Models\Visitor;
|
|
use BezhanSalleh\FilamentShield\Traits\HasWidgetShield;
|
|
use Carbon\Carbon;
|
|
use Filament\Widgets\ChartWidget;
|
|
use Filament\Widgets\Concerns\InteractsWithPageFilters;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class VisitorTrafficChart extends ChartWidget
|
|
{
|
|
use HasWidgetShield, InteractsWithPageFilters;
|
|
|
|
protected ?string $heading = 'Traffic Pengunjung';
|
|
|
|
protected static ?int $sort = 12;
|
|
|
|
protected int|string|array $columnSpan = 1;
|
|
|
|
protected ?string $pollingInterval = null;
|
|
|
|
protected function getData(): array
|
|
{
|
|
$startDate = $this->filters['startDate'] ?? null;
|
|
$endDate = $this->filters['endDate'] ?? null;
|
|
|
|
$data = Visitor::query()
|
|
->select(
|
|
DB::raw('SUM(counter) as total'),
|
|
DB::raw("DATE_FORMAT(date, '%Y-%m-%d') as day")
|
|
)
|
|
->when($startDate, fn ($q) => $q->whereDate('date', '>=', $startDate))
|
|
->when(! $startDate, fn ($q) => $q->where('date', '>=', now()->subDays(30)))
|
|
->when($endDate, fn ($q) => $q->whereDate('date', '<=', $endDate))
|
|
->groupBy('day')
|
|
->orderBy('day')
|
|
->get();
|
|
|
|
return [
|
|
'datasets' => [
|
|
[
|
|
'label' => 'Total Kunjungan',
|
|
'data' => $data->pluck('total')->toArray(),
|
|
'fill' => 'start',
|
|
'borderColor' => '#3B82F6',
|
|
'backgroundColor' => 'rgba(59, 130, 246, 0.1)',
|
|
],
|
|
],
|
|
'labels' => $data->pluck('day')->map(fn ($d) => Carbon::parse($d)->translatedFormat('d M'))->toArray(),
|
|
];
|
|
}
|
|
|
|
protected function getType(): string
|
|
{
|
|
return 'line';
|
|
}
|
|
}
|