定制 bbsnly/chartjs-php 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

bbsnly/chartjs-php

最新稳定版本:v4.0.1

Composer 安装命令:

composer require bbsnly/chartjs-php

包简介

PHP wrapper for Chart.js library

关键字:

README 文档

README

Contributor Covenant Tests codecov Total Downloads Latest Stable Version License

This package transforms how you create ChartJS elements by bringing them directly into PHP.

ChartJS-PHP eliminates the complexity of JavaScript when working with ChartJS charts. While ChartJS traditionally requires JavaScript implementation, our PHP solution delivers the same powerful charts through clean, efficient PHP code. By generating ChartJS elements directly in PHP, you write less code, maintain cleaner codebases, and deliver faster results. This is the definitive solution for PHP developers building data visualizations, dashboards, or any application requiring dynamic charts.

Note: Include the ChartJS library in your project as specified in their official documentation.

Installation

Installing ChartJS-PHP is straightforward with Composer. Run this command in your project directory:

composer require bbsnly/chartjs-php

Minimum Requirements:

  • PHP version: 8.1 or higher
  • ChartJS version: 2.0 or higher

Usage

Creating charts with ChartJS-PHP is simple and intuitive. Start by instantiating the Chart class, define your data, and render your chart. The library handles all the complexity for you.

Choose from our specialized chart classes for even faster development: BarChart, BubbleChart, DoughnutChart, LineChart, PieChart, PolarAreaChart, RadarChart, and ScatterChart.

Here's how to create a line chart:

use Bbsnly\ChartJs\Chart;
use Bbsnly\ChartJs\Config\Data;
use Bbsnly\ChartJs\Config\Dataset;
use Bbsnly\ChartJs\Config\Options;

$chart = new Chart;
$chart->type = 'line';

$data = new Data();
$data->labels = ['Red', 'Green', 'Blue'];

$dataset = new Dataset();
$dataset->data = [5, 10, 20];
$data->datasets[] = $dataset;

$chart->data($data);

$options = new Options();
$options->responsive = true;
$chart->options($options);

$chart->get(); // Returns the array of chart data
$chart->toJson(); // Returns the JSON representation of the chart data
$chart->toHtml('my_chart'); // Returns the HTML and JavaScript code for the chart

In the example below we will use the toHtml method to generate the HTML and JavaScript code for the chart.

<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>

<div>
    <?= $chart->toHtml('my_chart'); ?>
</div>

In the example below we will use the toJson method to generate the JSON representation of the chart data.

<div>
  <canvas id="myChart"></canvas>
</div>

<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>

<script>
  const ctx = document.getElementById('myChart');

  new Chart(ctx, <?= $chart->toJson(); ?>);
</script>

Tests

Run the test suite with:

composer test

Contributing

Read our Contributing guidelines and start improving ChartJS-PHP today.

License

The ChartJS PHP is open-sourced software licensed under the MIT license.

统计信息

  • 总下载量: 85.87k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 27
  • 点击次数: 1
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 27
  • Watchers: 3
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-07-17