Laravel Charts 圖表 使用

becage發表於2018-11-05
composer require consoletvs/charts:6.*

可能報錯

zsh: no matches found: consoletvs/charts:6.*

此時,需要在 ~/.zshrc 新增

alias composer='noglob composer'
composer require consoletvs/charts:6.*
php artisan vendor:publish --tag=charts_config
php artisan make:chart SampleChart

然後在控制器呼叫SampleChart類

use App\Charts\SampleChart;
$chart = new SampleChart;
$chart->labels(['One', 'Two', 'Three', 'Four']);
$chart->dataset('My dataset', 'line', [1, 2, 3, 4]);
$chart->dataset('My dataset 2', 'line', [4, 3, 2, 1]);
return view('sample_view', compact('chart'));

最後在HTML渲染

{!! $chart->container() !!}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.3/Chart.min.js"></script>
{!! $chart->script() !!}
本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章