diff --git a/Controllers/Main/DashboardController.cs b/Controllers/Main/DashboardController.cs index ba13442..4c638f4 100644 --- a/Controllers/Main/DashboardController.cs +++ b/Controllers/Main/DashboardController.cs @@ -1,4 +1,5 @@ -using Microsoft.AspNetCore.Mvc; +using BankSampahApp.Models; +using Microsoft.AspNetCore.Mvc; namespace BankSampahApp.Controllers.Main { @@ -7,12 +8,69 @@ namespace BankSampahApp.Controllers.Main { public IActionResult Index() { - return View("~/Views/Main/Dashboard/Index.cshtml"); + var model = BuildDashboardChartModel(); + return View("~/Views/Main/Dashboard/Index.cshtml", model); } public IActionResult Bsu() { return View("~/Views/Main/Dashboard/Bsu.cshtml"); } + + /// + /// Dummy chart data agar view dapat menampilkan grafik tanpa ketergantungan pada API. + /// + private static DashboardChartViewModel BuildDashboardChartModel() + { + var monthLabels = new List + { + "Januari", "Februari", "Maret", "April", "Mei", "Juni", + "Juli", "Agustus", "September", "Oktober", "November", "Desember" + }; + + var baseDatasets = new Dictionary> + { + ["BSI"] = new() { 520, 540, 560, 590, 620, 650, 680, 710, 740, 770, 800, 830 }, + ["BSU"] = new() { 610, 630, 640, 670, 690, 720, 760, 780, 810, 830, 860, 890 }, + ["Offtaker"] = new() { 1050, 1080, 1100, 1140, 1180, 1220, 1260, 1300, 1340, 1380, 1420, 1480 } + }; + + var years = Enumerable.Range(2020, 6).ToList(); + var firstYear = years.Min(); + var yearlyData = new Dictionary(); + + foreach (var year in years) + { + var offset = year - firstYear; + var payload = new DashboardChartPayload + { + Labels = new List(monthLabels) + }; + + foreach (var dataset in baseDatasets) + { + var random = new Random(HashCode.Combine(year, dataset.Key)); + payload.Datasets.Add(new DashboardChartDataset + { + Label = dataset.Key, + Data = dataset.Value + .Select(baseValue => + { + var variation = random.Next(-100, 120) + offset * 30; + return Math.Max(0, baseValue + variation); + }) + .ToList() + }); + } + + yearlyData[year] = payload; + } + + return new DashboardChartViewModel + { + YearlyData = yearlyData, + SelectedYear = yearlyData.Keys.Max() + }; + } } } diff --git a/Models/DashboardChartViewModel.cs b/Models/DashboardChartViewModel.cs new file mode 100644 index 0000000..b14c298 --- /dev/null +++ b/Models/DashboardChartViewModel.cs @@ -0,0 +1,50 @@ +using System.Collections.Generic; +using System.Linq; + +namespace BankSampahApp.Models; + +/// +/// View model yang menampung data grafik dashboard beserta opsi filter tahunan. +/// +public class DashboardChartViewModel +{ + /// + /// Data grafik per tahun (key berupa tahun). + /// + public Dictionary YearlyData { get; set; } = new(); + + /// + /// Tahun yang dipilih saat halaman pertama kali dimuat. + /// + public int SelectedYear { get; set; } + + /// + /// Daftar tahun yang tersedia untuk filter, disortir menurun. + /// + public IEnumerable AvailableYears => YearlyData.Keys.OrderByDescending(x => x); +} + +/// +/// Struktur data payload untuk chart per tahun. +/// +public class DashboardChartPayload +{ + /// + /// Label (umumnya nama bulan) yang ditampilkan pada sumbu X. + /// + public List Labels { get; set; } = new(); + + /// + /// Dataset yang ditampilkan pada grafik. + /// + public List Datasets { get; set; } = new(); +} + +/// +/// Dataset individual untuk Chart.js. +/// +public class DashboardChartDataset +{ + public string Label { get; set; } = string.Empty; + public List Data { get; set; } = new(); +} diff --git a/Views/Main/Dashboard/Index.cshtml b/Views/Main/Dashboard/Index.cshtml index dda4651..28f5f88 100644 --- a/Views/Main/Dashboard/Index.cshtml +++ b/Views/Main/Dashboard/Index.cshtml @@ -1,5 +1,11 @@ -@{ +@model BankSampahApp.Models.DashboardChartViewModel +@using System.Text.Json +@{ ViewData["Title"] = "Dashboard"; + var chartJson = JsonSerializer.Serialize(Model.YearlyData, new JsonSerializerOptions + { + PropertyNamingPolicy = JsonNamingPolicy.CamelCase + }); }
@@ -223,19 +229,40 @@
-
-
-
-
+
+
+
+
- Data Terverifikasi + Grafik Total Sampah per Bulan (BSI, BSU, Offtaker) +

+ Menampilkan total tonase sampah yang dikelola setiap bulan berdasarkan sumber BSI, BSU, dan Offtaker. +

+
+ + +
- +
@@ -243,4 +270,106 @@
- \ No newline at end of file +