noonenew / laravelchartjs
一个简单的包,用于简化并自动化在 Laravel 5.x 中使用 Chartjs v2 库的图表
2.5.1
2018-09-11 21:37 UTC
Requires
- php: >=5.6.4
- illuminate/support: ~5.1
This package is auto-updated.
Last update: 2024-09-18 18:48:54 UTC
README
一个简单的包,用于简化并自动化在 Laravel 5.x 中使用 Nick Downie 的 Chart.js v2 库的图表
设置
composer require noonenew/laravelchartjs
并将服务提供者在你的文件 config/app.php 中添加
Noonenew\LaravelChartJs\Providers\ChartjsServiceProvider::class
最后,目前,你必须安装并将 Chartjs 库添加到你的布局 / 模板中,该库可以轻松在 https://chart.js.cn 下载。此设置也将得到改进。
用法
您可以请求服务容器中负责构建图表的服务,并通过流畅的接口传递图表设置。
$service = app()->chartjs ->name() ->type() ->size() ->labels() ->datasets() ->options();
目前构建器需要图表的名称、图表的类型,可以是 chartjs 支持的任何类型,以及其他自定义配置,如标签、数据集、大小和选项。
在数据集接口中,您可以传递任何配置和选项到您的图表。所有在 chartjs 文档中可用的选项都受支持。只需用 PHP 数组表示法编写配置即可!
高级 chartjs 选项
由于当前版本允许添加基于简单 JSON 字符串的选项,因此无法生成如下选项
options: { scales: { xAxes: [{ type: 'time', time: { displayFormats: { quarter: 'MMM YYYY' } } }] } }
使用 optionsRaw(string) 方法可以以原始格式添加选项
传递字符串格式,如 JSON
$chart->optionsRaw("{ legend: { display:false }, scales: { xAxes: [{ gridLines: { display:false } }] } }");
或者,如果您愿意,可以传递 PHP 数组格式
$chart->optionsRaw([ 'legend' => [ 'display' => true, 'labels' => [ 'fontColor' => '#000' ] ], 'scales' => [ 'xAxes' => [ [ 'stacked' => true, 'gridLines' => [ 'display' => true ] ] ] ] ]);
示例
1 - 线形图 / 雷达图
// ExampleController.php $chartjs = app()->chartjs ->name('lineChartTest') ->type('line') ->size(['width' => 400, 'height' => 200]) ->labels(['January', 'February', 'March', 'April', 'May', 'June', 'July']) ->datasets([ [ "label" => "My First dataset", 'backgroundColor' => "rgba(38, 185, 154, 0.31)", 'borderColor' => "rgba(38, 185, 154, 0.7)", "pointBorderColor" => "rgba(38, 185, 154, 0.7)", "pointBackgroundColor" => "rgba(38, 185, 154, 0.7)", "pointHoverBackgroundColor" => "#fff", "pointHoverBorderColor" => "rgba(220,220,220,1)", 'data' => [65, 59, 80, 81, 56, 55, 40], ], [ "label" => "My Second dataset", 'backgroundColor' => "rgba(38, 185, 154, 0.31)", 'borderColor' => "rgba(38, 185, 154, 0.7)", "pointBorderColor" => "rgba(38, 185, 154, 0.7)", "pointBackgroundColor" => "rgba(38, 185, 154, 0.7)", "pointHoverBackgroundColor" => "#fff", "pointHoverBorderColor" => "rgba(220,220,220,1)", 'data' => [12, 33, 44, 44, 55, 23, 40], ] ]) ->options([]); return view('example', compact('chartjs')); // example.blade.php <div style="width:75%;"> {!! $chartjs->render() !!} </div>
2 - 条形图
// ExampleController.php $chartjs = app()->chartjs ->name('barChartTest') ->type('bar') ->size(['width' => 400, 'height' => 200]) ->labels(['Label x', 'Label y']) ->datasets([ [ "label" => "My First dataset", 'backgroundColor' => ['rgba(255, 99, 132, 0.2)', 'rgba(54, 162, 235, 0.2)'], 'data' => [69, 59] ], [ "label" => "My First dataset", 'backgroundColor' => ['rgba(255, 99, 132, 0.3)', 'rgba(54, 162, 235, 0.3)'], 'data' => [65, 12] ] ]) ->options([]); return view('example', compact('chartjs')); // example.blade.php <div style="width:75%;"> {!! $chartjs->render() !!} </div>
3 - 饼图 / 环形图
// ExampleController.php $chartjs = app()->chartjs ->name('pieChartTest') ->type('pie') ->size(['width' => 400, 'height' => 200]) ->labels(['Label x', 'Label y']) ->datasets([ [ 'backgroundColor' => ['#FF6384', '#36A2EB'], 'hoverBackgroundColor' => ['#FF6384', '#36A2EB'], 'data' => [69, 59] ] ]) ->options([]); return view('example', compact('chartjs')); // example.blade.php <div style="width:75%;"> {!! $chartjs->render() !!} </div>
注意
此 README 以及该包正在开发中,但将不断更新,一旦准备好生产,我会立即通知您。感谢您的理解。
如有任何问题或建议,请最好开启 issue!
许可协议
LaravelChartJs 是开源软件,许可协议为 MIT 许可证。