PHP Classes

PHP Graph: Generate charts to display as images in Web pages

Recommend this page to a friend!
  Info   View files Example   Screenshots Screenshots   View files View files (93)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 242 All time: 8,015 This week: 131Up
Version License PHP version Categories
graph-php 1.0.1GNU General Publi...5HTML, PHP 5, Graphics, Statistics
Description 

Author

This package can generate charts to display as images on Web pages.

It can take parameters that define details of the presentation of a chart like the values, colors, labels, background images, etc.

The package can render images for charts of different types like graph bars, line graphs, histograms, scatters with a cleaned visual graph image.

Picture of Rafael Martin Soto
  Performance   Level  
Name: Rafael Martin Soto <contact>
Classes: 14 packages by
Country: Spain Spain
Innovation award
Innovation award
Nominee: 8x

Winner: 4x

Example

<?php
 
/** example.php // example file for graph-php.class.php
  *
  *
  * @author Rafael Martin Soto
  * @author {@link https://www.inatica.com/ Inatica}
  * @blog {@link https://rafamartin10.blogspot.com/ Blog Rafael Martin Soto}
  * @since October 2021
  * @version 1.0.0
  * @license GNU General Public License v3.0
  *
  * */

require __DIR__ . '/graph-php.class.php';
?><!doctype html>

<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <title>Example Graph-PHP</title>
  <meta name="description" content="Example Graph-PHP">
  <meta name="author" content="SitePoint">

  <meta property="og:title" content="Example Graph-PHP">
  <meta property="og:type" content="website">
  <meta property="og:url" content="https://www.inatica.com">
  <meta property="og:description" content="Example Graph-PHP">

</head>
<body>
<?php

//$graph = new graph();
//$max_min = [10, 15];
//print "for ".$max_min[0]."/".$max_min[1]." diff ".($max_min[1]-$max_min[0]).": ";
//echo $graph->compute_offset($max_min[0])."/".$graph->compute_offset($max_min[1]);
//var_dump($graph->compute_offset($max_min) );
 
//unset($graph);



$graph = new graph();
$graph->bar( [1, 2, 3, 4] );
?>

  <img src="<?php echo $graph->output_gd_png_base64( );?>">
  <?php
 
unset($graph);



$graph = new graph();
$graph->title("Here your graph TITLE");
$graph->bar( [1, 2, 3, 4] );
?>

  <img src="<?php echo $graph->output_gd_png_base64( );?>">
  <?php
 
unset($graph);


$graph = new graph();
$graph->ylabel( 'Here your graph Y LABEL' );
$graph->bar( [1, 2, 3, 4] );
?>

  <img src="<?php echo $graph->output_gd_png_base64( );?>">
  <?php
 
unset($graph);


$graph = new graph();
$graph->xlabel( 'Here your graph X LABEL' );
$graph->bar( [1, 2, 3, 4] );
?>

  <img src="<?php echo $graph->output_gd_png_base64( );?>">
  <?php
 
unset($graph);




$graph = new graph();

$graph->bar( [10, 20, 30, 40], [1, 4, 9, 16] );
$graph->title("With X & Y Values");
?>

  <img src="<?php echo $graph->output_gd_png_base64( );?>">
  <?php
 
unset($graph);


$graph = new graph();

$graph->bar( [1, 2, 3, 4], [1, 4, 9, 16] );
$graph->axes([0, 6, 0, 20]);
$graph->title("Limits Axis X & Y");
?>

  <img src="<?php echo $graph->output_gd_png_base64( );?>">
  <?php
 
unset($graph);


$graph = new graph();
$graph->plot( [1, 1.5, 2, 1.8, 3] );
$graph->title("Simple Plot graph with line");
?>

  <img src="<?php echo $graph->output_gd_png_base64( );?>">
  <?php
 
unset($graph);


$graph = new graph();
$graph->plot( [1, 1.5, 2, 1.8, 3] );
$graph->plot( [2, 2.8, 1.7, 2, 2.3] );
$graph->title("Multi Line");
?>

  <img src="<?php echo $graph->output_gd_png_base64( );?>">
  <?php
 
unset($graph);


$graph = new graph();
$x = $graph->math->linspace( 0, 2, 50 );
$graph->plot( $x, $x, ['label'=>'linear'] );
$graph->plot( $x, $graph->math->pow($x, 2), ['label'=>'quadratic'] );
$graph->plot( $x, $graph->math->pow($x, 3), ['label'=>'cubic'] );
$graph->xlabel('x label');
$graph->ylabel('y label');
$graph->title("Simple Plot. With Legend & Labels X, Y");
$graph->legend( );
?>

  <img src="<?php echo $graph->output_gd_png_base64( );?>">
  <?php
 
unset($graph);



$graph = new graph();

$graph->bar( [1, 2, 3, 4], [10, 9, 10, 8] );
$graph->bar( [1, 2, 3, 4], [8, 6, 9, 7] );
$graph->bar( [1, 2, 3, 4], [6, 5, 7, 5] );
$graph->bar( [1, 2, 3, 4], [3, 3, 4, 2] );
$graph->axes([0, 6, 0, 20]);
$graph->title( 'Multi Bar & fixed Axis Values' );
?>

  <img src="<?php echo $graph->output_gd_png_base64( );?>">
  <?php
 
unset($graph);



$graph = new graph();

$graph->bar( [1, 2, 3, 4, 5, 6, 7], [1, 4, 9, 16, 17, 18, 17] );
$graph->plot( [1, 2, 3, 4, 5, 6, 7], [10,8, 5, 10,15, 16, 15] );
$graph->title( 'Bar & Line' );
?>

  <img src="<?php echo $graph->output_gd_png_base64( );?>">
  <?php
 
unset($graph);



$graph = new graph();

$graph->bar( [1, 2, 3, 4, 5, 6, 7], [1, 4, 9, 16, 17, 18, 17] );
$graph->plot( [1, 2, 3, 4, 5, 6, 7], [10,8, 5, 10,15, 16, 15] );
$graph->title( 'Legend' );
$graph->legend( );
?>

  <img src="<?php echo $graph->output_gd_png_base64( );?>">
  <?php
 
unset($graph);



$graph = new graph();

$graph->bar( [1, 2, 3, 4, 5, 6, 7], [1, 4, 9, 16, 17, 18, 17], ['label'=>'Name Legend 1'] );
$graph->plot( [1, 2, 3, 4, 5, 6, 7], [10,8, 5, 10,15, 16, 15], ['label'=>'Name Legend 2'] );
$graph->title( 'Legend with label names' );
$graph->legend( );
?>

  <img src="<?php echo $graph->output_gd_png_base64( );?>">
  <?php
 
unset($graph);


$graph = new graph();

$graph->plot( [4, 5, 6, 7], [1, 4, 9, 16], ['marker' => 'x'] );
$graph->plot( [4, 5, 6, 7], [5, 8, 7, 10], ['marker' => 'o'] );
$graph->axes([2, 9, 0, 20]);
$graph->title( 'Lines & Legend with "x" & "o" markers' );
$graph->legend( );
?>

  <img src="<?php echo $graph->output_gd_png_base64( );?>">
  <?php
 
unset($graph);

$graph = new graph();

$graph->plot( [4, 5, 6, 7], [1, 4, 9, 16], ['marker' => 'd'] );
$graph->plot( [4, 5, 6, 7], [5, 8, 7, 10], ['marker' => 'd'] );
$graph->axes([2, 9, 0, 20]);
$graph->title( '"d" - Diamond Marker' );
?>

  <img src="<?php echo $graph->output_gd_png_base64( );?>">
  <?php
 
unset($graph);



$graph = new graph();

$graph->plot( [4, 5, 6, 7], [1, 4, 9, 16], 'o' );
$graph->plot( [4, 5, 6, 7], [5, 8, 7, 10], 'x-' );
$graph->axes([2, 9, 0, 20]);
$graph->title( 'Line with marker & only markers' );
?>

  <img src="<?php echo $graph->output_gd_png_base64( );?>">
  <?php
 
