Laravel图表扩展包推荐:Charts
2016年11月15日 · 2283次 · 4条 · laravel,package,charts

介绍
在项目开发中,创建图表通常是一件痛苦的事情。因为你必须将数据转换为图表库支持的格式传输到模板中,并且每个图表的插件库是不同的。如果需要替换图表插件的时候,我们就得重新处理数据结构。
为了解决这一问题,一位名叫Erik
Campobadal的开发人员创建了一个新的Laravel图表包来创建交互式图表。它支持十多个最流行的图表库,从标准线图和条形图到实时图表库。都可以让我们在项目中随意使用,轻松切换。
安装
首先在命令终端里定位到项目的根目录,通过运行composer命令进行下载安装:
composer require consoletvs/charts
下载完成后,在目录config/app.php中添加该扩展包的服务提供者:
'providers' => [
ConsoleTVs\Charts\ChartsServiceProvider::class,
],
以及服务的别名:
'alias' => [
'Charts' => ConsoleTVs\Charts\Charts::class,
],
最后通过artisan发布该扩展包的资源,包括配置文件,模板标签等:
php artisan vendor:publish --tag=charts_config
php artisan vendor:publish --tag=charts_assets --force
命令执行后,在配置目录config/charts.php
中包含一个设置数组,你可以在里面找到扩展包的默认设置。
用法示例
控制器代码示例:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use Charts;
class TestController extends Controller
{
public function index()
{
$chart = Charts::create('line', 'highcharts')
->setView('custom.line.chart.view') // Use this if you want to use your own template
->setTitle('My nice chart')
->setLabels(['First', 'Second', 'Third'])
->setValues([5,10,20])
->setDimensions(1000,500)
->setResponsive(false);
return view('test', ['chart' => $chart]);
}
}
视图代码示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>My Charts</title>
{!! Charts::assets() !!}
</head>
<body>
<center>
{!! $chart->render() !!}
</center>
</body>
</html>
单数据图表
使用Charts::create()
方法创建图表,该方法接收两个参数。第一个参数是图表类型,第二个是使用的图表库。例如:
Charts::create('line', 'highcharts')
->setTitle('My nice chart')
->setLabels(['First', 'Second', 'Third'])
->setValues([5,10,20])
->setDimensions(1000,500)
->setResponsive(false);
附各图表插件库对单数据图表的支持情况:
单图表 | 线形图 | 区域图 | 柱状图 | 饼状图 | 环状图 | 地理图 | 测量图 | 温度表 | 百分比 | 进度条 |
---|---|---|---|---|---|---|---|---|---|---|
chartjs | x | x | x | x | x | - | - | - | - | - |
highcharts | x | x | x | x | x | x | - | - | - | - |
x | x | x | x | x | x | x | - | - | - | |
material | x | - | x | - | - | - | - | - | - | - |
chartist | x | x | x | x | x | - | - | - | - | - |
fusioncharts | x | x | x | x | x | - | - | - | - | - |
morris | x | x | x | - | x | - | - | - | - | - |
plottablejs | x | x | x | x | x | - | - | - | - | - |
minimalist | x | x | x | x | x | - | - | - | - | - |
canvas-gauges | - | - | - | - | - | - | x | x | - | - |
justgage | - | - | - | - | - | - | x | - | x | - |
progressbarjs | - | - | - | - | - | - | - | - | x | x |
多数据图表
要创建多数据集图表使用Charts::multi()
方法,参数和单数据图表相同。在增加数据时,需要使用setDataset()
方法添加数据。例如:
Charts::multi('line', 'highcharts')
->setColors(['#ff0000', '#00ff00', '#0000ff'])
->setLabels(['One', 'Two', 'Three'])
->setDataset('Test 1', [1,2,3])
->setDataset('Test 2', [0,6,0])
->setDataset('Test 3', [3,4,1]);
setDataset()
方法接收两个参数。第一个为字符串,代表元素标签;第二个参数为数组,代表数值:
Charts::multi('bar', 'minimalist')
->setResponsive(false)
->setDimensions(0, 500)
->setColors(['#ff0000', '#00ff00', '#0000ff'])
->setLabels(['One', 'Two', 'Three'])
->setDataset('Test 1', [1,2,3])
->setDataset('Test 2', [0,6,0])
->setDataset('Test 3', [3,4,1]);
附各图表插件库对多数据图表的支持情况:
多图表 | 线形图 | 区域图 | 柱状图 | 饼状图 | 环状图 | 地理图 | 测量图 | 温度表 | 百分比 | 进度条 |
---|---|---|---|---|---|---|---|---|---|---|
chartjs | x | x | x | - | - | - | - | - | - | - |
highcharts | x | x | x | - | - | - | - | - | - | - |
x | x | x | - | - | - | - | - | - | - | |
material | x | - | x | - | - | - | - | - | - | - |
chartist | x | x | x | - | - | - | - | - | - | - |
fusioncharts | x | x | x | - | - | - | - | - | - | - |
morris | x | x | x | - | - | - | - | - | - | - |
plottablejs | x | x | x | - | - | - | - | - | - | - |
minimalist | x | x | x | - | - | - | - | - | - | - |
canvas-gauges | - | - | - | - | - | - | - | - | - | - |
justgage | - | - | - | - | - | - | - | - | - | - |
progressbarjs | - | - | - | - | - | - | - | - | - | - |
图表示例
饼状图,highcharts不一定能更改此图表的颜色:
Charts::create('pie', 'highcharts')
->setTitle('My nice chart')
->setLabels(['First', 'Second', 'Third'])
->setValues([5,10,20])
->setDimensions(1000,500)
->setResponsive(false);
环状图,highcharts和chartist不一定能更改此图表的颜色:
Charts::create('donut', 'highcharts')
->setTitle('My nice chart')
->setLabels(['First', 'Second', 'Third'])
->setValues([5,10,20])
->setDimensions(1000,500)
->setResponsive(false);
线形图:
Charts::create('line', 'highcharts')
->setTitle('My nice chart')
->setElementLabel('My nice label')
->setLabels(['First', 'Second', 'Third'])
->setValues([5,10,20])
->setDimensions(1000,500)
->setResponsive(false);
区域图:
Charts::create('area', 'highcharts')
->setTitle('My nice chart')
->setElementLabel('My nice label')
->setLabels(['First', 'Second', 'Third'])
->setValues([5,10,20])
->setDimensions(1000,500)
->setResponsive(false);
柱状图,highcharts不一定能更改此图表的颜色:
Charts::create('bar', 'highcharts')
->setTitle('My nice chart')
->setElementLabel('My nice label')
->setLabels(['First', 'Second', 'Third'])
->setValues([5,10,20])
->setDimensions(1000,500)
->setResponsive(false);
地理位置,标签必须具有国家/地区代码,而不是名称。如果要向图表添加颜色,需要提供至少2种颜色的数组。
第一个是最小值,第二个是最大值:
Charts::create('geo', 'highcharts')
->setTitle('My nice chart')
->setElementLabel('My nice label')
->setLabels(['ES', 'FR', 'RU'])
->setColors(['#C5CAE9', '#283593'])
->setValues([5,10,20])
->setDimensions(1000,500)
->setResponsive(false);
扩展图表库
你可以在存储库中通过创建分支创建自己的图表。src/Templates
文件夹包含所有当前图表,但你可以添加如下:
- 创建一个新文件,语法是:
library.type.php
- 如果你的图表库被调用,mylib和模板属于线形图的扩展,你应该创建一个这样的文件:
mylib.line.php
- 调用使用:
$chart =
你需要将
Charts::create('line','mylib');CSS/JS
添加到/ src
文件夹中的includes.php
文件。
想获取更多有关Charts扩展包的内容,请访问github地址:https://github.com/ConsoleTVs/Charts
Laravel图表扩展包推荐:Charts的更多相关文章
- laravel composer 扩展包开发(超详细)
laravel composer 扩展包开发(超详细) 置顶 2018年02月05日 11:09:16 Simael__Aex 阅读数:10396 版权声明:转载请注明出处:http://blo ...
- 【转】下载量最高的 100 个 Laravel 扩展包推荐
说明 Laravel 另一个令人喜欢的地方,是拥有活跃的开发者社区,而活跃的开发者社区带来的,是繁华的扩展包生态. 本文对 Packagist 上打了 Laravel 标签 的扩展包进行整理,截止到现 ...
- Laravel 调试利器 —— Laravel Debugbar 扩展包安装及使用教程
1.简介 Laravel Debugbar 在 Laravel 5 中集成了 PHP Debug Bar ,用于显示调试及错误信息以方便开发.该扩展包包含了一个 ServiceProvider 用于注 ...
- Laravel / PHP 扩展包视频教程
https://laravel-china.org/courses/laravel-package 每周精选两个以上扩展包进行讲解,涵盖 PHP 和 Laravel 相关的最新.最热.最常用的扩展包. ...
- laravel本地化扩展包的下载使用
1.下载扩展包 composer require caouecs/laravel-lang:~3.0 2.下载完成之后在根目录下的vendor中caouces\src下就是语言的扩展包 2.1我们复制 ...
- laravel 实用扩展包
1.beyondcode / laravel-self-diagnosis 环境检测.检测 php 版本.扩展 是否正常,数据库连接是否正常等 2.nunomaduro/larastan larave ...
- laravel后台扩展包
https://github.com/the-control-group/voyager
- 手把手安装Laravel框架(permissions扩展包)实现RBAC权限---以及一些安装时的ERROR
a.依赖管理工具,框架,环境 1.composer 2.laravel(我的是5.5) 3.PHP(我的7.2),MySql(我的5.7) b,安装 1.首先需要安装一个干净的 Laravel 项目, ...
- 基于Composer的Laravel扩展包开发工作流
使用场景 在引用第三方包的时候,对第三方包有改动需求,需要将代码放在自己的仓库:并且自己的其他项目也有需求引用自定义的第三方包:甚至自己会发布修改后的第三方包: 读完本文你讲获得: Git Submo ...
随机推荐
- java在注解中绑定方法参数的解决方案
我们有这样子的需求,需要记录用户操作某个方法的信息并记录到日志里面,例如,用户在保存和更新任务的时候,我们需要记录下用户的ip,具体是保存还是更新,调用的是哪个方法,保存和更新的任务名称以及操作是否成 ...
- maven快速入门之安装
maven是基于项目对象模型(pom) 可以通过y一小段描述信息来管理项目的构建,报告,和文档的软件项目管理工具. 覆盖编译,测试运行清理构建周期,提供仓库的概念,统一帮助管理项目. 下载:http: ...
- 配置Ubuntu虚拟环境
1.ubuntu默认root用户没有激活,激活root用户,就要为root用户创建密码 $sudo passwd root 2.修改主机名 $vi /etc/hostname 3.安装ssh服 ...
- 3.纯 CSS 创作一个容器厚条纹边框特效
原文地址:3.纯 CSS 创作一个容器厚条纹边框特效 没有啥好点子呀,不爽 HTML代码: <div class="box"> <div class=" ...
- apache中 MaxClients 与MaxRequestsPerChild
据现象来对APACHE调优,以前用MAXCLIENTS 3000,砖家建议后,改为1500,今天查资料如下: http://www.linuxqq.net/ MaxClients 要加到多少?连接数理 ...
- eclipse中添加aptana插件(html.css.js自动提示)
一.关于aptana aptana是一款很不错的插件,本人主要用于安装此类插件,在eclipse中用于编辑javascript代码,html代码,和css代码的,因为其有自动纠错功能,当然安装后的问题 ...
- 1.Redis的应用场景
转自:http://www.runoob.com/redis/redis-tutorial.html Redis开创了一种新的数据存储思路,使用Redis,我们不用在面对功能单调的数据库时,把精力放在 ...
- 解决org.springframework.context.NoSuchMessageException: No message found under code 'login.validate.er
转自:https://blog.csdn.net/steveguoshao/article/details/36184971 在项目中遇到 org.springframework.context.No ...
- 异常信息ASM ClassReader failed to parse class file的问题解决
jdk8与spring 3不兼容问题:用jdk7.或者升级到spring4 详情:异常信息ASM ClassReader failed to parse class file的问题解决
- spring coud feign
1. 依赖 <parent> <groupId>org.springframework.boot</groupId> <artifactId>sprin ...