Reputation: 65
I want to pass data into Chart.js, however nothing appear on the card. How to solve this problem adn what is the correct way to pass data into Chart.js? I cant directly put the data into the script in this way.
Controller
public function ViewAnalytic(){
$id = Auth::user()->id;
$electrical = DB::table('maintenances')->where('landlord_id', $id)->where('category', 'Electrical System')->count();
$pest = DB::table('maintenances')->where('landlord_id', $id)->where('category', 'Plumbing System')->count();
$plumbing = DB::table('maintenances')->where('landlord_id', $id)->where('category', 'Pest Infectations')->count();
$structural = DB::table('maintenances')->where('landlord_id', $id)->where('category', 'Structural Repairs')->count();
$appliances = DB::table('maintenances')->where('landlord_id', $id)->where('category', 'Electrical Appliances')->count();
$others = DB::table('maintenances')->where('landlord_id', $id)->where('category', 'Others')->count();
return view('analytic.analytic-page', compact('total_num', 'vacant', 'rented', 'user', 'paid', 'unpaid',
'electrical', 'pest', 'plumbing', 'structural', 'appliances', 'others'));
}
view.blade.php
<div class="card-body px-3 py-4-5">
<canvas id="myChart" width="200" height="200"></canvas>
</div>
Script
<script>
const data = {
labels: [
'Electrical System',
'Plumbing System',
'Pest Infections',
'Structural Repairs',
'Electrical Appliances',
'Others'
],
datasets: [{
label: 'My First Dataset',
data: [{!!$electrical!!}, {!!$plumbing!!}g, {!!$pest!!}, {!!$structural!!}, {!!$appliances!!}, {!!$others!!}],
backgroundColor: [
'rgb(255, 99, 132)',
'rgb(204, 102, 235)',
'rgb(255, 205, 86)',
'rgb(0, 204, 102)',
'rgb(102, 153, 255)',
'rgb(191, 0, 205)'
],
hoverOffset: 4
}]
};
const ctx = document.getElementById('myChart').getContext('2d');
const myChart = new Chart(ctx, {
type: 'pie',
data: data,
});
</script>
Upvotes: 0
Views: 453
Reputation: 41
{!!$plumbing!!}g
I think that you can try removing this "g" in your data. At first look everything is correct.
Upvotes: 0
Reputation: 280
Make an ajax request in the view file, then push the values in the chart. And you can try this
{!! json_encode($electrical) !!}
Have a look at this tutorial
https://www.codeleaks.io/ajax-get-post-request-in-laravel/
Upvotes: 1