feat: introduce Issue Sentiment By Department chart to the Analytic page, relocating Company Status chart from Dashboard and adding Cooperation Status chart.
This commit is contained in:
parent
01db1c4794
commit
0189ef874d
@ -3,9 +3,11 @@
|
|||||||
namespace App\Filament\Pages;
|
namespace App\Filament\Pages;
|
||||||
|
|
||||||
use App\Enums\RoleEnum;
|
use App\Enums\RoleEnum;
|
||||||
|
use App\Filament\Widgets\Charts\CompanyStatusChart;
|
||||||
use App\Filament\Widgets\Charts\CooperationStatusChart;
|
use App\Filament\Widgets\Charts\CooperationStatusChart;
|
||||||
use App\Filament\Widgets\Charts\IssueDepartmentChart;
|
use App\Filament\Widgets\Charts\IssueDepartmentChart;
|
||||||
use App\Filament\Widgets\Charts\IssueLocationChart;
|
use App\Filament\Widgets\Charts\IssueLocationChart;
|
||||||
|
use App\Filament\Widgets\Charts\IssueSentimentByDepartmentChart;
|
||||||
use App\Filament\Widgets\Charts\JournalistRegistrationChart;
|
use App\Filament\Widgets\Charts\JournalistRegistrationChart;
|
||||||
use App\Filament\Widgets\Charts\MediaClassificationChart;
|
use App\Filament\Widgets\Charts\MediaClassificationChart;
|
||||||
use App\Filament\Widgets\Charts\MediaCooperationRankingChart;
|
use App\Filament\Widgets\Charts\MediaCooperationRankingChart;
|
||||||
@ -98,8 +100,10 @@ public function resetFilters(): void
|
|||||||
public function getWidgets(): array
|
public function getWidgets(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
|
CompanyStatusChart::class,
|
||||||
CooperationStatusChart::class,
|
CooperationStatusChart::class,
|
||||||
IssueDepartmentChart::class,
|
IssueDepartmentChart::class,
|
||||||
|
IssueSentimentByDepartmentChart::class,
|
||||||
IssueLocationChart::class,
|
IssueLocationChart::class,
|
||||||
JournalistRegistrationChart::class,
|
JournalistRegistrationChart::class,
|
||||||
MediaClassificationChart::class,
|
MediaClassificationChart::class,
|
||||||
|
|||||||
@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
namespace App\Filament\Pages;
|
namespace App\Filament\Pages;
|
||||||
|
|
||||||
use App\Filament\Widgets\Charts\CompanyStatusChart;
|
|
||||||
use App\Filament\Widgets\Stats\OverviewStats;
|
use App\Filament\Widgets\Stats\OverviewStats;
|
||||||
use BezhanSalleh\FilamentShield\Traits\HasPageShield;
|
use BezhanSalleh\FilamentShield\Traits\HasPageShield;
|
||||||
use Filament\Pages\Dashboard as BaseDashboard;
|
use Filament\Pages\Dashboard as BaseDashboard;
|
||||||
@ -22,7 +21,6 @@ public function getWidgets(): array
|
|||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
OverviewStats::class,
|
OverviewStats::class,
|
||||||
CompanyStatusChart::class,
|
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
124
app/Filament/Widgets/Charts/IssueSentimentByDepartmentChart.php
Normal file
124
app/Filament/Widgets/Charts/IssueSentimentByDepartmentChart.php
Normal file
@ -0,0 +1,124 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Filament\Widgets\Charts;
|
||||||
|
|
||||||
|
use App\Enums\IssueSentiment;
|
||||||
|
use App\Models\Department;
|
||||||
|
use App\Models\IssueManagement;
|
||||||
|
use BezhanSalleh\FilamentShield\Traits\HasWidgetShield;
|
||||||
|
use Filament\Widgets\ChartWidget;
|
||||||
|
use Filament\Widgets\Concerns\InteractsWithPageFilters;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
|
||||||
|
class IssueSentimentByDepartmentChart extends ChartWidget
|
||||||
|
{
|
||||||
|
use HasWidgetShield, InteractsWithPageFilters;
|
||||||
|
|
||||||
|
protected ?string $heading = 'Perbandingan Sentimen Isu Per Perangkat Daerah (Top 10)';
|
||||||
|
|
||||||
|
protected static ?int $sort = 17;
|
||||||
|
|
||||||
|
protected int|string|array $columnSpan = 'full';
|
||||||
|
|
||||||
|
protected ?string $pollingInterval = null;
|
||||||
|
|
||||||
|
protected function getData(): array
|
||||||
|
{
|
||||||
|
$startDate = $this->filters['startDate'] ?? null;
|
||||||
|
$endDate = $this->filters['endDate'] ?? null;
|
||||||
|
|
||||||
|
$issueTable = (new IssueManagement)->getTable();
|
||||||
|
$departmentTable = (new Department)->getTable();
|
||||||
|
$pivotTable = 'department_issue_management';
|
||||||
|
|
||||||
|
// Get Top 10 Departments by total issues
|
||||||
|
$topDepartments = Department::query()
|
||||||
|
->join($pivotTable, "{$departmentTable}.id", '=', "{$pivotTable}.department_id")
|
||||||
|
->join($issueTable, "{$pivotTable}.issue_management_id", '=', "{$issueTable}.id")
|
||||||
|
->when($startDate, fn ($q) => $q->whereDate("{$issueTable}.created_at", '>=', $startDate))
|
||||||
|
->when($endDate, fn ($q) => $q->whereDate("{$issueTable}.created_at", '<=', $endDate))
|
||||||
|
->select("{$departmentTable}.id", "{$departmentTable}.name", DB::raw('count(*) as total'))
|
||||||
|
->groupBy("{$departmentTable}.id", "{$departmentTable}.name")
|
||||||
|
->orderByDesc('total')
|
||||||
|
->limit(10)
|
||||||
|
->get();
|
||||||
|
|
||||||
|
if ($topDepartments->isEmpty()) {
|
||||||
|
return [
|
||||||
|
'datasets' => [],
|
||||||
|
'labels' => [],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
$labels = $topDepartments->pluck('name')->toArray();
|
||||||
|
$departmentIds = $topDepartments->pluck('id')->toArray();
|
||||||
|
|
||||||
|
// Get counts per sentiment for these departments
|
||||||
|
$rawCounts = IssueManagement::query()
|
||||||
|
->join($pivotTable, "{$issueTable}.id", '=', "{$pivotTable}.issue_management_id")
|
||||||
|
->whereIn("{$pivotTable}.department_id", $departmentIds)
|
||||||
|
->when($startDate, fn ($q) => $q->whereDate("{$issueTable}.created_at", '>=', $startDate))
|
||||||
|
->when($endDate, fn ($q) => $q->whereDate("{$issueTable}.created_at", '<=', $endDate))
|
||||||
|
->select("{$pivotTable}.department_id", 'issue', DB::raw('count(*) as total'))
|
||||||
|
->groupBy("{$pivotTable}.department_id", 'issue')
|
||||||
|
->get();
|
||||||
|
|
||||||
|
$datasets = [];
|
||||||
|
$sentiments = IssueSentiment::cases();
|
||||||
|
|
||||||
|
foreach ($sentiments as $sentiment) {
|
||||||
|
$data = [];
|
||||||
|
foreach ($departmentIds as $id) {
|
||||||
|
$count = $rawCounts->where('department_id', $id)
|
||||||
|
->where('issue', $sentiment->value)
|
||||||
|
->first();
|
||||||
|
$data[] = $count ? $count->total : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
$datasets[] = [
|
||||||
|
'label' => $sentiment->getLabel(),
|
||||||
|
'data' => $data,
|
||||||
|
'backgroundColor' => $this->getHexColor($sentiment),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
'datasets' => $datasets,
|
||||||
|
'labels' => $labels,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getHexColor(IssueSentiment $sentiment): string
|
||||||
|
{
|
||||||
|
return match ($sentiment) {
|
||||||
|
IssueSentiment::POSITIVE => '#10B981', // green-500
|
||||||
|
IssueSentiment::NEGATIVE => '#EF4444', // red-500
|
||||||
|
IssueSentiment::NEUTRAL => '#6B7280', // gray-500
|
||||||
|
IssueSentiment::CRISIS => '#F97316', // orange-500
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getType(): string
|
||||||
|
{
|
||||||
|
return 'bar';
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getOptions(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'plugins' => [
|
||||||
|
'legend' => [
|
||||||
|
'display' => true,
|
||||||
|
],
|
||||||
|
],
|
||||||
|
'scales' => [
|
||||||
|
'x' => [
|
||||||
|
'stacked' => true,
|
||||||
|
],
|
||||||
|
'y' => [
|
||||||
|
'stacked' => true,
|
||||||
|
],
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -140,6 +140,8 @@ public function run(): void
|
|||||||
"Replicate:IssueManagement",
|
"Replicate:IssueManagement",
|
||||||
"Reorder:IssueManagement",
|
"Reorder:IssueManagement",
|
||||||
|
|
||||||
|
"View:IssueSentimentByDepartmentChart",
|
||||||
|
|
||||||
"View:JournalistRegistrationChart",
|
"View:JournalistRegistrationChart",
|
||||||
|
|
||||||
"View:LogTable",
|
"View:LogTable",
|
||||||
@ -405,6 +407,8 @@ public function run(): void
|
|||||||
"Replicate:IssueManagement",
|
"Replicate:IssueManagement",
|
||||||
"Reorder:IssueManagement",
|
"Reorder:IssueManagement",
|
||||||
|
|
||||||
|
"View:IssueSentimentByDepartmentChart",
|
||||||
|
|
||||||
"View:JournalistRegistrationChart",
|
"View:JournalistRegistrationChart",
|
||||||
|
|
||||||
"ViewAny:Location",
|
"ViewAny:Location",
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user