I want to crate a graph using chart.js and show it in my view. But after parsing the data to the view, it always come with undefined variable (in my case, bulan and pendapatan is undefined).
This is my controller to parse the data
$query = DB::table("transaksipenjualan")->select(DB::raw('EXTRACT(MONTH FROM tanggaltransaksi) AS Bulan, SUM(total) as Pendapatan'))
->where('tanggalTransaksi', 'LIKE', '%'.$request->tahun.'%')
->groupBy(DB::raw('EXTRACT(MONTH FROM tanggaltransaksi)'))
->get();
$count=count($query);
$label = [];
$data = [];
for($i=0;$i<$count;$i++)
{
$label[$i] = $query[$i]->Bulan;
$data[$i] = $query[$i]->Pendapatan;
}
return view('printPreview/pendapatanBulanan', ['data'=>$query, 'bulan'=>$label, 'pendapatan'=>$data]);
And this is my script to get the data
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
}
}]
}
}
});
I don't know very well how to pass the data to a script, so I need some advice. Thanks!