jakubkratina/larachartie
Composer 安装命令:
composer require jakubkratina/larachartie
包简介
Lightweight PHP wrapper for Google Chart javascript library for Laravel
README 文档
README
Lightweight PHP wrapper for Google Chart javascript library for Laravel framework
Installation
Install via composer:
composer require jakubkratina/larachartie
Register service provider:
\JK\LaraChartie\ChartieServiceProvider::class
Register LaraChartie's facade:
'Chartie' => JK\LaraChartie\Facades\Chart::class
Usage
You can create a folder for you definitions, for example app\Charts\DataTable.
In this folder you can put your files, which have to implements JK\LaraChartie\Contracts\Source contract.
<?php namespace App\Charts\DataTable; use Carbon\Carbon; use JK\LaraChartie\Contracts\DataTable; use JK\LaraChartie\Contracts\Source; use JK\LaraChartie\DataTable\Type; class UsersSource implements JK\LaraChartie\Contracts\Source { /** * @param DataTable $dataTable */ public function columns(DataTable $dataTable) { $dataTable ->addColumn(Type::DATE, 'Created At') ->addStringColumn('Name') ->addStringColumn('Country'); } /** * @param DataTable $dataTable */ public function fill(DataTable $dataTable) { foreach (User::all() as $user) { $dataTable->addRow( $user->created_at, $user->firstname, [ 'value' => $user->country, 'format' => 'User is from ' . $user->country ] ); } } }
Then you can just have to call following in the your controller class:
use use JK\LaraChartie\Facades\Chart; use use App\Charts\DataTable\UsersStorage; class UsersController extends Controller { /** * @return array */ public function progress() { return Chart::dataTable() ->source(UsersStorage::class) ->toArray(); } }
And your output will be like this:
{
"cols": [
{
"label": "Date",
"type": "date"
},
{
"label": "Name",
"type": "string"
},
{
"label": "Country",
"type": "string"
}
],
"rows": [
{
"c": [
{
"v": "Date(2016, 8, 12, 18, 24, 21)",
"f": null
},
{
"v": "John",
"f": null
},
{
"v": "ZA",
"f": "User is from ZA"
}
]
},
{
"c": [
{
"v": "Date(2016, 8, 15, 10, 0, 53)",
"f": null
},
{
"v": "Tomas",
"f": null
},
{
"v": "CA",
"f": "User is from CA"
}
]
}
]
}
And at the end of the day, you just have to pass the data into the Google chart via an ajax request:
const users = $.ajax({ url: '/api/users', dataType: 'json', async: false }).responseText; const data = new google.visualization.DataTable(users);
Formatters
As a default formatter is used LineChartFormatter, which provides the previous response.
If you want to format the result, use one of the followings formatters:
PieChartFormatter
return Chart::dataTable() ->addStringColumn('Tasks') ->addNumberColumn('Hours per Day') ->formatter(PieChartFormatter) ->addRows([ ['Work', 11], ['Eat', 2], ['Commute', 2], ['Watch TV', 2], ['Sleep', 7] ]) ->toArray();
Response
[
["Tasks", "Hours per Day"],
["Work", 11],
["Eat", 2],
["Commute", 2],
["Watch TV", 2],
["Sleep", 7]
]
DataTable's methods
interface DataTable { /** * @param string $type * @param string $label * @return $this */ public function addColumn($type, $label); /** * @param string $label * @return $this */ public function addStringColumn($label); /** * @param string $label * @return $this */ public function addNumberColumn($label); /** * @param string $label * @return $this */ public function addBooleanColumn($label); /** * @param string $label * @return $this */ public function addDateColumn($label); /** * @param string $label * @return $this */ public function addDateTimeColumn($label); /** * @param array ...$values * @return $this */ public function addRow(... $values); /** * @return Collection|Column[] */ public function columns(); /** * @return Collection|Column[] */ public function rows(); /** * @return array */ public function toArray(); /** * @param string $dataTable * @return DataTable */ public function source($dataTable); }
jakubkratina/larachartie 适用场景与选型建议
jakubkratina/larachartie 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 172 次下载、GitHub Stars 达 1, 最近一次更新时间为 2016 年 10 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「OOP」 「wrapper」 「Google Charts」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jakubkratina/larachartie 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jakubkratina/larachartie 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jakubkratina/larachartie 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
LinkedIn API PHP SDK with OAuth 2.0 & CSRF support. Can be used for social sign in or sharing on LinkedIn. Examples. Documentation.
LinkedIn API PHP SDK with OAuth 2.0 & CSRF support. Can be used for social sign in or sharing on LinkedIn. Examples. Documentation.
A php library to bootstrap the use of Domain Driven Design valueObjects
Super simple HTTP client
gv config package
A PHP (read-only) wrapper for the Git command line utility
统计信息
- 总下载量: 172
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 11
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-10-21