MIF_E31221105/app/Http/Controllers/DashboardController.php

313 lines
12 KiB
PHP

<?php
// namespace App\Http\Controllers;
// use App\Models\TahunAngkatan;
// use App\Models\DataAlumni;
// use App\Models\HasilKlasifikasi;
// use Illuminate\Support\Facades\DB;
// use Illuminate\Http\Request;
// class DashboardController extends Controller
// {
// public function index()
// {
// $angkatanList = TahunAngkatan::all();
// $totalAlumni = DataAlumni::count();
// $angkatanStats = TahunAngkatan::withCount('alumni')->get();
// $clusterStats = HasilKlasifikasi::select('cluster', DB::raw('count(*) as total'))
// ->groupBy('cluster')
// ->get();
// // Ambil data centroid per iterasi (jika disimpan)
// $centroidData = HasilKlasifikasi::select('iterasi', 'centroid_awal', 'centroid_akhir')
// ->orderBy('iterasi')
// ->get();
// return view('dashboard', compact('angkatanList','totalAlumni', 'angkatanStats', 'clusterStats', 'centroidData'));
// }
// public function filter(Request $request)
// {
// $angkatan = $request->angkatan;
// $angkatanList = TahunAngkatan::all();
// $dataAlumni = DataAlumni::with(['jabatan', 'kualifikasi_bidang', 'angkatan'])
// ->where('angkatan', $angkatan)
// ->get();
// $hasil = HasilKlasifikasi::with('alumni')
// ->whereHas('alumni', fn($q) => $q->where('angkatan', $angkatan))
// ->get();
// // Data untuk Chart Tren
// $trens = TahunAngkatan::with(['alumni.hasilKlasifikasi'])
// ->get()
// ->map(function ($item) {
// return [
// 'tahun' => $item->tahun,
// 'cluster_0' => $item->alumni->where('hasilKlasifikasi.cluster', 0)->count(),
// 'cluster_1' => $item->alumni->where('hasilKlasifikasi.cluster', 1)->count(),
// 'cluster_2' => $item->alumni->where('hasilKlasifikasi.cluster', 2)->count(),
// ];
// });
// return view('dashboard', compact('dataAlumni', 'hasil', 'angkatan', 'angkatanList', 'trens'));
// }
// }
//edit sendiri
// namespace App\Http\Controllers;
// use Illuminate\Http\Request;
// use Illuminate\Support\Facades\DB;
// class DashboardController extends Controller
// {
// public function index(Request $request)
// {
// // --- FILTER ANGKATAN ---
// $angkatanFilter = $request->input('angkatan');
// $angkatanList = DB::table('tahun_angkatan')->pluck('tahun', 'id_angkatan');
// // --- MAP CLUSTER ---
// $clusterMap = [0 => 'TIDAK SESUAI', 1 => 'KURANG SESUAI', 2 => 'SANGAT SESUAI'];
// // --- PIE CHART: Distribusi Cluster ---
// $clusterQuery = DB::table('hasil_klasifikasi')
// ->join('data_alumni', 'hasil_klasifikasi.id_alumni', '=', 'data_alumni.id_alumni')
// ->when($angkatanFilter, fn($q) => $q->where('data_alumni.angkatan', $angkatanFilter))
// ->select('hasil_klasifikasi.cluster', DB::raw('COUNT(*) as total'))
// ->groupBy('hasil_klasifikasi.cluster')
// ->get();
// $clusterLabels = array_values($clusterMap);
// $clusterValues = [];
// foreach (array_keys($clusterMap) as $key) {
// $found = $clusterQuery->firstWhere('cluster', $key);
// $clusterValues[] = $found ? $found->total : 0;
// }
// // --- LINE CHART: Profesi per Angkatan ---
// $professionTrends = DB::table('data_alumni')
// ->join('tahun_angkatan', 'data_alumni.angkatan', '=', 'tahun_angkatan.id_angkatan')
// ->select('tahun_angkatan.tahun', 'data_alumni.jenis_profesi', DB::raw('COUNT(*) as total'))
// ->groupBy('tahun_angkatan.tahun', 'data_alumni.jenis_profesi')
// ->orderBy('tahun_angkatan.tahun')
// ->get();
// $years = $professionTrends->pluck('tahun')->unique()->values()->toArray();
// $professions = [
// 'Programmer', 'Data Analyst', 'Business Intelligence Developer',
// 'Wirausaha', 'Non IT', 'Tidak diketahui', 'Tidak bekerja',
// 'Pelajar', 'Wirausaha IT', 'Infokom'
// ];
// $chartData = [];
// foreach ($professions as $profession) {
// $data = [];
// foreach ($years as $year) {
// $match = $professionTrends->firstWhere(fn($row) =>
// $row->tahun == $year && $row->jenis_profesi == $profession
// );
// $data[] = $match ? $match->total : 0;
// }
// $chartData[] = ['name' => $profession, 'data' => $data];
// }
// // --- PROFESI TERBANYAK ---
// $topJob = DB::table('data_alumni')
// ->when($angkatanFilter, fn($q) => $q->where('angkatan', $angkatanFilter))
// ->select('jenis_profesi', DB::raw('COUNT(*) as total'))
// ->whereNotNull('jenis_profesi')
// ->groupBy('jenis_profesi')
// ->orderByDesc('total')
// ->first();
// // --- TOTAL DATA ---
// $totalAlumniCount = DB::table('data_alumni')
// ->when($angkatanFilter, fn($q) => $q->where('angkatan', $angkatanFilter))
// ->count();
// $totalAngkatanCount = DB::table('tahun_angkatan')->count();
// // --- CLUSTER CENTERS (Dummy untuk Scatter Plot) ---
// $clusterCenters = [
// 0 => ['x' => 1, 'y' => 0],
// 1 => ['x' => 3, 'y' => 3],
// 2 => ['x' => 6, 'y' => 6],
// ];
// // --- SCATTER PLOT DATA ---
// $scatterData = [];
// try {
// $alumni = DB::table('hasil_klasifikasi')
// ->join('data_alumni', 'hasil_klasifikasi.id_alumni', '=', 'data_alumni.id_alumni')
// ->join('kualifikasi_bidang', 'data_alumni.kualifikasi_bidang_kerja', '=', 'kualifikasi_bidang.id_kualifikasi')
// ->select(
// 'hasil_klasifikasi.cluster',
// 'data_alumni.nama',
// 'data_alumni.jenis_profesi',
// 'kualifikasi_bidang.nilai_kompetensi as x_value',
// 'kualifikasi_bidang.nilai_kesesuaian as y_value'
// )
// ->when($angkatanFilter, fn($q) => $q->where('data_alumni.angkatan', $angkatanFilter))
// ->get();
// foreach ($alumni as $a) {
// $cluster = $a->cluster;
// $center = $clusterCenters[$cluster] ?? ['x' => 0, 'y' => 0];
// $x = $a->x_value ?? max(0, min(100, $center['x'] + mt_rand(-15, 15)));
// $y = $a->y_value ?? max(0, min(100, $center['y'] + mt_rand(-15, 15)));
// $scatterData[$cluster][] = [
// 'x' => $x,
// 'y' => $y,
// 'name' => $a->nama . ' (' . ($a->jenis_profesi ?? 'Unknown') . ')'
// ];
// }
// } catch (\Exception $e) {
// // Fallback dummy data jika gagal mengambil data asli
// foreach ($clusterMap as $cluster => $label) {
// $center = $clusterCenters[$cluster] ?? ['x' => 0, 'y' => 0];
// for ($i = 0; $i < mt_rand(25, 50); $i++) {
// $scatterData[$cluster][] = [
// 'x' => max(0, min(100, $center['x'] + mt_rand(-15, 15))),
// 'y' => max(0, min(100, $center['y'] + mt_rand(-15, 15))),
// 'name' => 'Alumni ' . ($i + 1)
// ];
// }
// }
// }
// return view('dashboard', [
// 'chartYears' => $years,
// 'chartData' => $chartData,
// 'angkatanList' => $angkatanList,
// 'angkatanFilter' => $angkatanFilter,
// 'clusterLabels' => $clusterLabels,
// 'clusterValues' => $clusterValues,
// 'topJobName' => $topJob->jenis_profesi ?? 'Belum Ada Data',
// 'topJobCount' => $topJob->total ?? 0,
// 'totalAlumniCount' => $totalAlumniCount,
// 'totalAngkatanCount' => $totalAngkatanCount,
// 'scatterData' => $scatterData
// ]);
// }
// }
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class DashboardController extends Controller
{
public function index(Request $request)
{
// --- FILTER ANGKATAN UNTUK PIE CHART ---
$angkatanFilter = $request->input('angkatan');
$angkatanList = DB::table('tahun_angkatan')->pluck('tahun', 'id_angkatan');
// Data untuk Pie Chart Cluster
$query = DB::table('hasil_klasifikasi')
->join('data_alumni', 'hasil_klasifikasi.id_alumni', '=', 'data_alumni.id_alumni')
->join('tahun_angkatan', 'data_alumni.angkatan', '=', 'tahun_angkatan.id_angkatan')
->select('hasil_klasifikasi.cluster', DB::raw('COUNT(*) as total'))
->groupBy('hasil_klasifikasi.cluster');
if ($angkatanFilter) {
$query->where('data_alumni.angkatan', $angkatanFilter);
}
$clusterData = $query->get();
$clusterMap = [0 => 'TIDAK SESUAI', 1 => 'KURANG SESUAI', 2 => 'SANGAT SESUAI'];
$clusterLabels = [];
$clusterValues = [];
foreach ($clusterMap as $key => $label) {
$clusterLabels[] = $label;
$found = $clusterData->firstWhere('cluster', $key);
$clusterValues[] = $found ? $found->total : 0;
}
// --- LINE CHART: PROFESI PER ANGKATAN ---
$professionTrends = DB::table('data_alumni')
->join('tahun_angkatan', 'data_alumni.angkatan', '=', 'tahun_angkatan.id_angkatan')
->select('tahun_angkatan.tahun', 'data_alumni.jenis_profesi', DB::raw('COUNT(*) as total'))
->groupBy('tahun_angkatan.tahun', 'data_alumni.jenis_profesi')
->orderBy('tahun_angkatan.tahun')
->get();
$years = $professionTrends->pluck('tahun')->unique()->values()->toArray();
$professions = [
'Programmer',
'Data Analyst',
'Business Intelligence Developer',
'Wirausaha',
'Non IT',
'Tidak diketahui',
'Tidak bekerja',
'Pelajar',
'Wirausaha IT',
'Infokom'
];
$chartData = [];
foreach ($professions as $profession) {
$professionData = ['name' => $profession, 'data' => []];
foreach ($years as $year) {
$count = $professionTrends->firstWhere(fn($item) => $item->tahun == $year && $item->jenis_profesi == $profession);
$professionData['data'][] = $count ? $count->total : 0;
}
$chartData[] = $professionData;
}
// --- CARD: PROFESI PALING BANYAK DITEKUNI ---
// --- CARD: PROFESI PALING BANYAK DITEKUNI ---
$topJobQuery = DB::table('data_alumni')
->join('tahun_angkatan', 'data_alumni.angkatan', '=', 'tahun_angkatan.id_angkatan')
->select('data_alumni.jenis_profesi', DB::raw('COUNT(*) as total'))
->when($angkatanFilter, function ($query) use ($angkatanFilter) {
return $query->where('data_alumni.angkatan', $angkatanFilter);
})
->whereNotNull('data_alumni.jenis_profesi')
->groupBy('data_alumni.jenis_profesi')
->orderByDesc('total')
->first();
$angkatanFilter = $request->query('angkatan');
$totalAlumni = DB::table('data_alumni');
if ($angkatanFilter) {
$totalAlumni->where('angkatan', $angkatanFilter);
}
$totalAlumniCount = $totalAlumni->count();
$totalAngkatanCount = DB::table('tahun_angkatan')->count();
return view('dashboard', [
'chartYears' => $years,
'chartData' => $chartData,
'angkatanList' => $angkatanList,
'angkatanFilter' => $angkatanFilter,
'clusterLabels' => $clusterLabels,
'clusterValues' => $clusterValues,
'topJobName' => $topJobQuery->jenis_profesi ?? 'Belum Ada Data',
'topJobCount' => $topJobQuery->total ?? 0,
'totalAlumniCount' => $totalAlumniCount,
'totalAngkatanCount' => $totalAngkatanCount,
]);
}
}