Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ composer.phar
/vendor/
codeCoverage
build
*DS_Store
*DS_Store
/report.xml
46 changes: 29 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
[![composer.lock](https://poser.pugx.org/halfpastfouram/phpchartjs/composerlock)](https://packagist.org/packages/halfpastfouram/phpchartjs)
[![Build Status](https://travis-ci.org/halfpastfouram/PHPChartJS.svg?branch=master)](https://travis-ci.org/halfpastfouram/PHPChartJS)
[![Code Climate](https://codeclimate.com/github/halfpastfouram/PHPChartJS/badges/gpa.svg)](https://codeclimate.com/github/halfpastfouram/PHPChartJS)
[![Test Coverage](https://codeclimate.com/github/halfpastfouram/PHPChartJS/badges/coverage.svg)](https://codeclimate.com/github/halfpastfouram/PHPChartJS/coverage)
[![Quality](https://scrutinizer-ci.com/g/halfpastfouram/PHPChartJS/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/halfpastfouram/PHPChartJS/)

### Development
[![Latest Unstable Version](https://poser.pugx.org/halfpastfouram/phpchartjs/v/unstable)](https://packagist.org/packages/halfpastfouram/phpchartjs)
[![Build Status](https://travis-ci.org/halfpastfouram/PHPChartJS.svg?branch=dev)](https://travis-ci.org/halfpastfouram/PHPChartJS)

# PHPChartJS
PHP OOP library for [ChartJS](http://www.chartjs.org/)
__PHP OOP library for [ChartJS](http://www.chartjs.org/).__

_Wish to utilize ChartJS but need to manage the graph with PHP? This library is for you._

PHPChartJS acts as an interface between the ChartJS library and the server side code. Set up a chart in no time and have every aspect of the graph managable from your PHP code. This interface is set up to provide code completion in every scenario so you never have to guess or lookup what options are available for the chosen chart. The library is entirely object oriented.

Expand All @@ -25,27 +26,23 @@ This library is still in active development and aims to implement all options Ch
use Halfpastfour\PHPChartJS\Chart\Bar;

$bar = new Bar();
$bar->setId("myBar");
$bar->setId('myChart');

// Set labels
$bar->labels()->exchangeArray(["M", "T", "W", "T", "F", "S", "S"]);

// Add apples
// Add Datasets
$apples = $bar->createDataSet();

$apples->setLabel("apples")
->setBackgroundColor("rgba( 0, 150, 0, .5 )")
->data()->exchangeArray([12, 19, 3, 17, 28, 24, 7]);
->setBackgroundColor("rgba( 0, 150, 0, .5 )")
->data()->exchangeArray([12, 19, 3, 17, 28, 24, 7]);
$bar->addDataSet($apples);

// Add oranges as well
$oranges = $bar->createDataSet();
$oranges->setLabel("oranges")
->setBackgroundColor("rgba( 255, 153, 0, .5 )")
->data()->exchangeArray([ 30, 29, 5, 5, 20, 3 ]);

// Add some extra data
$oranges->data()->append(10);

->setBackgroundColor('rgba( 255, 153, 0, .5 )')
->data()->exchangeArray([30, 29, 5, 5, 20, 3, 10]);
$bar->addDataSet($oranges);

// Render the chart
Expand All @@ -57,9 +54,18 @@ The result generated by rendering the chart will look something like this:
<canvas id="myBar"></canvas>
<script>
window.onload=(function(oldLoad){return function(){
if( oldLoad ) oldLoad();
var ctx = document.getElementById( "myBar" ).getContext( "2d" );
var chart = new Chart( ctx, {"type":"bar","data":{"labels":["M","T","W","T","F","S","S"],"datasets":[{"data":[12,19,3,17,28,24,7],"label":"apples","backgroundColor":"rgba( 0, 150, 0, .5 )"},{"data":[30,29,5,5,20,3,10],"label":"oranges","backgroundColor":"rgba( 255, 153, 0, .5 )"}]}} );
if (oldLoad) {
oldLoad();
}

var ctx = document.getElementById("myChart").getContext("2d");
var chart = new Chart(ctx, {"type":"bar","data":{"labels":["M","T","W","T","F","S","S"],"datasets":[{"data":[12,19,3,17,28,24,7],"label":"apples","backgroundColor":"rgba( 0, 150, 0, .5 )"},{"data":[30,29,5,5,20,3,10],"label":"oranges","backgroundColor":"rgba( 255, 153, 0, .5 )"}]}});

if (! window.hasOwnProperty('chartInstances')) {
window.chartInstances = {};
}

window.chartInstances['myChart'] = chart;
}})(window.onload);
</script>
````
Expand All @@ -68,10 +74,16 @@ var chart = new Chart( ctx, {"type":"bar","data":{"labels":["M","T","W","T","F",
You can provide javascript callbacks with ease:

````php
$myCallback = "function(item){ console.log(item); }";
$myCallback = "function(item){console.log(item);}";
$bar->options()->getTooltips()->callbacks()->setAfterBody($myCallback);
````

The onclick event can also be utilized:

```php
$bar->options()->setOnClick('myClickHandler');
```

### Rendering

Rendering the chart creates some HTML and some JavaScript. The JavaScript contains a JSON object providing the necessary
Expand Down
20 changes: 9 additions & 11 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,35 @@
"php"
],
"readme": "README.md",
"homepage": "http://halfpastfouram.github.io/PHPChartJS",
"homepage": "https://halfpastfouram.github.io/phpchartjs/",
"type": "package",
"version": "v1.2.2",
"version": "v1.3.0",
"license": "AGPL-3.0-or-later",
"authors": [
{
"name": "Bob Kruithof"
}
],
"require": {
"php": ">=5.6.0 || ^7.0",
"php": "^7.4.33 || ^8.0",
"ext-dom": "*",
"ext-json": "*",
"laminas/laminas-json": "3.1.2",
"halfpastfouram/collection": "1.0.0",
"symfony/var-dumper": "^3.4"
"laminas/laminas-json": "^3.3",
"halfpastfouram/collection": "1.0.0"
},
"require-dev": {
"phpunit/phpunit": "5.7.*",
"squizlabs/php_codesniffer": "3.5.3",
"friendsofphp/php-cs-fixer": "*",
"sensiolabs/security-checker": "^5.0"
"friendsofphp/php-cs-fixer": "^v3.15.1",
"phpunit/phpunit": "^9.6"
},
"autoload": {
"psr-4": {
"Halfpastfour\\PHPChartJS\\": "src/",
"Test\\": "test/"
"Halfpastfour\\PHPChartJSTest\\": "test/unit"
}
},
"scripts": {
"test": "./vendor/bin/phpunit",
"test": "./vendor/bin/phpunit -c phpunit.xml --coverage-text --colors=never --log-junit report.xml",
"cs-check": "./vendor/bin/phpcs",
"cs-fix": "./vendor/bin/phpcbf"
}
Expand Down
Loading