simedkom/app/Filament/Widgets/MediaClassificationChart.php

60 lines
1.6 KiB
PHP

<?php
namespace App\Filament\Widgets;
use App\Enums\MediaClassification;
use App\Models\PartnerMedia;
use BezhanSalleh\FilamentShield\Traits\HasWidgetShield;
use Filament\Widgets\ChartWidget;
use Filament\Widgets\Concerns\InteractsWithPageFilters;
class MediaClassificationChart extends ChartWidget
{
use HasWidgetShield, InteractsWithPageFilters;
protected ?string $heading = 'Klasifikasi Media';
protected static ?int $sort = 3;
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 = PartnerMedia::query()
->when($startDate, fn ($q) => $q->whereDate('created_at', '>=', $startDate))
->when($endDate, fn ($q) => $q->whereDate('created_at', '<=', $endDate))
->selectRaw('classification, count(*) as total')
->groupBy('classification')
->get();
$labels = [];
$counts = [];
foreach (MediaClassification::cases() as $case) {
$labels[] = $case->getLabel();
$counts[] = $data->where('classification', $case)->first()?->total ?? 0;
}
return [
'datasets' => [
[
'label' => 'Jumlah Media',
'data' => $counts,
'backgroundColor' => '#4F46E5',
],
],
'labels' => $labels,
];
}
protected function getType(): string
{
return 'bar';
}
}