ondrej-vrto/php-linechart
Composer 安装命令:
composer require ondrej-vrto/php-linechart
包简介
Generates a simple line chart in SVG format using PHP.
README 文档
README
Generates a simple line chart in SVG format using PHP
Installation
You can install the package via composer:
composer require ondrej-vrto/php-linechart
Usage
$data = [0, 2, 1, 3, 3, 2, 1, 5, 4]; $svg = LineChart::new($data)->make();
Creates the following svg string
<svg viewBox="0 0 200 30" width="200" preserveAspectRatio="none" xmlns="http://www.w3.org/2000/svg"> <defs> <linearGradient x2="0" y1="1" id="color-44dac552-0a1b-4b31-9c35-8bda05443b6f"> <stop stop-color="#fbd808" offset="0"></stop> <stop stop-color="#ff9005" offset="0.34"></stop> <stop stop-color="#f9530b" offset="0.67"></stop> <stop stop-color="#ff0000" offset="1"></stop> </linearGradient> <mask id="linechart-44dac552-0a1b-4b31-9c35-8bda05443b6f"> <polyline stroke="#fff" stroke-width="2" fill="transparent" stroke-linecap="round" stroke-linejoin="round" vector-effect="non-scaling-stroke" transform="scale(24.75 -5.6) translate(0.0404 -5.1786)" points="0 0 1 2 2 1 3 3 4 3 5 2 6 1 7 5 8 4"> </polyline> </mask> </defs> <g> <rect width="200" height="30" fill="url(#color-44dac552-0a1b-4b31-9c35-8bda05443b6f)" mask="url(#linechart-44dac552-0a1b-4b31-9c35-8bda05443b6f)" /> </g> </svg>
The generated svg looks like this.
Input data types
$data = [0, 1, 2, 3]; // integers $data = [0.12, 1.5555, 5.4]; // decimal numbers $data = [true, false, true]; // booleans $data = ["0", "002", "4.05"]; // numbers in string $data = collect([0, 1, 2, 3]); // Illuminate\Support\Collection from Laravel $data = [5]; // one value => prepend zero value $data = []; // empty array => set two zero value $data = [null]; // null => set two zero value
It is possible to use the spread operator and insert values individually.
$svg = LineChart::new(0, 1, 2, 3, 4, 5)->make();
Example data from Laravel Eloquent query.
$collection = WebVisits::query() ->select(['day_visit_count']) ->whereMorphedTo('visitable', $model) ->orderByDesc('date') ->limit(365) ->get() ->pluck('day_visit_count'); $svg = LineChart::new($collection)->make();
Customization
$svg = LineChart::new($data) ->withStrokeWidth(5) ->withOrderReversed() ->withMaxItemAmount(50) ->withLockYAxisRange(200) ->withDimensions(500, 100) ->withColorGradient('Green', 'Orange', 'Red') ->make();
withStrokeWidthwill determine the stroke's widthwithOrderReversedreverses the order of valueswithMaxItemAmountwill determine how many values will be shown. If you originally passed on more values than this max, then the oldest ones will be omitted. If the max amount is set to a number that's higher than the current amount, then the graph will extended.withLockYAxisRangesets the maximum value of the vertical axis. This is useful if you have multiple charts that should have the same length vertical scale. By default, the maximum value is determined based on the input values.withDimensionswill determine the width and height of the rendered SVGwithColorGradientyou can choose any number of colors. A gradient for the graph is automatically generated from them.
Possible color value types for method withColorGradient()
text : Blue, Orange, Cyan, ...
hex : #0000ff, #eee, ...
rgb : rgb(0, 0, 255)
rgba : rgba(0, 0, 255, 1.0)
hsl : hsl(240, 100%, 50%)
hsla : hsla(240, 100%, 50%, 1.0)
cmyk : cmyk(100%,100%,0%,0%)
xyz : xyz(18.05, 7.22, 95.05)
hsb : hsb(241, 100%, 50%)
CIELab : CIELab(32.3, 79.2, -107.86)
Color gradien example:
$svg = LineChart::new($data) ->withColorGradient('rgb(48, 231, 237)', 'rgb(0, 166, 215)', 'rgb(0, 88, 179)', 'rgb(0, 27, 135)') ->make();
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Credits
Alternatives
License
The MIT License (MIT). Please see License File for more information.
ondrej-vrto/php-linechart 适用场景与选型建议
ondrej-vrto/php-linechart 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 282 次下载、GitHub Stars 达 3, 最近一次更新时间为 2023 年 01 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「color」 「svg」 「line chart」 「ondrej vrto」 「line graph」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ondrej-vrto/php-linechart 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ondrej-vrto/php-linechart 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ondrej-vrto/php-linechart 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP class to add Font Awesome 5+'s SVG icons inline without Javascript.
Utility functions to inline SVGs in PHP Projects
Native (browser) color input field for SilverStripe
Generates QR Codes
Colorizes the Flarum forum header and interface based on the active tag's color.
A PHP console tool to generate Markdown documentation from your docblocks and types hints using templates.
统计信息
- 总下载量: 282
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 15
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-01-23


