1

I try to create a graph using Chart.js and get the data from database, but I am confused how to pass the data to an array variable in view.

This is the Controller

public function ApiLaporanBulanan()
{
    $data = DB::table("transaksipenjualan")->select(DB::raw('EXTRACT(MONTH FROM tanggaltransaksi) AS Bulan, SUM(total) as Pendapatan'))
    ->groupBy(DB::raw('EXTRACT(MONTH FROM tanggaltransaksi)'))
    ->get();
    return response()->json($data);

    //Accessing Data
    dd($data[0]->Bulan);
}

This is the script in view

<script>
  var url = "{{url('laporan/pendapatanAPI')}}";
  var Bulan = [];
  var Pendapatan = [];
  $(document).ready(function(){
    $.get(url, function(response){
      response.forEach(function(data){
          Bulan.push(data->Bulan);
          Pendapatan.push(data->Pendapatan);
      });
      var ctx = document.getElementById("canvas").getContext('2d');
          var myChart = new Chart(ctx, {
            type: 'bar',
            data: {
                labels: Bulan,
                datasets: [{
                    label: 'Nilai Pendapatan',
                    data: Pendapatan,
                    borderWidth: 1
                }]
            },
            options: {
                scales: {
                    yAxes: [{
                        ticks: {
                            beginAtZero:true
                        }
                    }]
                }
            }
        });
    });
  });
</script>
6
  • can i know your blade page name?? Commented May 7, 2019 at 6:15
  • I worked with chat,js in blades by formating data in the controller and then passing it to javsacript array itself. Worked perfectly. Commented May 7, 2019 at 6:17
  • Route::get('laporan/pendapatanAPI', 'ReportController@ApiLaporanBulanan'); Commented May 7, 2019 at 6:24
  • if you are familiar with using packages github.com/ConsoleTVs/Charts Commented May 7, 2019 at 6:51
  • i mean your view blade file where you want to show blade the chart js Commented May 7, 2019 at 7:58

1 Answer 1

2

You can create two array in your controller.

$label = [];
$dataset = [];

and loop through your collection and push data to these arrays

foreach($data as $value){
   $label[] = $value->data_field;
   $dataset[] = $value->data_field;
}

And pass it to your blade and assign these array to your chart

...
var myChart = new Chart(ctx, {
            type: 'bar',
            data: {
                labels: Bulan,
                datasets: [{
                    label: 'Nilai Pendapatan',// label data
                    data: Pendapatan, // dataset
                    borderWidth: 1
                }]
            },
....
Sign up to request clarification or add additional context in comments.

1 Comment

Is this the right way to pass the data? return response()->json(array('Bulan'=>$label, 'Pendapatan'=>$data));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.