Home > Net >  Laravel chart to make report by month and year
Laravel chart to make report by month and year

Time:11-03

i want to make report monthly. so, i have total amount in january 2021 and january 2022. i want to separate it in chart like jan'21 and jan'22. help me please and sorry for my bad English

this my controller

  public function grafik_pemasukan(Request $request)
{
    $total_pemasukan = Pengiriman::select(DB::raw("CAST(SUM(total_biaya_keseluruhan)as int)as total_pemasukan"))
        ->GroupBy(DB::raw("Month(tanggal)"))
        ->pluck('total_pemasukan');


    $bulan = Pengiriman::select(DB::raw("MONTHNAME(tanggal) as bulan"))
        ->GroupBy(DB::raw("MONTHNAME(tanggal)"))
        ->pluck('bulan');
    return view('pengiriman.grafik_pemasukan', compact('total_pemasukan', 'bulan'));
}

and this my javascript for chart

  <script type="text/javascript">
    var total_pemasukan = <?php echo json_encode($total_pemasukan); ?>;
    var bulan = <?php echo json_encode($bulan); ?>;
    Highcharts.chart('grafik', {
        title: {
            text: 'Pemasukan Bulanan CV. ARSA JAYA MANDIRI'
        },
        xAxis: {
            categories: bulan
        },
        yAxis: {
            title: {
                text: 'Total Pendapatan'
            }
        },
        legend: {
            layout: 'vertical',
            align: 'right',
            verticalAlign: 'middle'
        },
        plotOptions: {
            series: {
                allowPointSelect: true
            }
        },
        series: [{
            name: 'Total Pemasukan',
            data: total_pemasukan
        }],
        responsive: {
            rules: [{
                condition: {
                    maxWidth: 500
                },
                chartOptions: {
                    legend: {
                        layout: 'horizontal',
                        align: 'center',
                        verticalAlign: 'bottom'
                    }
                }
            }]
        }
    });
</script>

this is my graphic enter image description here

CodePudding user response:

There is a great package by Laravel Daily for this that you can use. Readme is very self explanatory:

https://github.com/LaravelDaily/laravel-charts

You would want something like this:

$chart_options = [
    'chart_title' => 'Pemasukan Bulanan CV. ARSA JAYA MANDIRI',
    'report_type' => 'group_by_date',
    'model' => 'App\Models\User', // Check this line and use appropriate model
    'group_by_field' => 'created_at',
    'group_by_period' => 'month',
    'chart_type' => 'line',
];
$chart1 = new LaravelChart($chart_options);

You can make and render as many charts as needed using this same setup.

  • Related