У меня есть файл js, который генерирует диаграмму chartjs. Я хочу, чтобы диаграмма извлекала данные динамически, используя идентификатор.
Это код в Laravel Blade:
<div class="card">
<canvas id="dash-donut-rice" width="100%" height="100%"></canvas>
<script src="{{url( 'vendor/dash-donut.js' )}}"></script>
</div>
Вот маршрут:
Route::get('/get-donut/{id}', 'DashboardController@getDonut');
Вот контроллер:
public function getDonut($id)
{
//dd($id);
$new = PurchaseOrder::where('status', 'NEW')->where('bu_id', $id)->count();
$completed = PurchaseOrder::where('status', 'COMPLETED')->where('bu_id', $id)->count();
$cancelled = PurchaseOrder::where('status', 'CANCELLED')->where('bu_id', $id)->count();
$total = PurchaseOrder::where('bu_id', $id)->count();
//$center_text = ($completed / $total) * 100 . '%';
if ($total == 0) {
$center_text = '0%';
} else {
$center_text = number_format(($completed / $total) * 100, 0) . '%';
}
$donut_data_array = array(
'count' => [$cancelled, $new, $completed],
'center_text' => $center_text,
);
return $donut_data_array;
}
А вот функция ChartJS:
ajaxGetPostMonthlyData: function () {
//var urlPath = 'http://' + window.location.hostname + '/get-post-chart-data';
//var urlPath = 'http://nipayandtuazon.com/get-rice-donut';
//var urlPath = 'http://nipayandtuazon.com/get-rice-donut';
var request = $.ajax( {
method: 'GET',
url: '/get-donut/1',
} );
https://imgur.com/a/R4oos6V
Заранее спасибо!