mikuspetr / charts-php
使用 Chart.js 构建 PHP 图表的库
v1.0.5
2022-06-06 11:36 UTC
Requires
- php: >=7.1.0
README
ChartsPhp 是一个库,它建立了 JavaScript 库 Chart.js 和 PHP 之间的接口。您可以直接从 PHP 绘制图表。
先决条件
- Chart.js - 使用 NPM、CDN 等安装 Chart.js,安装说明
- Composer - 您可以从 getcomposer.org 下载。
使用 composer 安装 ChartsPhp
composer require mikuspetr/charts-php
示例
use ChartsPhp\ChartsPhp; $chartType = 'bar'; // The arrays structure copies data object structure in Chart.js $labels = ['January', 'February', 'March', 'April', 'May', 'June',]; $datasets = [ [ 'label' => 'apples', 'data' => [10, 12, 8, 25, 32, 20] ], [ 'label' => 'bananas', 'data' => [11, 4, 15, 17, 10, 23] ] ]; $options = ['aspectRatio' => 3]; // use constructor to create new chart $barChart = ChartsPhp::createChart($chartType, $labels, $datasets, $options); // render HTML canvas echo $barChart->renderHtml(); // render JavaScript that draw chart in canvas (require Chart.js) echo $barChart->renderScript(); // use add methods to create new chart $lineChart = ChartsPhp::createChart('line') ->setLabels($labels) ->addDatasets($datasets) ->setOptions(['aspectRatio' => 4, 'cubicInterpolationMode' => 'monotone']); // render both, HTML and JavaScript echo $lineChart;