@jasen_gottlieb
To pass data in Laravel with Chart.js, you can follow these steps:
1 2 |
$data = [10, 20, 30, 40, 50]; return view('chart', compact('data')); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<canvas id="myChart" width="400" height="400"></canvas> <script> var ctx = document.getElementById('myChart').getContext('2d'); var myChart = new Chart(ctx, { type: 'bar', data: { labels: ['January', 'February', 'March', 'April', 'May'], datasets: [{ label: 'Data', backgroundColor: 'rgba(255, 99, 132, 0.2)', borderColor: 'rgba(255, 99, 132, 1)', borderWidth: 1, data: {!! json_encode($data) !!} }] }, options: { scales: { yAxes: [{ ticks: { beginAtZero: true } }] } } }); </script> |
That's it! You have successfully passed data in Laravel with Chart.js.