unset($graph);





$graph = new graph();
$x = $graph->math->linspace( 0, 5, 20 );
$graph->plot( [ [$x, $x, 'r--'], [$x, $graph->math->pow($x, 2), 'bs'], [$x, $graph->math->pow($x, 3), 'g^'] ] );
$graph->title( 'Colors, disctont. line & markers "--", "square", "^"' );
?>

  <img src="<?php echo $graph->output_gd_png_base64( );?>">
  <?php
 
unset($graph);



$graph = new graph();

$graph->bar( [4.2, 4.4, 4.6, 4.8, 5, 5.2, 5.4, 5.6, 5.8, 6], [1, 4, 9, 16, 1, 4, 9, 16, 5, 4] );
$graph->xlabel( 'entry a' );
$graph->title( 'Histogram of IQ' );
?>

  <img src="<?php echo $graph->output_gd_png_base64( );?>">
  <?php
 
unset($graph);



$graph = new graph();

$graph->plot( [1, 2, 3, 4], [1, 4, 9, 16] );
$graph->plot( [1, 2, 3, 4], [10, 3, 5, 10] );
$graph->plot( [1, 2, 3, 4], [5, 4, 3, 4] );
$graph->plot( [1, 2, 3, 4], [7, 4, 10, 15] );
$graph->plot( [1, 2, 3, 4], [7, 8, 4, 2] );
$graph->plot( [1, 2, 3, 4], [2, 1, 2, 20] );
$graph->plot( [1, 2, 3, 4], [20, 10, 20, 20] );
$graph->plot( [1, 2, 3, 4], [3, 4, 5, 4] );
$graph->plot( [1, 2, 3, 4], [7, 8, 9, 10] );
$graph->plot( [1, 2, 3, 4], [10, 11, 12, 13] );
$graph->plot( [1, 2, 3, 4], [14, 15, 16, 17] );
$graph->plot( [1, 2, 3, 4], [16, 17, 18, 19] );
$graph->axes([null, null, 0, 20]);
$graph->title( 'A lot of lines' );
?>

  <img src="<?php echo $graph->output_gd_png_base64( );?>">
  <?php
 
unset($graph);



$graph = new graph();
$graph->set_drawguidelines( );

$graph->plot( [1, 2, 3, 4], [1, 4, 9, 16] );
$graph->plot( [1, 2, 3, 4], [10, 3, 5, 10] );
$graph->plot( [1, 2, 3, 4], [5, 4, 3, 4] );
$graph->plot( [1, 2, 3, 4], [7, 4, 10, 15] );
$graph->plot( [1, 2, 3, 4], [7, 8, 4, 2] );
$graph->plot( [1, 2, 3, 4], [2, 1, 2, 20] );
$graph->plot( [1, 2, 3, 4], [20, 10, 20, 20] );
$graph->plot( [1, 2, 3, 4], [3, 4, 5, 4] );
$graph->plot( [1, 2, 3, 4], [7, 8, 9, 10] );
$graph->plot( [1, 2, 3, 4], [10, 11, 12, 13] );
$graph->plot( [1, 2, 3, 4], [14, 15, 16, 17] );
$graph->plot( [1, 2, 3, 4], [16, 17, 18, 19] );
$graph->axes([null, null, 0, 20]);
$graph->title( 'Better with Guidelines' );
?>

  <img src="<?php echo $graph->output_gd_png_base64( );?>">
  <?php
 
unset($graph);



$graph = new graph();

$graph->plot( [4, 5, 6, 7], [1, 4, 9, 16], ['marker' => __DIR__ . '/author.png',] );
$graph->title( 'Custom image ^_^\'' );
?>

  <img src="<?php echo $graph->output_gd_png_base64( );?>">
  <?php
 
unset($graph);



$graph = new graph();

$graph->plot( [4, 5, 6, 7], [1, 4, 9, 16], ['marker' => __DIR__ . '/custom_marker.png', 'label'=>'PHP'] );
$graph->plot( [4, 5, 6, 7], [3, 7, 13, 8], ['marker' => __DIR__ . '/tux.png', 'label'=>'GNU/Linux'] );
$graph->plot( [4, 5, 6, 7], [5, 2, 7, 5], ['marker' => __DIR__ . '/nginx.png', 'label'=>'NGINX'] );
$graph->legendwidthlines( 65 );
$graph->legendlabelheight( 33 );
$graph->title( 'Custom Images with Legend' );
$graph->legend( );
?>

  <img src="<?php echo $graph->output_gd_png_base64( );?>">
  <?php
 
unset($graph);



$graph = new graph();

$graph->plot( [4, 5, 6, 7], [1, 4, 9, 16], ['marker' => __DIR__ . '/custom_marker.png', 'label'=>'PHP'] );
$graph->plot( [4, 5, 6, 7], [3, 7, 13, 8], ['marker' => __DIR__ . '/tux.png', 'label'=>'GNU/Linux'] );
$graph->plot( [4, 5, 6, 7], [5, 2, 7, 5], ['marker' => __DIR__ . '/nginx.png', 'label'=>'NGINX'] );
$graph->set_drawguidelines( );
$graph->legendwidthlines( 65 );
$graph->legendlabelheight( 33 );
$graph->title( 'Better with Guidelines' );
$graph->legend( );
?>

  <img src="<?php echo $graph->output_gd_png_base64( );?>">
  <?php
 
unset($graph);





$graph = new graph();
$graph->imread( __DIR__ .'/background_example.jpg');
$x = $graph->math->linspace( 0, 5, 20 );
$graph->title( 'Original Background' );
$graph->plot( [ [$x, $x, 'r--'], [$x, $graph->math->pow($x, 2), 'bs'], [$x, $graph->math->pow($x, 3), 'g^'] ] );
?>

  <img src="<?php echo $graph->output_gd_png_base64( );?>">
  <?php
 
unset($graph);





$graph = new graph();
$graph->imread( __DIR__ .'/background_example.jpg');
$graph->title( 'Scaled Bicubic Background' );
$graph->bckgr_img_gd->imagescale( 64, -1, IMG_BICUBIC );
$x = $graph->math->linspace( 0, 5, 20 );
$graph->plot( [ [$x, $x, 'r--'], [$x, $graph->math->pow($x, 2), 'bs'], [$x, $graph->math->pow($x, 3), 'g^'] ] );
?>

  <img src="<?php echo $graph->output_gd_png_base64( );?>">
  <?php
 
unset($graph);



$graph = new graph();
$graph->imread( __DIR__ .'/background_example.jpg');
$graph->title( 'Smooth Background' );
$graph->bckgr_img_gd->filter( IMG_FILTER_SMOOTH, -7 );
$x = $graph->math->linspace( 0, 5, 20 );
$graph->plot( [ [$x, $x, 'r--'], [$x, $graph->math->pow($x, 2), 'bs'], [$x, $graph->math->pow($x, 3), 'g^'] ] );
?>

  <img src="<?php echo $graph->output_gd_png_base64( );?>">
  <?php
 
unset($graph);


$graph = new graph();
$graph->imread( __DIR__ .'/background_example.jpg');
$graph->title( 'Gaussian Blur Filter Background' );
for(
$i = 0; $i< 10; $i++ ){
 
$graph->bckgr_img_gd->filter( IMG_FILTER_GAUSSIAN_BLUR );
}
$x = $graph->math->linspace( 0, 5, 20 );
$graph->plot( [ [$x, $x, 'r--'], [$x, $graph->math->pow($x, 2), 'bs'], [$x, $graph->math->pow($x, 3), 'g^'] ] );
?>

  <img src="<?php echo $graph->output_gd_png_base64( );?>">
  <?php
 
unset($graph);







