MIF_E31212366/app/Charts/YearlyDBDChart.php

44 lines
1.1 KiB
PHP

<?php
namespace App\Charts;
use ArielMejiaDev\LarapexCharts\LarapexChart;
use App\Models\DataDBD;
use Illuminate\Support\Facades\DB;
class YearlyDBDChart
{
protected $chart;
// protected $dataByYearAll;
public function __construct()
{
$this->chart = new LarapexChart;
// $this->dataByYearAll = $dataByYearAll;
}
public function build(): \ArielMejiaDev\LarapexCharts\PieChart
{
$data = DataDBD::select('tahun', DB::raw('SUM(jumlah_kasus) as total_kasus'))
->whereIn('tahun', [2019, 2020, 2021, 2022, 2023])
->groupBy('tahun')
->pluck('total_kasus', 'tahun')->toArray();
// Buat array kosong untuk menyimpan label yang dibuat
$labels = [];
// Loop melalui data dan gabungkan tahun dan total kasus ke dalam label
foreach ($data as $year => $totalCases) {
// Contoh: Gabungkan tahun dan total kasus ke dalam label
$labels[] = 'Tahun ' . $year . ' - ' . $totalCases;
}
// Return chart dengan data dan label yang telah dibuat
return $this->chart->pieChart()
->addData(array_keys($data))
->setLabels($labels)
->setHeight(350);
}
}