namespace App\Filament\Resources\ProjectResource\Widgets;
use App\Models\Project;
use Filament\Widgets\ChartWidget;
class ActiveProjectsByProjMgrChart extends ChartWidget
{
protected static ?string $heading = 'Active Projects by PM';
protected static ?string $pollingInterval = null;
protected static ?string $maxHeight = '400px';
protected function getData(): array
{
$data = Project::query()
->join('users', 'users.id', '=', 'projects.project_manager_id')
->selectRaw('users.name, count(projects.id) as active_projects_count')
->groupBy('users.name')
->whereNull('completed_at')
->orderByDesc('active_projects_count')
->get();
$pms = $data->pluck('name')->toArray();
$colors = [];
for ($i = 1; $i <= count($pms); $i++) {
$colors[] = sprintf('#%06X', mt_rand(0, 0xFFFFFF));
}
$counts = $data->pluck('active_projects_count')->toArray();
return [
'datasets' => [
[
'label' => 'Active Projects',
'data' => $counts,
'backgroundColor' => $colors,
'hoverOffset' => 10,
],
],
'labels' => $pms,
];
}
protected function getType(): string
{
return 'pie';
}
}