$graph = new graph();
$graph->imread( __DIR__ .'/background_example.jpg');
$graph->title( 'Sepia, Brightness & contrast Background' );
$graph->bckgr_img_gd->filter( IMG_FILTER_SEPIA, 4, 80 );
$graph->bckgr_img_gd->filter( IMG_FILTER_BRIGHTNESS, -50 );
$graph->bckgr_img_gd->filter( IMG_FILTER_BRIGHTNESS, 100 );
$graph->bckgr_img_gd->filter( IMG_FILTER_CONTRAST, 50 );
$graph->bckgr_img_gd->filter( IMG_FILTER_BRIGHTNESS, 100 );
$x = $graph->math->linspace( 0, 5, 20 );
$graph->plot( [ [$x, $x, 'r--'], [$x, $graph->math->pow($x, 2), 'bs'], [$x, $graph->math->pow($x, 3), 'g^'] ] );
?>

  <img src="<?php echo $graph->output_gd_png_base64( );?>">
  <?php
 
unset($graph);




$graph = new graph();
$graph->imread( __DIR__ .'/background_example.jpg');
$graph->title( 'Gray, Brightness & contrast Background' );
$graph->bckgr_img_gd->filter( IMG_FILTER_GRAYSCALE );
$graph->bckgr_img_gd->filter( IMG_FILTER_BRIGHTNESS, -50 );
$graph->bckgr_img_gd->filter( IMG_FILTER_BRIGHTNESS, 100 );
$graph->bckgr_img_gd->filter( IMG_FILTER_CONTRAST, 50 );
$graph->bckgr_img_gd->filter( IMG_FILTER_BRIGHTNESS, 115 );
$x = $graph->math->linspace( 0, 5, 20 );
$graph->plot( [ [$x, $x, 'r--'], [$x, $graph->math->pow($x, 2), 'bs'], [$x, $graph->math->pow($x, 3), 'g^'] ] );
?>

  <img src="<?php echo $graph->output_gd_png_base64( );?>">
  <?php
 
unset($graph);




$graph = new graph();
$graph->imread( __DIR__ .'/background_example.jpg');
$graph->title( 'B/W, Brightness & contrast Background' );
$graph->bckgr_img_gd->filter( IMG_FILTER_BLACK_WHITE );
$graph->bckgr_img_gd->filter( IMG_FILTER_BRIGHTNESS, -50 );
$graph->bckgr_img_gd->filter( IMG_FILTER_BRIGHTNESS, 100 );
$graph->bckgr_img_gd->filter( IMG_FILTER_CONTRAST, 50 );
$graph->bckgr_img_gd->filter( IMG_FILTER_BRIGHTNESS, 100 );
$x = $graph->math->linspace( 0, 5, 20 );
$graph->plot( [ [$x, $x, 'r--'], [$x, $graph->math->pow($x, 2), 'bs'], [$x, $graph->math->pow($x, 3), 'g^'] ] );
?>

  <img src="<?php echo $graph->output_gd_png_base64( );?>">
  <?php
 
unset($graph);




// Interpolation IMG_NEAREST_NEIGHBOUR
$graph = new graph();
$graph->imread( __DIR__ .'/background_example.jpg');
$graph->title( 'Scaled & unscaled with Bilinear/G. Blur Background' );

$graph->bckgr_img_gd->imagescale( 64, -1, IMG_BILINEAR_FIXED );
 
$graph->bckgr_img_gd->filter( IMG_FILTER_GAUSSIAN_BLUR );
$graph->bckgr_img_gd->imagescale( 400, -1, IMG_BILINEAR_FIXED );

$x = $graph->math->linspace( 0, 5, 20 );
$graph->plot( [ [$x, $x, 'r--'], [$x, $graph->math->pow($x, 2), 'bs'], [$x, $graph->math->pow($x, 3), 'g^'] ] );
?>

  <img src="<?php echo $graph->output_gd_png_base64( );?>">
  <?php
 
unset($graph);



// Interpolation IMG_NEAREST_NEIGHBOUR
$graph = new graph();
$graph->imread( __DIR__ .'/background_example.jpg');
$graph->title( 'Scaled & unscaled with Bicubic/G. Blur Background x1' );

$graph->bckgr_img_gd->imagescale( 64, -1, IMG_BICUBIC );
$graph->bckgr_img_gd->filter( IMG_FILTER_GAUSSIAN_BLUR );
$graph->bckgr_img_gd->imagescale( 400, -1, IMG_BICUBIC );

$x = $graph->math->linspace( 0, 5, 20 );
$graph->plot( [ [$x, $x, 'r--'], [$x, $graph->math->pow($x, 2), 'bs'], [$x, $graph->math->pow($x, 3), 'g^'] ] );
?>

  <img src="<?php echo $graph->output_gd_png_base64( );?>">
  <?php
 
unset($graph);



// Interpolation IMG_NEAREST_NEIGHBOUR
$graph = new graph();
$graph->imread( __DIR__ .'/background_example.jpg');
$graph->title( 'Scaled & unscaled with Bicubic/G. Blur Background x3' );

$graph->bckgr_img_gd->imagescale( 64, -1, IMG_BICUBIC );
for(
$i = 0; $i< 3; $i++ ){
 
$graph->bckgr_img_gd->filter( IMG_FILTER_GAUSSIAN_BLUR );
}
$graph->bckgr_img_gd->imagescale( 400, -1, IMG_BICUBIC );

$x = $graph->math->linspace( 0, 5, 20 );
$graph->plot( [ [$x, $x, 'r--'], [$x, $graph->math->pow($x, 2), 'bs'], [$x, $graph->math->pow($x, 3), 'g^'] ] );
?>

  <img src="<?php echo $graph->output_gd_png_base64( );?>">
  <?php
 
unset($graph);



// Interpolation IMG_NEAREST_NEIGHBOUR
$graph = new graph();
$graph->imread( __DIR__ .'/background_example.jpg');
$graph->title( 'Scaled & unscaled with Bicubic/G. Blur Background x5' );

$graph->bckgr_img_gd->imagescale( 64, -1, IMG_BICUBIC );
for(
$i = 0; $i< 5; $i++ ){
 
$graph->bckgr_img_gd->filter( IMG_FILTER_GAUSSIAN_BLUR );
}
$graph->bckgr_img_gd->imagescale( 400, -1, IMG_BICUBIC );

$x = $graph->math->linspace( 0, 5, 20 );
$graph->plot( [ [$x, $x, 'r--'], [$x, $graph->math->pow($x, 2), 'bs'], [$x, $graph->math->pow($x, 3), 'g^'] ] );
?>

  <img src="<?php echo $graph->output_gd_png_base64( );?>">
  <?php
 
unset($graph);



$graph = new graph();
$graph->imread( __DIR__ .'/background_example.jpg');
$x = $graph->math->linspace( 0, 2, 50 );
$graph->plot( $x, $x, ['label'=>'linear'] );
$graph->plot( $x, $graph->math->pow($x, 2), ['label'=>'quadratic'] );
$graph->plot( $x, $graph->math->pow($x, 3), ['label'=>'cubic'] );
$graph->xlabel('Don\'t use darkness images O_o. Is an example');
$graph->ylabel('y label');
$graph->title("Simple Plot with background & Legend");
$graph->legend( );
?>

  <img src="<?php echo $graph->output_gd_png_base64( );?>">
  <?php
 
unset($graph);



$graph = new graph();
$graph->imread( __DIR__ .'/background_example.jpg');
$graph->set_drawguidelines( );
$x = $graph->math->linspace( 0, 2, 50 );
$graph->plot( $x, $x, ['label'=>'linear'] );
$graph->plot( $x, $graph->math->pow($x, 2), ['label'=>'quadratic'] );
$graph->plot( $x, $graph->math->pow($x, 3), ['label'=>'cubic'] );
$graph->xlabel('Horrible, but is an example of what you can do ;D');
$graph->ylabel('y label');
$graph->title("Simple Plot with background, Legend & Guidelines");
$graph->legend( );
?>

  <img src="<?php echo $graph->output_gd_png_base64( );?>">
  <?php
 
