2017 © Pedro Peláez
 

package phpchartjs

PHP library for ChartJS 2.0

image

halfpastfouram/phpchartjs

PHP library for ChartJS 2.0

  • Tuesday, April 17, 2018
  • by bobkruithof
  • Repository
  • 5 Watchers
  • 13 Stars
  • 471 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 7 Open issues
  • 3 Versions
  • 24 % Grown

The README.md

Stable

Latest Stable Version Total Downloads License composer.lock Build Status Code Climate Test Coverage Quality, (*1)

Development

Latest Unstable Version Build Status, (*2)

PHPChartJS

PHP OOP library for ChartJS, (*3)

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., (*4)

This library is still in active development and aims to implement all options ChartJS has to offer. Check the Configuration milestone to view the progress of implementing all existing options., (*5)

Example use

<?php
use Halfpastfour\PHPChartJS\Chart\Bar;

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

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

// Add apples
$apples = $bar->createDataSet();
$apples->setLabel("apples")
  ->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);

$bar->addDataSet($oranges);

// Render the chart
echo $bar->render();

The result generated by rendering the chart will look something like this:, (*6)

<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 )"}]}} );
}})(window.onload);
</script>

Callbacks

You can provide javascript callbacks with ease:, (*7)

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

Rendering

Rendering the chart creates some HTML and some JavaScript. The JavaScript contains a JSON object providing the necessary configuration for ChartJS. Every part of the configuration can be cast to an array or a JSON object., (*8)

Render isolated part of the configuration:, (*9)

$json = $myChart->options()->getScales()->jsonSerialize();

Or return an array containing the set configuration:, (*10)

$options = $myChart->options()->getScales()->getArrayCopy();

Pretty mode

If you're not a fan of the long lines of code that are being generated you can force the rendering to be done in pretty mode, see the following example., (*11)

// Render in pretty mode
$bar->render(true);

Want to see more? Fork this project and take a look at the examples in the test folder to explore the different options., (*12)

Configuration options

Every option that is supported by ChartJS will be made available in this library., (*13)

Layers

ChartJS requires you to build a JSON object containing the configuration options you want to set for the current chart. These options are spread over multiple layers inside the configuration object. Accessing these layers with PHPChartJS is super easy., (*14)

Let's say we wanted to access the chart's getAnimation subtree within the options subtree:, (*15)

$getAnimation = $myChart->options()->getAnimation();

You can now adjust any of ChartJS's getAnimation settings by using the getters and setters provided in that particular class., (*16)

Collections

If ChartJS requires an array with certain items as subsets in a configuration option that array will be represented by a collection in PHPChartJS. The collection can always by accessed directly to add, remove and replace values., (*17)

In some cases a specific object with a predetermined list of options is required in a collection. In these cases methods will be provided to create a new instance of said object and adding it to the collection., (*18)

Datasets are stored in a collection:, (*19)

// Create new dataset
$dataset = $myChart->createDataSet();
... (add data to the dataset)
$myChart->addDataSet($dataset);

But the actual data in a dataset is also stored inside a collection:, (*20)

// Create new dataset
$dataset = $myChart->createDataSet();

// Add some data 
$dataset->data()->append(1)->append(2);

// Prepend some data
$dataset->data()->prepend(0);

// Replace data at certain position
$dataset->data()->offsetSet(1, 3);

// Retrieve data from certain position
$value = $dataset->data()->offsetGet(1); // 3

// Add a lot of data at once whilst returning the old values
$oldData = $dataset->data()->exchangeArray([1, 2, 3]); // array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) }

$myChart->addDataSet($dataset);

For more information about the collections visit the halfpastfouram/collection project., (*21)

Installation

Using composer

$ composer require halfpastfouram/phpchartjs

Development

This project uses composer, which should be installed on your system. Most Linux systems have composer available in their PHP packages. Alternatively you can download composer from getcomposer.org., (*22)

If you use the PhpStorm IDE then you can simply init composer through the IDE. However, full use requires the commandline. See PhpStorm help, search for composer., (*23)

To start development, do composer install from the project directory., (*24)

Remark Do not use composer update unless you changed the dependency requirements in composer.json. The difference is that composer install will use composer.lock read-only, while composer update will update your composer.lock file regardless of any change. As the composer.lock file is committed to the repo, other developers might conclude dependencies have changed, while they have not., (*25)

The Versions

31/10 2017

dev-master

9999999-dev http://github.com/halfpastfouram/PHPChartJS

PHP library for ChartJS 2.0

  Sources   Download

AGPL 3.0

The Requires

 

The Development Requires

by Bob Kruithof

php chartjs graph

31/10 2017

v1.0.1

1.0.1.0 http://github.com/halfpastfouram/PHPChartJS

PHP library for ChartJS 2.0

  Sources   Download

AGPL 3.0

The Requires

 

The Development Requires

by Bob Kruithof

php chartjs graph