Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bbsnly/chartjs-php
PHP wrapper for Chart.js library
https://github.com/bbsnly/chartjs-php
analytics chart-generation chart-library chartjs charts data-visualization graphs php php-library php-wrapper statistics
Last synced: about 18 hours ago
JSON representation
PHP wrapper for Chart.js library
- Host: GitHub
- URL: https://github.com/bbsnly/chartjs-php
- Owner: bbsnly
- License: mit
- Created: 2017-07-16T16:46:51.000Z (over 7 years ago)
- Default Branch: main
- Last Pushed: 2025-01-28T11:09:10.000Z (13 days ago)
- Last Synced: 2025-02-08T14:24:10.783Z (1 day ago)
- Topics: analytics, chart-generation, chart-library, chartjs, charts, data-visualization, graphs, php, php-library, php-wrapper, statistics
- Language: PHP
- Homepage: https://github.com/bbsnly/chartjs-php
- Size: 182 KB
- Stars: 24
- Watchers: 4
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# ChartJS-PHP
[![Contributor Covenant](https://img.shields.io/badge/Contributor%20Covenant-2.1-4baaaa.svg)](CODE_OF_CONDUCT.md)
[![Tests](https://github.com/bbsnly/chartjs-php/actions/workflows/php.yml/badge.svg)](https://github.com/bbsnly/chartjs-php/actions)
[![codecov](https://codecov.io/gh/bbsnly/chartjs-php/graph/badge.svg?token=MTIBNP8BDT)](https://codecov.io/gh/bbsnly/chartjs-php)
[![Total Downloads](https://poser.pugx.org/bbsnly/chartjs-php/d/total.svg)](https://packagist.org/packages/bbsnly/chartjs-php)
[![Latest Stable Version](https://poser.pugx.org/bbsnly/chartjs-php/v/stable.svg)](https://packagist.org/packages/bbsnly/chartjs-php)
[![License](https://poser.pugx.org/bbsnly/chartjs-php/license.svg)](https://packagist.org/packages/bbsnly/chartjs-php)This package transforms how you create [ChartJS](https://www.chartjs.org/ "ChartJS") elements by bringing them directly into PHP.
ChartJS-PHP eliminates the complexity of JavaScript when working with [ChartJS](https://www.chartjs.org/ "ChartJS") charts. While [ChartJS](https://www.chartjs.org/ "ChartJS") traditionally requires JavaScript implementation, our PHP solution delivers the same powerful charts through clean, efficient PHP code. By generating [ChartJS](https://www.chartjs.org/ "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](https://getcomposer.org/). Run this command in your project directory:
```shell
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:
```php
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.
```html
= $chart->toHtml('my_chart'); ?>
```---
In the example below we will use the `toJson` method to generate the JSON representation of the chart data.
```html
const ctx = document.getElementById('myChart');
new Chart(ctx, <?= $chart->toJson(); ?>);
```
## Tests
Run the test suite with:
```shell
composer test
```## Contributing
Read our [Contributing](CONTRIBUTING.md) guidelines and start improving ChartJS-PHP today.
## License
The ChartJS PHP is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT).