MIF_E31211906/SistemPeramalanDBD/app/Controllers/H.php

122 lines
4.1 KiB
PHP

<?php
namespace App\Controllers;
use App\Models\ModelH1;
use App\Models\ModelH2;
use App\Models\ModelDarah;
use App\Models\ModelCurah;
class H extends BaseController
{
protected $DarahModel;
protected $CurahModel;
function __construct()
{
$this->DarahModel = new ModelH1();
$this->CurahModel = new ModelH2();
$this->DarahModel1= new ModelDarah();
$this->CurahModel1 = new ModelCurah();
}
public function index2()
{
$data_curah = $this->CurahModel->getCurah()->getResultArray();
$data_darah = $this->DarahModel->getDarah()->getResultArray();
// Validate that filterint is set and is a number
// Inisialisasi array untuk data chart
$chart_data = [
'labels' => [],
'datasets' => []
];
if (empty($data_darah)) {
$data_darah = [['id' => 0, 'Januari' => 0, 'Februari' => 0, 'Maret' => 0, 'April' => 0, 'Mei' => 0, 'Juni' => 0, 'Juli' => 0, 'Agustus' => 0, 'September' => 0, 'Oktober' => 0, 'November' => 0, 'Desember' => 0]];
}
if (empty($data_curah)) {
$data_curah = [['id' => 0, 'Januari' => 0, 'Februari' => 0, 'Maret' => 0, 'April' => 0, 'Mei' => 0, 'Juni' => 0, 'Juli' => 0, 'Agustus' => 0, 'September' => 0, 'Oktober' => 0, 'November' => 0, 'Desember' => 0]];
}
// Ambil tahun dari data terakhir, dan tiga tahun sebelumnya
$current_year_darah = current($data_darah)['id'];
echo $current_year_darah;
$current_year_curah = current($data_curah)['id'];
$years = [$current_year_darah, $current_year_curah];
$colors = [
'rgba(255, 99, 132, 1)', // Warna untuk data demam berdarah
'rgba(54, 162, 235, 1)' // Warna untuk data curah hujan
];
// Proses data demam berdarah
// Proses data demam berdarah
foreach ($data_darah as $row) {
if (in_array($row['id'], $years)) {
$dataset_key = array_search($row['id'], $years);
foreach ($row as $key => $value) {
if ($key !== 'id' && $key !== 'id') {
// Periksa apakah label sudah ada sebelumnya
if (!in_array($key, $chart_data['labels'])) {
$chart_data['labels'][] = $key; // Tambahkan label ke dalam array jika belum ada
}
if (!isset($chart_data['datasets'][$dataset_key])) {
$chart_data['datasets'][$dataset_key] = [
'label' => "Kasus Demam Berdarah ",
'data' => [],
'borderColor' => $colors[0],
'borderWidth' => 1,
'fill' => false
];
}
$chart_data['datasets'][$dataset_key]['data'][] = $value;
}
}
}
}
// Proses data curah hujan
foreach ($data_curah as $row) {
if (in_array($row['id'], $years)) {
$dataset_key = count($chart_data['datasets']); // Buat key baru untuk curah hujan
foreach ($row as $key => $value) {
if ($key !== 'id' && $key !== 'id') {
// Periksa apakah label sudah ada sebelumnya
if (!in_array($key, $chart_data['labels'])) {
$chart_data['labels'][] = $key; // Tambahkan label ke dalam array jika belum ada
}
if (!isset($chart_data['datasets'][$dataset_key])) {
$chart_data['datasets'][$dataset_key] = [
'label' => "Curah Hujan ",
'data' => [],
'borderColor' => $colors[1],
'borderWidth' => 1,
'fill' => false
];
}
$chart_data['datasets'][$dataset_key]['data'][] = $value;
}
}
}
}
$data['body'] = 'hasil';
$data['chart_data'] = json_encode($chart_data);
$data['tahun1'] = $this->DarahModel1->getDarah3();
$data['tahun2'] = $this->CurahModel1->getCurah3();
return view('template', $data);
}
public function tahun1(){
}
}