unset($graph);







$graph = new graph();
$graph->plot( [4, 5, 6, 7], [1, 4, 9, 13], ['marker' => __DIR__ . '/custom_marker.png', 'label'=>'PHP'] );
$graph->plot( [4, 5, 6, 7], [3, 6, 12, 8], ['marker' => __DIR__ . '/tux.png', 'label'=>'GNU/Linux'] );
$graph->plot( [4, 5, 6, 7], [5, 2, 7, 5], ['marker' => __DIR__ . '/nginx.png', 'label'=>'NGINX'] );
$graph->legendwidthlines( 65 );
$graph->legendlabelheight( 33 );
$graph->imread( __DIR__ .'/background_example.jpg');
$graph->title( 'Better use of background' );
$graph->set_drawguidelines( );
$graph->bckgr_img_gd->filter( IMG_FILTER_GRAYSCALE );
$graph->bckgr_img_gd->filter( IMG_FILTER_BRIGHTNESS, -50 );
$graph->bckgr_img_gd->filter( IMG_FILTER_BRIGHTNESS, 100 );
$graph->bckgr_img_gd->filter( IMG_FILTER_CONTRAST, 50 );
$graph->bckgr_img_gd->filter( IMG_FILTER_BRIGHTNESS, 115 );
$graph->bckgr_img_gd->flip( );
$graph->legend();
?>

  <img src="<?php echo $graph->output_gd_png_base64( );?>">
  <?php
 
unset($graph);







$graph = new graph();
$graph->plot( [40000, 50000, 60000, 70000], [1, 4, 9, 13] );
$graph->title( 'Rotation Xticks' );
$graph->set_drawguidelines( );
$graph->xticks( ['rotation' => 45]);
?>

  <img src="<?php echo $graph->output_gd_png_base64( );?>">
  <?php
 
unset($graph);



$graph = new graph();

$arr_values = [
    [ [
1, 1.5, 2, 2, 3, 4], [10, 9.5, 9, 10, 8, 9] ],
    [ [
4, 5, 5.7, 6, 7, 8], [8, 6, 7.3, 8, 7, 8] ],
];

$graph->title( 'Scatter' );
$graph->scatter( $arr_values );
?>

  <img src="<?php echo $graph->output_gd_png_base64( );?>">
  <?php
 
unset($graph);



$graph = new graph();

$arr_values = [
    [ [
1, 1.5, 2, 2, 3, 4], [10, 9.5, 9, 10, 8, 9], ['label'=>'Male'] ],
    [ [
4, 5, 5.7, 6, 7, 8], [8, 6, 7.3, 8, 7, 8], ['label'=>'Female'] ],
];

$graph->title( 'Scatter & legend' );
$graph->scatter( $arr_values );
$graph->legend( );
?>

  <img src="<?php echo $graph->output_gd_png_base64( );?>">
  <?php
 
unset($graph);


$graph = new graph();
$graph->title( 'VALUES FOR HISTOGRAM' );
$graph->bar( [10, 11, 40, 45, 50, 55, 60, 60, 70, 80, 85, 90, 95, 100, 105, 106, 107, 108, 109, 110, 111, 112, 123, 140, 150] );
?>

  <img src="<?php echo $graph->output_gd_png_base64( );?>">
  <?php
 
unset($graph);


$graph = new graph();
$graph->title( 'DEFAULT HISTOGRAM' );
$graph->hist( [10, 11, 40, 45, 50, 55, 60, 60, 70, 80, 85, 90, 95, 100, 105, 106, 107, 108, 109, 110, 111, 112, 123, 140, 150] );
?>

  <img src="<?php echo $graph->output_gd_png_base64( );?>">
  <?php
 
unset($graph);


$graph = new graph();
$graph->title( '30 xval HISTOGRAM' );
$graph->hist( [10, 11, 40, 45, 50, 55, 60, 60, 70, 80, 85, 90, 95, 100, 105, 106, 107, 108, 109, 110, 111, 112, 123, 140, 150], ['num_blocks' => 30] );
?>

  <img src="<?php echo $graph->output_gd_png_base64( );?>">
  <?php
 
unset($graph);


$graph = new graph();
$graph->title( '7 xval HISTOGRAM' );
$graph->hist( [10, 11, 40, 45, 50, 55, 60, 60, 70, 80, 85, 90, 95, 100, 105, 106, 107, 108, 109, 110, 111, 112, 123, 140, 150], ['num_blocks' => 7] );
?>

  <img src="<?php echo $graph->output_gd_png_base64( );?>">
  <?php
 
unset($graph);


?>
</body>
</html>


Details

Graph-PHP Graph charts in PHP

graph charts, graph bars, histograms, graph lines, scratter, marks

V 1.0.1

CREATE GRAPHS IN PHP:

You can create graphs charts with bars, graphs lines with marks, graphs with background images, histograms, ....

SCREENSHOTS:

Screenshot graph lines created in Pure PHP

Nice line graph display with markers

Screenshot graph bar & lines created in Pure PHP

Graph Bars & lines

Screenshot graph lines created in Pure PHP

Nice Math line graph

And now some group of samples:

Screenshot graphs created in Pure PHP Screenshot graphs created in Pure PHP Screenshot graphs created in Pure PHP

# REQUERIMENTS:

Requires packages:

