Chartjs-Laravel Charts Refresh With Ajax

1๐Ÿ‘

โœ…

So, to use the refresh functionality you need a controller with a method to return the data as json.

So create a new controller method:

public function chartApi() {
    $chart = new SampleChart;
    $chart->dataset('Sample Test', 'bar', [3,4,1]);
    $chart->dataset('Sample Test', 'line', [1,4,3]);
    return $chart->api();
}

And add a route for it:

Route::get('chart-data', ['uses' => 'YourController@chartApi']);

Then, in your controller that is rendering the chart view:

$chart = new SampleChart;

$api = url('/chart-data');

$chart->labels(['test1', 'test2', 'test3'])->load($api)

return view('yourview', ['chart' => $chart]);

Leave a comment