feijs/morris-graphs

为 Laravel 生成 morris 图表

dev-master 2015-09-19 12:28 UTC

This package is auto-updated.

Last update: 2024-09-16 03:06:10 UTC


README

此包提供了一个类,可以轻松生成带有 Morris.js 的动态图表

安装

composer.json 中添加此包并运行 composer update

"require": {
	"feijs/morris-graphs": "dev-master"
}

将 ServiceProvider 添加到 config\app.php 中的 providers

'Feijs\MorrisGraphs\MorrisGraphsServiceProvider'

并发布包资源

php artisan asset:publish "feijs/morris-graphs"

用法

创建图表

创建一个工厂对象并将其传递给视图

use Feijs\MorrisGraphs\Factory as Graph;

public function show()
{
	$graph = new Graph();
	return View::make('views.index')->with('graph', $graph)
}

然后在视图中包含以下内容

{{ $graph->includes() }}	//Preferably once per page

{{ $graph->div() }}			//Where you want to place the graph

{{ $graph->ranges('7', '14', 21) }}		//Add buttons for data ranges

{{ $graph->dynamic($source_url, ['quantity1', 'quantity2']) }}	

//Or

{{ $graph->fixed([$source_data], ['quantity1', 'quantity2']) }}	

对于饼图,第二个参数可以省略

数据

源数据应组织如下

[ 
	['x-key' => 'x-value', 'y1' => 'y1-value', 'y2' => `y2-value`, ...],
	['x-key' => 'x-value', 'y1' => 'y1-value', 'y2' => `y2-value`, ...],
	...
]

除了饼图,应如下组织

[ 
	['label' => 'Label1', 'value' => 'Value1'],
	['label' => 'Label2', 'value' => 'Value2'],
	...
]

设置

要自定义图表设置,您可以调用以下设置器

水平键

必须与数据集中的水平键匹配,默认为 x

$graph->setXKey('segment');

图表类型

BarLineAreaDonut 中选择,默认为 Bar

$graph->setGraphType('Donut');

Id

此 ID 将与 div 中的脚本匹配

$graph->setGraphId('1234');

高度

图表的高度,默认为 250px

$graph->setHeight('300px');

标签翻译

您可以在配置中指定包含图表标签和错误消息翻译的文件

  • 发布包配置文件
php artisan config:publish "feijs/morris-graphs"
  • translations.labelstranslation.messages 中设置翻译文件