simedkom/app/Filament/Widgets/VisitorTrafficChart.php

48 lines
1.3 KiB
PHP

<?php
namespace App\Filament\Widgets;
use App\Models\Visitor;
use Filament\Widgets\ChartWidget;
use Illuminate\Support\Facades\DB;
class VisitorTrafficChart extends ChartWidget
{
protected ?string $heading = 'Traffic Pengunjung (30 Hari Terakhir)';
protected static ?int $sort = 12;
protected int|string|array $columnSpan = 'full';
protected function getData(): array
{
$data = Visitor::query()
->select(
DB::raw('SUM(counter) as total'),
DB::raw("DATE_FORMAT(date, '%Y-%m-%d') as day")
)
->where('date', '>=', now()->subDays(30))
->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\Carbon::parse($d)->translatedFormat('d M'))->toArray(),
];
}
protected function getType(): string
{
return 'line';
}
}