I try to display bar chart based on data stored in table of database like image below, below image just for ilustration
- Route
Route::get('/chart', 'ChartController@index');
Route::get('/chart', 'ChartController@chart');
- Controller
public function index()
{
return view('chart.index');
}
public function chart()
{
$result = DB::table('agreement')->select('val_agrement')->get();
return response()->json($result);
}
- blade template
@extends('adminlte::page')
@section('content')
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.3/css/bootstrap-select.min.css">
<body>
<div class="row">
<div class="col-md-10 col-md-offset-1">
<div class="panel panel-default">
<div class="panel-heading"><b>CHART</b></div>
<div class="panel-body">
<canvas id="canvas" height="300" width="700"></canvas>
</div>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.3/js/bootstrap-select.min.js" charset="utf-8"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.bundle.js" charset="utf-8"></script>
<script>
var url = "{{url('chart')}}";
var ctx = document.getElementById("canvas").getContext('2d');
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels:VALUEA,
datasets: [{
label: 'VALUEA',
data: valuea,
borderWidth: 1
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero:true
}
}]
}
}
});
</script>
@endsection
how to create chart in laravel, so far, my code as above. thank you
im sorry, its difficult to write blade code here.