feat: add Popular News and Visitor Traffic Filament chart widgets

This commit is contained in:
Yoga Pangestu 2026-01-29 10:23:18 +07:00
parent 416739e25f
commit 56535076ad
2 changed files with 86 additions and 0 deletions

View File

@ -0,0 +1,39 @@
<?php
namespace App\Filament\Widgets;
use App\Models\News;
use Filament\Widgets\ChartWidget;
class PopularNewsChart extends ChartWidget
{
protected ?string $heading = 'Berita Terpopuler';
protected static ?int $sort = 13;
protected int|string|array $columnSpan = 'full';
protected function getData(): array
{
$data = News::query()
->orderByDesc('views')
->limit(10)
->get();
return [
'datasets' => [
[
'label' => 'Total Dilihat',
'data' => $data->pluck('views')->toArray(),
'backgroundColor' => '#EC4899',
],
],
'labels' => $data->pluck('title')->map(fn ($t) => str($t)->limit(30))->toArray(),
];
}
protected function getType(): string
{
return 'bar';
}
}

View File

@ -0,0 +1,47 @@
<?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';
}
}