simedkom/app/Filament/Widgets/PopularNewsChart.php

51 lines
1.4 KiB
PHP

<?php
namespace App\Filament\Widgets;
use App\Models\News;
use BezhanSalleh\FilamentShield\Traits\HasWidgetShield;
use Filament\Widgets\ChartWidget;
use Filament\Widgets\Concerns\InteractsWithPageFilters;
class PopularNewsChart extends ChartWidget
{
use HasWidgetShield, InteractsWithPageFilters;
protected ?string $heading = 'Berita Terpopuler';
protected static ?int $sort = 13;
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 = News::query()
->when($startDate, fn ($q) => $q->whereDate('created_at', '>=', $startDate))
->when($endDate, fn ($q) => $q->whereDate('created_at', '<=', $endDate))
->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';
}
}