img2img (https://github.com/vivesweb/img2img)

ext-op-ml-php (https://github.com/vivesweb/ext_op_ml)

Requires DeJaVu Fonts: (https://travis-ci.org/dejavu-fonts/dejavu-fonts)

- A minimum (minimum, minimum, minimum requeriments is needed). Tested on:

- Simple Raspberry pi (B +	512MB	700 MHz ARM11) with Raspbian Lite PHP7.3 (i love this gadgets)  :heart_eyes:

- VirtualBox Ubuntu Server 20.04.2 LTS (Focal Fossa) with PHP7.4.3

- Red Hat Enterprise Linux Server release 6.10 (Santiago) PHP Version 7.3.25 (Production Server) 512Mb Memory Limit

- Red Hat Enterprise Linux release 8.4 (Ootpa). PHP Version 8.0.11 (Production Server) 512Mb Memory Limit

# SERVER REQUERIMENTS:

- PHP with GD enabled: sudo apt install php-gd

- img2img class: If you need to use IMG_FILTER_SCATTER you need at least PHP >= PHP7.4.0. See official doc: https://www.php.net/manual/en/function.imagefilter.php

- PHP with Imagick for some functions. It is not required for by most methods, but in some, like reading pdf or psd, you will need it. Install: sudo apt install php-imagick
  
- Requires for Instagram background effects: https://github.com/zaachi/PHP-Instagram-effects

FILES:

graph-php.class.php -> Main File.

example.php -> Example File.

# INSTALLATION: A lot of easy :smiley:. It is written in PURE PHP. Only need to include the files. Tested on basic PHP installation

     require_once( 'graph-php.class.php' );
     

# NOTES: Before draw the Graph, first we need to prepare values inside (as type bar, lines, etc....)

RESUME OF METHODS:

  • CREATE GRAPH OBJECT:

$graph = new graph( $cfg=null );

default $cfg:

[
'width'     => 6.4, // 6.4 inches
    'height'    => 4.8, // 4.8 inches 
    'dpi'       => 100, // 100 dpis
    'padding'   => .6, // 0.6 inches
    'fontdir'   => __DIR__.'/fonts',
    'fontfamilypath' => 'dejavu-fonts-ttf-2.37/ttf',
    'font'      => 'DejaVuSans.ttf',    
    'fontsize'  => 10.5,   
    'xtickfontsize'  => 10.5,   
    'ytickfontsize'  => 10.5,
    'axes'      => [ 'prop_cycle' => []
        ],
    'lines'     =>
        ['width' => 3],
    'values'     => [],
    'x_values'     => [],
    'y_values'     => [],
    'ylabel'    => '',
    'xlabel'    => '',
    'title'     => '',
    'cycler'    => [ 
        'color' => [ 
            'default' => ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd', '#8c564b', '#e377c2', '#7f7f7f', '#bcbd22', '#17becf']
            ],
        'linestyle'  => [ '-', '--', ':', '-.' ]
        ],
    'backgroundstyle'       => 'solid',
    'backgroundcolor'       => '#ffffff',
    'bordertype'            => 'square',
    'paddingleft'           => .79,
    'paddingright'          => .63,
    'paddingtop'            => .58,
    'paddingbottom'         => .515,
    'paddinginsideleft'     => .2,
    'paddinginsideright'    => .2,
    'paddinginsidetop'      => .15,
    'paddinginsidebottom'   => .15,
    'ymarginleftlabel'      => 10,
    'xmarginlabelsticks'    => 16,
    'ymarginlabelsticks'    => 10,
    'xshowlabelticks'       => true,
    'yshowlabelticks'       => true,
    'margintitle'           => 50,
    'x_drawguidelines'      => false,
    'y_drawguidelines'      => false,
	'centerlabels'			=> false,
	'bars'	                => ['percmarginbetwbars' => 20 ],
	'legend'			    => false,
	'legendmarginleft'		=> 7,
    'legendmargintop'		=> 6,
    'legendpaddingleft'		=> 5,
    'legendpaddingtop'		=> 1,
    'legendpaddingright'	=> 5,
    'legendpaddingbottom'	=> 3,
    'legendwidthlines'	    => 28,
    'legendlabelheight'     => 21,
	'width_marker_x'		=> 9,
	'height_marker_x'		=> 9,
	'width_marker_o'		=> 10,
	'height_marker_o'		=> 10,
    'xticks'                => ['rotation' => 0]
	
]; // /$default_cfg

Example:

  $graph = new graph();

- PREPARE BAR GRAPH:

$graph->bar( $array_values, $arr_values_y_param = null, $cfg = null );

This method prepare a serie of values in BAR format. Do not echo the graph. To see how to echo graph, see output_gd_png_base64() method.

$array_values: Values of Axis X

$arr_values_y_param: Values of Axis Y. If not given, each $array_values will be the Axys Y values and each value will be an index position in Axis X automatically

$cfg: *see CFG_GRAPH_TYPES

Example:

  $graph = new graph();
  $graph->bar( [1, 2, 3, 4] );
  echo '<img src="'.$graph->output_gd_png_base64( ).'" >'; // Echo img raw data in html page
  

With this simple code you will generate the most simplest graph:

Simple graph bar

Example with x & y values:

  $graph = new graph();
  $graph->bar( [10, 20, 30, 40], [1, 4, 9, 16] );
  $graph->title("With X & Y Values"); // Set the title of the bar. See title() method
  echo '<img src="'.$graph->output_gd_png_base64( ).'" >'; // Echo img raw data in html page
  

With this simple code you will generate the most simplest graph:

Simple graph bar with X & Y values

  • PREPARE LINE GRAPH:

$graph->plot( $array_values, $arr_values_y_param = null, $cfg = null );

This method prepare a serie of values in LINES format. Do not echo the graph. To see how to echo graph, see output_gd_png_base64() method.

$array_values: Values of Axis X

$arr_values_y_param: Values of Axis Y. If not given, each $array_values will be the Axys Y values and each value will be an index position in Axis X automatically

$cfg: *see CFG_GRAPH_TYPES

Example:

  $graph = new graph();
  $graph->plot( [1, 1.5, 2, 1.8, 3] );
  $graph->title("Simple Plot graph with line"); // Set the title of the bar. See title() method
  echo '<img src="'.$graph->output_gd_png_base64( ).'" >'; // Echo img raw data in html page
  

With this simple code you will generate the most simplest line graph:

Simple plot line graph

  • PREPARE MULTILINE GRAPH:

You can prepare multilines calling plot() method as many series as you need.

Example:

  $graph = new graph();
  $graph->plot( [1, 1.5, 2, 1.8, 3] );
  $graph->plot( [2, 2.8, 1.7, 2, 2.3] );
  $graph->title("Multi Line"); // Set the title of the bar. See title() method
  echo '<img src="'.$graph->output_gd_png_base64( ).'" >'; // Echo img raw data in html page
  

With this simple code you will generate Multiline Graph:

Plot Multiline graph

  • MERGE LINES & BAR GRAPH:

You can merge line/multilines & bars calling plot()/bar() method as many series as you need.

Example:

  $graph = new graph();
  $graph->bar( [1, 2, 3, 4, 5, 6, 7], [1, 4, 9, 16, 17, 18, 17] );
  $graph->plot( [1, 2, 3, 4, 5, 6, 7], [10,8, 5, 10,15, 16, 15] );
  $graph->title( 'Bar & Line' ); // Set the title of the bar. See title() method
  echo '<img src="'.$graph->output_gd_png_base64( ).'" >'; // Echo img raw data in html page
  

With this simple code you will generate Line & Bar Graph:

Plot Line & Bar graph

- SET GRAPH TITLE:

$graph->title( $title );

$graph->set_title( $title ); // synonymous of title()

This method set the graph TITLE

$title: String with the title

Example:

  $graph = new graph();
  $graph->bar( [1, 2, 3, 4] );
  $graph->title("Here your graph TITLE");
  echo '<img src="'.$graph->output_gd_png_base64( ).'" >'; // Echo img raw data in html page
  

With this simple code you will generate Simple Bar graph with title:

Simple graph bar with title

- SET GRAPH X LABEL:

$graph->xlabel( $xlabel );

$graph->set_xlabel( $xlabel ); // synonymous of xlabel()

This method set the graph X LABEL

$xlabel: String with the X LABEL

Example:

  $graph = new graph();
  $graph->bar( [1, 2, 3, 4] );
  $graph->xlabel( 'Here your graph X LABEL' );
  echo '<img src="'.$graph->output_gd_png_base64( ).'" >'; // Echo img raw data in html page
  

With this simple code you will generate Simple Bar graph with X label:

Simple graph bar with X LABEL

- SET GRAPH Y LABEL:

$graph->ylabel( $ylabel );

$graph->set_ylabel( $ylabel ); // synonymous of ylabel()

This method set the graph Y LABEL

$ylabel: String with the Y LABEL

Example:

  $graph = new graph();
  $graph->bar( [1, 2, 3, 4] );
  $graph->ylabel( 'Here your graph Y LABEL' );
  echo '<img src="'.$graph->output_gd_png_base64( ).'" >'; // Echo img raw data in html page
  

With this simple code you will generate Simple Bar graph with Y label:

Simple graph bar with Y LABEL

- LIMIT AXES VALUES:

$graph->axes( $xmin, $xmax, $ymin, $ymax );

$graph->axes( $arr_min_max );

This method set the min & max values of axis X & Y

You can send each value separatelly with

$xmin: Minimum Value for Axis X

$xmax: Maximum Value for Axis X

$ymin: Minimum Value for Axis Y

$ymax: Maximum Value for Axis Y

If you prefer, you can send the params in one array as:

$arr_params = [$xmin, $xmax, $ymin, $ymax]

If any of params is NULL, then the system will select it automatically

Example:

  $graph = new graph();
  $graph->bar( [1, 2, 3, 4], [1, 4, 9, 16] );
  $arr_params = [0, 6, 0, 20];
  $graph->axes($arr_params); // Same as $graph->axes(0, 6, 0, 20);
  $graph->title("Limits Axis X & Y"); // Set the Title
  echo '<img src="'.$graph->output_gd_png_base64( ).'" >'; // Echo img raw data in html page
  

With this simple code you will generate Simple Bar graph with limits in X & Y axis:

Simple graph bar with Limits Max & Min in Axes X & Y

- SHOW LEGEND:

$graph->legend( $legend = true );

Calling this method the graph show the legend. You can set it to false to hide the legend. By default the legend is not showed.

By default, the title of each serie to show in legend will be the index of the serie. You can set the name of the title with $cfg param whe call bar(), plot() methods

Example:

$graph->bar( [1, 2, 3, 4, 5, 6, 7], [1, 4, 9, 16, 17, 18, 17] );
$graph->plot( [1, 2, 3, 4, 5, 6, 7], [10,8, 5, 10,15, 16, 15] );
$graph->title( 'Legend' );
$graph->legend( );
echo '<img src="'.$graph->output_gd_png_base64( ).'" >'; // Echo img raw data in html page
  

With this simple code you will generate Graph with legend:

Simple graph with legend

Example with legend names:

$graph->bar( [1, 2, 3, 4, 5, 6, 7], [1, 4, 9, 16, 17, 18, 17], ['label'=>'Name Legend 1'] );
$graph->plot( [1, 2, 3, 4, 5, 6, 7], [10,8, 5, 10,15, 16, 15], ['label'=>'Name Legend 2'] );
$graph->title( 'Legend with label names' );
$graph->legend( );
echo '<img src="'.$graph->output_gd_png_base64( ).'" >'; // Echo img raw data in html page
  

With this simple code you will generate Graph with legend:

Simple graph with legend names

- MARKERS:

When plot a line or bar, you can send a param marker. Markers allowed: 'x', 'o', 'd', '^', 's'

*See USING LINES, COLORS & MARKERS.

Example:

$graph->plot( [4, 5, 6, 7], [1, 4, 9, 16], ['marker' => 'x'] );
$graph->plot( [4, 5, 6, 7], [5, 8, 7, 10], ['marker' => 'o'] );
$graph->axes([2, 9, 0, 20]);
$graph->title( 'Lines & Legend with "x" & "o" markers' );
$graph->legend( );
echo '<img src="'.$graph->output_gd_png_base64( ).'" >'; // Echo img raw data in html page
  

With this simple code you will generate Graph with markers:

Simple graph with markers

You can pass directly the marker and format line. Example of use with line with marker and only marker:

$graph->plot( [4, 5, 6, 7], [1, 4, 9, 16], 'o' );
$graph->plot( [4, 5, 6, 7], [5, 8, 7, 10], 'x-' );
$graph->axes([2, 9, 0, 20]);
$graph->title( 'Line with marker & only markers' );
echo '<img src="'.$graph->output_gd_png_base64( ).'" >'; // Echo img raw data in html page
  

With this simple code you will generate Graph with markers and serie with marker and line:

Simple graph with markers and markers with lines

- PASS MORE THAN 1 PLOT VALUES, COLORS & MARKERS:

In 1 simple row you can pass more than 1 array of values with colors & markers. See USING LINES, COLORS & MARKERS:

$graph = new graph();
$x = $graph->math->linspace( 0, 5, 20 );
$graph->plot( [ [$x, $x, 'r--'], [$x, $graph->math->pow($x, 2), 'bs'], [$x, $graph->math->pow($x, 3), 'g^'] ] );
$graph->title( 'Colors, disctont. line & markers "--", "square", "^"' );
echo '<img src="'.$graph->output_gd_png_base64( ).'" >'; // Echo img raw data in html page
  

With this simple code you will generate Graph with mutiple values, colors & markers:

Simple graph with multi array of values, colors & markers

- CUSTOM PNG MARKERS:

You can use your custom PNG markers. The file need to be .png file (NOTE: ONLY WORKS WITH PNG-8. Don't work with PNG-24)

Example:

$graph->plot( [4, 5, 6, 7], [1, 4, 9, 16], ['marker' => __DIR__ . '/author.png',] );
$graph->title( 'Custom image ^_^\'' );
echo '<img src="'.$graph->output_gd_png_base64( ).'" >'; // Echo img raw data in html page
  

With this simple code you will generate Graph with custom markers:

Simple graph with custom markers

- SHOW GUIDELINES:

You can use background guidelines

$graph->set_drawguidelines( $show=true );

Example:

$graph->plot( [4, 5, 6, 7], [1, 4, 9, 16], ['marker' => __DIR__ . '/custom_marker.png', 'label'=>'PHP'] );
$graph->plot( [4, 5, 6, 7], [3, 7, 13, 8], ['marker' => __DIR__ . '/tux.png', 'label'=>'GNU/Linux'] );
$graph->plot( [4, 5, 6, 7], [5, 2, 7, 5], ['marker' => __DIR__ . '/nginx.png', 'label'=>'NGINX'] );
$graph->set_drawguidelines( );
$graph->legendwidthlines( 65 ); // Change width of legend lines
$graph->legendlabelheight( 33 ); // Change height of legend labels
$graph->title( 'Better with Guidelines' );
$graph->legend( );
echo '<img src="'.$graph->output_gd_png_base64( ).'" >'; // Echo img raw data in html page
  

With this simple code you will generate Graph with custom markers & Guidelines:

Simple graph with Guidelines

- SHOW X GUIDELINES:

You can show only background X guidelines

$graph->set_x_drawguidelines( $show=true );

Example:

$graph->set_x_drawguidelines( );

- SHOW Y GUIDELINES:

You can show only background Y guidelines

$graph->set_y_drawguidelines( $show=true );

Example:

$graph->set_y_drawguidelines( );

- CHANGE LINE WIDTH OF LEGEND:

Sometimes you will need to change the width of the series line at Legend. You can to set the with legendwidthlines()

$graph->legendwidthlines( $legendwidthlines=28 );

See example at SHOW GUIDELINES

- CHANGE LABEL HEIGHT OF LEGEND:

Sometimes you will need to change the height of the labe series line at Legend. You can to set the with legendlabelheight()

$graph->legendlabelheight( $legendlabelheight=21 );

See example at SHOW GUIDELINES

- SET BACKGROUND IMAGE:

You can set a background image. Support .jpg, .png, .gif. The class that load the image is img2img (https://github.com/vivesweb/img2img). See the documentation for possibilites

$graph->imread( $filename );

Example:

$graph = new graph();
$graph->imread( __DIR__ .'/background_example.jpg');
$x = $graph->math->linspace( 0, 5, 20 );
$graph->title( 'Original Background' );
$graph->plot( [ [$x, $x, 'r--'], [$x, $graph->math->pow($x, 2), 'bs'], [$x, $graph->math->pow($x, 3), 'g^'] ] );
echo '<img src="'.$graph->output_gd_png_base64( ).'" >'; // Echo img raw data in html page
  

With this simple code you will generate Graph with background image:

Simple graph with Background image

Is not a good idea use original image. You will need to transform it to more clearer picture. With the img2img class you can transform it directly. img2img (https://github.com/vivesweb/img2img). See the documentation for possibilites. You can see https://github.com/vivesweb/graph-php/blob/main/example.php to take some ideas for its use.

The class of img2img is loaded at $graph->bckgr_img_gd. You can use directly the methods of img2img. See the next example.

Example with cleared background image:

$graph->plot( [4, 5, 6, 7], [1, 4, 9, 13], ['marker' => __DIR__ . '/custom_marker.png', 'label'=>'PHP'] );
$graph->plot( [4, 5, 6, 7], [3, 6, 12, 8], ['marker' => __DIR__ . '/tux.png', 'label'=>'GNU/Linux'] );
$graph->plot( [4, 5, 6, 7], [5, 2, 7, 5], ['marker' => __DIR__ . '/nginx.png', 'label'=>'NGINX'] );
$graph->legendwidthlines( 65 );
$graph->legendlabelheight( 33 );
$graph->imread( __DIR__ .'/background_example.jpg');
$graph->title( 'Better use of background' );
$graph->set_drawguidelines( );
$graph->bckgr_img_gd->filter( IMG_FILTER_GRAYSCALE );
$graph->bckgr_img_gd->filter( IMG_FILTER_BRIGHTNESS, -50 );
$graph->bckgr_img_gd->filter( IMG_FILTER_BRIGHTNESS, 100 );
$graph->bckgr_img_gd->filter( IMG_FILTER_CONTRAST, 50 );
$graph->bckgr_img_gd->filter( IMG_FILTER_BRIGHTNESS, 115 );
$graph->bckgr_img_gd->flip( );
$graph->legend();
echo '<img src="'.$graph->output_gd_png_base64( ).'" >'; // Echo img raw data in html page
  

With this simple code you will generate Graph with cleared background image:

Simple graph with Cleared Background image

- ROTATE TICKS X LABELS:

You can rotate text on X Axis labels. In this case we use 45º. if you want original horizontal, set rotation to 0.

$graph->xticks( ['rotation' => 45] );

Example:

$graph->plot( [40000, 50000, 60000, 70000], [1, 4, 9, 13] );
$graph->title( 'Rotation Xticks' );
$graph->set_drawguidelines( );
$graph->xticks( ['rotation' => 45]);
echo '<img src="'.$graph->output_gd_png_base64( ).'" >'; // Echo img raw data in html page
  

With this simple code you will generate Graph with rotation X labels:

Simple graph with rotation 45º X labes

- SCATTER:

You can create a Scatter Graph.

$graph->scatter( $arr_values );

$arr_values will be as:

$arr_values = [
	[ [1, 1.5, 2, 2, 3, 4], [10, 9.5, 9, 10, 8, 9] ], // For Serie 1 [ [x_arrayvalues], [y_arrayvalues] ]
    	[ [4, 5, 5.7, 6, 7, 8], [8, 6, 7.3, 8, 7, 8] ], // For Serie 2 [ [x_arrayvalues], [y_arrayvalues] ]
]

If you want labels on Legend, use this:

$arr_values = [
	[ [1, 1.5, 2, 2, 3, 4], [10, 9.5, 9, 10, 8, 9], ['label'=>'Male'] ],
	[ [4, 5, 5.7, 6, 7, 8], [8, 6, 7.3, 8, 7, 8], ['label'=>'Female'] ],
];

Example:

$arr_values = [
		[ [1, 1.5, 2, 2, 3, 4], [10, 9.5, 9, 10, 8, 9], ['label'=>'Male'] ],
		[ [4, 5, 5.7, 6, 7, 8], [8, 6, 7.3, 8, 7, 8], ['label'=>'Female'] ],
];

$graph->title( 'Scatter & legend' );
$graph->scatter( $arr_values );
$graph->legend( );
echo '<img src="'.$graph->output_gd_png_base64( ).'" >'; // Echo img raw data in html page
  

With this simple code you will generate Scatter Graph:

Simple graph with Scatter

- HISTOGRAM:

You can create an histogram from data.

$graph->hist( $arr_values, $cfg = null );

$cfg['num_blocks] = blocks to divide histogram. Default = 10

Example:

$graph->title( 'DEFAULT HISTOGRAM' );
$graph->hist( [10, 11, 40, 45, 50, 55, 60, 60, 70, 80, 85, 90, 95, 100, 105, 106, 107, 108, 109, 110, 111, 112, 123, 140, 150] );
echo '<img src="'.$graph->output_gd_png_base64( ).'" >'; // Echo img raw data in html page
  

With this simple code you will generate Histogram Graph:

Simple graph with histogram

Example of Histogram with 7 blocks:

$graph->title( '7 xval HISTOGRAM' );
$graph->hist( [10, 11, 40, 45, 50, 55, 60, 60, 70, 80, 85, 90, 95, 100, 105, 106, 107, 108, 109, 110, 111, 112, 123, 140, 150], ['num_blocks' => 7] );
echo '<img src="'.$graph->output_gd_png_base64( ).'" >'; // Echo img raw data in html page
  

With this simple code you will generate Histogram Graph with 7 bocks:

Simple graph with histogram

- GET BASE64 IMAGE DATE:

In this earlier version you can get the data in Base64 format and use it directly into html page.

From Version 1.0.1:

  • In $cfg you can specify if you want to draw X ticks labels or Y ticks labes with array configuration: [ 'xshowlabelticks' => true, 'xshowlabelticks' => true ].
  • In $cfg you can sepecify border type: [ 'bordertype' => 'square' ] or [ 'bordertype' => 'halfsquare' ]. Default is square. Halfsquare normaly used when draw subplots.
  • In $cfg you can sepecify the ymarginleftlabel: [ 'ymarginleftlabel' => 1 ]

$graph->output_gd_png_base64( $cfg = null )

Example:

echo '<img src="'.$graph->output_gd_png_base64( ).'" >'; // Echo img raw data in html page
echo '<img src="'.$graph->output_gd_png_base64( [ 'xshowlabelticks' => true, 'xshowlabelticks' => true ] ).'" >'; // Echo img raw data in html page without ticks labels
  

- SET WIDTH GRAPH:

You can change the default (6.4 inches) width graph in inches

$graph->width( $width = 6.4 );

Example:

$graph->width( 12.8 );
  

- SET HEIGHT GRAPH:

You can change the default (4.8 inches) height graph in inches

$graph->height( $height = 4.8 );

Example:

$graph->height( 9.6 );


- CFG_GRAPH_TYPES - USING LINES, COLORS & MARKERS:

You can set some types of graphs:

  • When prepare a plot, you can to pass a cfg_param with:

'-': Single line

'--': Discontinued line

'bar': Bar

'scatter': Scatter

One of this colors:

['b' => '#0000ff', 'g' => '#008000', 'r' => '#ff0000', 'c' => '#00cccc', 'm' => '#cc0000', 'y' => '#cc0000', 'k' => '#000000', 'w' => '#ffffff',

    '#1f77b4' => '#1f77b4', '#ff7f0e' => '#ff7f0e', '#2ca02c' => '#2ca02c', '#d62728' => '#d62728', '#9467bd' => '#9467bd', '#8c564b' => '#8c564b',
    '#e377c2' => '#e377c2', '#7f7f7f' => '#7f7f7f', '#bcbd22' => '#bcbd22', '#17becf' => '#17becf']

For markers you can to use:

'filename.png' -> Your file name of png of your marker

'o': round marker

'x': X marker

'^': Triangle marker

's': Square marker

'd': Diamond marker

By default, each time that you prepare a values, the system select next color in cycler: cycler' => [

        'color' => [ 
            'default' => ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd', '#8c564b', '#e377c2', '#7f7f7f', '#bcbd22', '#17becf']
            ],
        ],
  

If you want a special color for one serie, you can to pass it in $cfg var when draw plot(), bar(). See the examples

- MATH FUNCTIONS:

$graph->math

This class is provided with the math library ext-op-ml-php (https://github.com/vivesweb/ext_op_ml)

Here is an example of its use. Check the library documentation to see possibilities

Example:

$graph = new graph();
$x = $graph->math->linspace( 0, 2, 50 );
$graph->plot( $x, $x, ['label'=>'linear'] );
$graph->plot( $x, $graph->math->pow($x, 2), ['label'=>'quadratic'] );
$graph->plot( $x, $graph->math->pow($x, 3), ['label'=>'cubic'] );
$graph->xlabel('x label');
$graph->ylabel('y label');
$graph->title("Simple Plot. With Legend & Labels X, Y");
$graph->legend( );
echo '<img src="'.$graph->output_gd_png_base64( ).'" >'; // Echo img raw data in html page
  

With this simple code you will generate Simple Line graph with Math Functions:

Simple graph bar with Limits Max & Min in Axes X & Y. Math Functions

Of course. You can use it freely :vulcan_salute::alien:

By Rafa.

@author Rafael Martin Soto

@author {@link http://www.inatica.com/ Inatica}

@blog {@link https://rafamartin10.blogspot.com/ Rafael Martin's Blog}

@since October 2021

@version 1.0.1

@license GNU General Public License v3.0


Screenshots  
  • background_graph_image
  • bar_line_grahph
  • custom_markers
  • graph_histogram
  • marker_graph
  • mat_graph
  • scatter
  • simple_graph
  Files folder image Files  
File Role Description
Files folder imageext-op-ml-php (2 files)
Files folder imagefonts (1 directory)
Files folder imageimg2img (17 files, 1 directory)
Accessible without login Image file author.png Icon Icon image
Accessible without login Image file background_example.jpg Data Auxiliary data
Accessible without login Image file custom_marker.png Icon Icon image
Accessible without login Plain text file example.php Example Example script
Plain text file graph-php.class.php Class Class source
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Image file multigraph1.jpg Data Auxiliary data
Accessible without login Image file multigraph2.jpg Data Auxiliary data
Accessible without login Image file multigraph3.jpg Icon Icon image
Accessible without login Image file nginx.png Icon Icon image
Accessible without login Plain text file README.md Doc. Documentation
Accessible without login Plain text file releases Data Auxiliary data
Accessible without login Image file sample2.png Icon Icon image
Accessible without login Image file sample3.png Icon Icon image
Accessible without login Image file sample4.png Icon Icon image
Accessible without login Image file samplebackground.png Icon Icon image
Accessible without login Image file samplebarline.png Icon Icon image
Accessible without login Image file samplecustompng.png Icon Icon image
Accessible without login Image file samplegraybackground.png Icon Icon image
Accessible without login Image file sampleguidelines.png Icon Icon image
Accessible without login Image file samplehistogram.png Icon Icon image
Accessible without login Image file samplehistseven.png Icon Icon image
Accessible without login Image file samplelegend.png Icon Icon image
Accessible without login Image file samplelegendnames.png Icon Icon image
Accessible without login Image file samplelimitsxy.png Icon Icon image
Accessible without login Image file samplemarker.png Icon Icon image
Accessible without login Image file samplemarkerplot.png Icon Icon image
Accessible without login Image file samplemarkers.png Icon Icon image
Accessible without login Image file samplemultiline.png Icon Icon image
Accessible without login Image file sampleplotline.png Icon Icon image
Accessible without login Image file samplerotationticks.png Icon Icon image
Accessible without login Image file samplescatter.png Icon Icon image
Accessible without login Image file samplesimple.png Icon Icon image
Accessible without login Image file sampletitle.png Icon Icon image
Accessible without login Image file samplexlabel.png Icon Icon image
Accessible without login Image file samplexyvalues.png Icon Icon image
Accessible without login Image file sampleylabel.png Icon Icon image
Accessible without login Image file tux.png Icon Icon image

  Files folder image Files  /  ext-op-ml-php  
File Role Description
  Accessible without login Plain text file example.php Example Example script
  Plain text file ext-op-ml-php.class.php Class Class source

  Files folder image Files  /  fonts  
File Role Description
Files folder imagedejavu-fonts-ttf-2.37 (8 files, 1 directory)

  Files folder image Files  /  fonts  /  dejavu-fonts-ttf-2.37  
File Role Description
Files folder imagefontconfig (6 files)
  Accessible without login Plain text file AUTHORS Data Auxiliary data
  Accessible without login Plain text file BUGS Data Auxiliary data
  Accessible without login Plain text file langcover.txt Doc. Documentation
  Accessible without login Plain text file LICENSE Lic. License text
  Accessible without login Plain text file NEWS Data Auxiliary data
  Accessible without login Plain text file README.md Doc. Documentation
  Accessible without login Plain text file status.txt Doc. Documentation
  Accessible without login Plain text file unicover.txt Doc. Documentation

  Files folder image Files  /  fonts  /  dejavu-fonts-ttf-2.37  /  fontconfig  
File Role Description
  Accessible without login Plain text file 20-unhint-small-dejavu-sans-mono.conf Data Auxiliary data
  Accessible without login Plain text file 20-unhint-small-dejavu-sans.conf Data Auxiliary data
  Accessible without login Plain text file 20-unhint-small-dejavu-serif.conf Data Auxiliary data
  Accessible without login Plain text file 57-dejavu-sans-mono.conf Data Auxiliary data
  Accessible without login Plain text file 57-dejavu-sans.conf Data Auxiliary data
  Accessible without login Plain text file 57-dejavu-serif.conf Data Auxiliary data

  Files folder image Files  /  img2img  
File Role Description
Files folder imagePHP-Instagram-effects-master (3 files, 3 directories)
  Accessible without login Plain text file example.php Example Example script
  Plain text file img2img.class.php Class Class source
  Accessible without login Image file img2img_result_0.jpg Data Auxiliary data
  Accessible without login Image file img2img_result_1.jpg Icon Icon image
  Accessible without login Image file img2img_result_10.jpg Icon Icon image
  Accessible without login Image file img2img_result_11.jpg Icon Icon image
  Accessible without login Image file img2img_result_2.jpg Data Auxiliary data
  Accessible without login Image file img2img_result_3.jpg Data Auxiliary data
  Accessible without login Image file img2img_result_4.jpg Data Auxiliary data
  Accessible without login Image file img2img_result_5.png Icon Icon image
  Accessible without login Image file img2img_result_6.png Icon Icon image
  Accessible without login Image file img2img_result_7.jpg Data Auxiliary data
  Accessible without login Image file img2img_result_8.jpg Icon Icon image
  Accessible without login Image file img2img_result_9.jpg Icon Icon image
  Accessible without login Plain text file mime_types.php Aux. Auxiliary script
  Accessible without login Image file source_example.jpg Data Auxiliary data
  Accessible without login Plain text file source_example_pdf.pdf Data Auxiliary data

  Files folder image Files  /  img2img  /  PHP-Instagram-effects-master  
File Role Description
Files folder imageassets (5 files)
Files folder imageexamples (13 files)
Files folder imagesrc (1 directory)
  Accessible without login Plain text file composer.json Data Auxiliary data
  Accessible without login Plain text file LICENCE Lic. License text
  Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  img2img  /  PHP-Instagram-effects-master  /  assets  
File Role Description
  Accessible without login Image file bg1.jpg Data Auxiliary data
  Accessible without login Image file pattern1.jpg Icon Icon image
  Accessible without login Image file pattern3.jpg Data Auxiliary data
  Accessible without login Image file pattern4.jpg Data Auxiliary data
  Accessible without login Image file pattern5.jpg Data Auxiliary data

  Files folder image Files  /  img2img  /  PHP-Instagram-effects-master  /  examples  
File Role Description
  Accessible without login Plain text file aqua.php Example Example script
  Accessible without login Plain text file boost.php Example Example script
  Accessible without login Plain text file bubbles.php Example Example script
  Accessible without login Plain text file colorise.php Example Example script
  Accessible without login Plain text file cool.php Example Example script
  Accessible without login Image file example.jpg Icon Icon image
  Accessible without login Plain text file fuzzy.php Example Example script
  Accessible without login Plain text file gray.php Example Example script
  Accessible without login Plain text file light.php Example Example script
  Accessible without login Plain text file old.php Example Example script
  Accessible without login Plain text file old2.php Example Example script
  Accessible without login Plain text file old3.php Example Example script
  Accessible without login Plain text file sepia.php Example Example script

  Files folder image Files  /  img2img  /  PHP-Instagram-effects-master  /  src  
File Role Description
Files folder imageImage (1 file)

  Files folder image Files  /  img2img  /  PHP-Instagram-effects-master  /  src  /  Image  
File Role Description
  Plain text file Filter.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 92%
Total:242
This week:0
All time:8,015
This week:131Up