phpMyGraph

Name:phpMyGraph
Version:5.0.5
Last version date:31-05-2011
Author:Martijn Beulens <mbeulens(at)bawic.nl>

phpMyGraph is written and maintained by Martijn Beulens. This PHP library is written in PHP5 and has no PHP4 support.

This class can be used to render several types of charts in images.
It can render bar, line and polygon charts from an array of chart data parameters.
Several chart parameters may be configured such as the colors, borders, fonts, etc..
Look at the samples below which show you how to use it.

Download

Download the latest copy of phpMyGraph here

What's new in this version

5.0.5 Update

All line and polygon graphs started at 0. This issue is now fixed and the start of the graph will be the first value of the array.

5.0.4 Update

A display bug occured when sending a data array with all negative values.
This problem is solved as you can see in the following examples.

Example 1: All negative values

<?php
        
//Data array with all negative values
        
$data = array(
            
'00' => -1.4,
            
'01' => -1.2,
            
'02' => -1.5,
            
'03' => -2.3,
            
'04' => -3.2,
            
'05' => -3,
            
'06' => -4,
            
'07' => -2.2,
            
'08' => -4,
            
'09' => -1.2,
            
'10' => -1
        
);
    
?>
All negative values

Example 2: All negative starting with 0

<?php
        
//Data array with all negatives but starting with 0
        
$data = array(
            
'00' => 0,
            
'01' => -1.2,
            
'02' => -1.5,
            
'03' => -2.3,
            
'04' => -3.2,
            
'05' => -3,
            
'06' => -4,
            
'07' => -2.2,
            
'08' => -4,
            
'09' => -1.2,
            
'10' => -1
        
);
    
?>
All negative values

Example 3: All null values

<?php
        
//Data array with all nulls
        
$data = array(
            
'00' => 0,
            
'01' => 0,
            
'02' => 0,
            
'03' => 0,
            
'04' => 0,
            
'05' => 0,
            
'06' => 0,
            
'07' => 0,
            
'08' => 0,
            
'09' => 0,
            
'10' => 0
        
);
    
?>
All negative values

5.0.3 Update

Jack Finch provided some very usefull text alignment functionality.
You are now able to right align your key's and labels by setting the new configuration directives (key-right-align, label-right-align) to true.
Next to this Jack also provided a round range functionality. (You can set this via the new configuration directive "round-value-range"

5.0.2 Update

Some translation issues regarding the configuration of phpMyGraph where pointed out to me by Tekin Ozbek.
avarage -> average
devider -> divider
I've corrected them in version 5.0.2. (Thank you Tekin)

What's left for the next version

WARNING NEW WAY OFF CALLING phpMyGraph

<?php

//Old (Backwards compatibilty)
    //Create phpMyGraph instance
    
$graph = new phpMyGraph();

    
//Parse
    
$graph->parseHorizontalLineGraph($data$cfg);

//Via class
    //Create instance
    
$graph = new horizontalLineGraph();

    
//Parse
    
$graph->parse($data$cfg);

//Via factory
    //Create instance via factory
    
$graph phpMyGraph::factory('horizontalLineGraph');

    
//Parse
    
$graph->parse($data$cfg);

?>

Examples

Horizontal line graph

class: horizontalLineGraph

<?php    
    
//Set content-type header
    
header("Content-type: image/png");

    
//Include phpMyGraph5.0.php
    
include_once('phpMyGraph5.0.php');
    
    
//Set config directives
    
$cfg['title'] = 'Example graph';
    
$cfg['width'] = 500;
    
$cfg['height'] = 250;
    
    
//Set data
    
$data = array(
        
'Jan' => 12,
        
'Feb' => 25,
        
'Mar' => 0,
        
'Apr' => 7,
        
'May' => 80,
        
'Jun' => 67,
        
'Jul' => 45,
        
'Aug' => 66,
        
'Sep' => 23,
        
'Oct' => 23,
        
'Nov' => 78,
        
'Dec' => 23
    
);
    
    
//Create phpMyGraph instance
    
$graph = new phpMyGraph();

    
//Parse
    
$graph->parseHorizontalLineGraph($data$cfg);
?>

The code above will output

Horizontal line graph

Simple horizontal column graph

class: horizontalSimpleColumnGraph

<?php    
    
//Set content-type header
    
header("Content-type: image/png");

    
//Include phpMyGraph5.0.php
    
include_once('phpMyGraph5.0.php');
    
    
//Set config directives
    
$cfg['title'] = 'Example graph';
    
$cfg['width'] = 500;
    
$cfg['height'] = 250;
    
    
//Set data
    
$data = array(
        
'Jan' => 12,
        
'Feb' => 25,
        
'Mar' => 0,
        
'Apr' => 7,
        
'May' => 80,
        
'Jun' => 67,
        
'Jul' => 45,
        
'Aug' => 66,
        
'Sep' => 23,
        
'Oct' => 23,
        
'Nov' => 78,
        
'Dec' => 23
    
);
    
    
//Create phpMyGraph instance
    
$graph = new phpMyGraph();

    
//Parse
    
$graph->parseHorizontalSimpleColumnGraph($data$cfg);
?>

The code above will output

Simple horizontal column graph

Horizontal shadow column graph

class: horizontalColumnGraph

<?php    
    
//Set content-type header
    
header("Content-type: image/png");

    
//Include phpMyGraph5.0.php
    
include_once('phpMyGraph5.0.php');
    
    
//Set config directives
    
$cfg['title'] = 'Example graph';
    
$cfg['width'] = 500;
    
$cfg['height'] = 250;
    
    
//Set data
    
$data = array(
        
'Jan' => 12,
        
'Feb' => 25,
        
'Mar' => 0,
        
'Apr' => 7,
        
'May' => 80,
        
'Jun' => 67,
        
'Jul' => 45,
        
'Aug' => 66,
        
'Sep' => 23,
        
'Oct' => 23,
        
'Nov' => 78,
        
'Dec' => 23
    
);
    
    
//Create phpMyGraph instance
    
$graph = new phpMyGraph();

    
//Parse
    
$graph->parseHorizontalColumnGraph($data$cfg);
?>

The code above will output

Horizontal shadow column graph

Horizontal polygon graph

class: horizontalPolygonGraph

<?php    
    
//Set content-type header
    
header("Content-type: image/png");

    
//Include phpMyGraph5.0.php
    
include_once('phpMyGraph5.0.php');
    
    
//Set config directives
    
$cfg['title'] = 'Example graph';
    
$cfg['width'] = 500;
    
$cfg['height'] = 250;
    
    
//Set data
    
$data = array(
        
'Jan' => 12,
        
'Feb' => 25,
        
'Mar' => 0,
        
'Apr' => 7,
        
'May' => 80,
        
'Jun' => 67,
        
'Jul' => 45,
        
'Aug' => 66,
        
'Sep' => 23,
        
'Oct' => 23,
        
'Nov' => 78,
        
'Dec' => 23
    
);
    
    
//Create phpMyGraph instance
    
$graph = new phpMyGraph();

    
//Parse
    
$graph->parseHorizontalPolygonGraph($data$cfg);
?>

The code above will output

Horizontal polygon graph

Vertical line graph

class: verticalLineGraph

<?php    
    
//Set content-type header
    
header("Content-type: image/png");

    
//Include phpMyGraph5.0.php
    
include_once('phpMyGraph5.0.php');
    
    
//Set config directives
    
$cfg['title'] = 'Example graph';
    
$cfg['width'] = 500;
    
$cfg['height'] = 250;
    
    
//Set data
    
$data = array(
        
'Jan' => 12,
        
'Feb' => 25,
        
'Mar' => 0,
        
'Apr' => 7,
        
'May' => 80,
        
'Jun' => 67,
        
'Jul' => 45,
        
'Aug' => 66,
        
'Sep' => 23,
        
'Oct' => 23,
        
'Nov' => 78,
        
'Dec' => 23
    
);
    
    
//Create phpMyGraph instance
    
$graph = new phpMyGraph();

    
//Parse
    
$graph->parseVerticalLineGraph($data$cfg);
?>

The code above will output

Vertical line graph

Simple vertical column graph

class: verticalSimpleColumnGraph

<?php    
    
//Set content-type header
    
header("Content-type: image/png");

    
//Include phpMyGraph5.0.php
    
include_once('phpMyGraph5.0.php');
    
    
//Set config directives
    
$cfg['title'] = 'Example graph';
    
$cfg['width'] = 500;
    
$cfg['height'] = 250;
    
    
//Set data
    
$data = array(
        
'Jan' => 12,
        
'Feb' => 25,
        
'Mar' => 0,
        
'Apr' => 7,
        
'May' => 80,
        
'Jun' => 67,
        
'Jul' => 45,
        
'Aug' => 66,
        
'Sep' => 23,
        
'Oct' => 23,
        
'Nov' => 78,
        
'Dec' => 23
    
);
    
    
//Create phpMyGraph instance
    
$graph = new phpMyGraph();

    
//Parse
    
$graph->parseVerticalSimpleColumnGraph($data$cfg);
?>

The code above will output

Simple vertical column graph

Vertical shadow column graph

class: verticalColumnGraph

<?php    
    
//Set content-type header
    
header("Content-type: image/png");

    
//Include phpMyGraph5.0.php
    
include_once('phpMyGraph5.0.php');
    
    
//Set config directives
    
$cfg['title'] = 'Example graph';
    
$cfg['width'] = 500;
    
$cfg['height'] = 250;
    
    
//Set data
    
$data = array(
        
'Jan' => 12,
        
'Feb' => 25,
        
'Mar' => 0,
        
'Apr' => 7,
        
'May' => 80,
        
'Jun' => 67,
        
'Jul' => 45,
        
'Aug' => 66,
        
'Sep' => 23,
        
'Oct' => 23,
        
'Nov' => 78,
        
'Dec' => 23
    
);
    
    
//Create phpMyGraph instance
    
$graph = new phpMyGraph();

    
//Parse
    
$graph->parseVerticalColumnGraph($data$cfg);
?>

The code above will output

Vertical shadow column graph

Vertical polygon graph

class: verticalPolygonGraph

<?php    
    
//Set content-type header
    
header("Content-type: image/png");

    
//Include phpMyGraph5.0.php
    
include_once('phpMyGraph5.0.php');
    
    
//Set config directives
    
$cfg['title'] = 'Example graph';
    
$cfg['width'] = 500;
    
$cfg['height'] = 250;
    
    
//Set data
    
$data = array(
        
'Jan' => 12,
        
'Feb' => 25,
        
'Mar' => 0,
        
'Apr' => 7,
        
'May' => 80,
        
'Jun' => 67,
        
'Jul' => 45,
        
'Aug' => 66,
        
'Sep' => 23,
        
'Oct' => 23,
        
'Nov' => 78,
        
'Dec' => 23
    
);
    
    
//Create phpMyGraph instance
    
$graph = new phpMyGraph();

    
//Parse
    
$graph->parseVerticalPolygonGraph($data$cfg);
?>

The code above will output

Vertical polygon graph

Compare graph

class: verticalLineGraph (You can also use the other types)

<?php    
    
//Include phpMyGraph5.0.php
    
include_once('phpMyGraph5.0.php');
    
    
//Set config directives
    
$cfg['title'] = 'Example graph';
    
$cfg['width'] = 500;
    
$cfg['height'] = 250;
    
    
//Set data 1
    
$data1 = array(
        
'00' => 10,
        
'01' => 20,
        
'02' => 10,
        
'03' => 40,
        
'04' => 50,
        
'05' => 30,
        
'06' => 20,
        
'07' => 50,
        
'08' => 30,
        
'09' => 20,
        
'10' => 10
    
);

    
//Set data 2
    
$data2 = array(
        
'00' => 5,
        
'01' => 8,
        
'02' => 19,
        
'03' => 43,
        
'04' => 56,
        
'05' => 10,
        
'06' => 18,
        
'07' => 47,
        
'08' => 22,
        
'09' => 11,
        
'10' => 8
    
);
    
    
//Create phpMyGraph instance
    
$graph = new verticalLineGraph();

    
//Parse
    
$graph->parseCompare($data1$data2$cfg);
?> 

The code above will output

Compare

Configuration Directives

All directives

Directive Type Default value Comment
type arrayValue png Type for image output (png, jpg, gif)
width numeric 500 Width for image
height numeric 200 Height for image
background-color color FFFFFF Background color
background-image file Background image
title text Title for graph
title-visible boolean 1 Set title visibility
title-font-size arrayValue 2 Title font size (1, 2, 3, 4, 5, 6)
title-color color 000000 Title color (Use HEX)
zero-line-visible boolean 1 Set zero line visibility
zero-line-color color 000000 Zero line color
zero-line-alpha numeric 0 Zero line alpha
average-line-visible boolean 1 Set average line visibility
average-line-color color 0000FF Average line color (Use HEX)
average-line-alpha numeric 0 Average line alpha
key-color color 006699 Key color (Use HEX)
key-visible boolean 1 Set key visibility
key-font-size arrayValue 2 Set key font size (1, 2, 3, 4, 5, 6)
key-right-align boolean Right Align Keys
label text Label text
label-color color 000000 Label color (Use HEX)
label-visible boolean 1 Set label visibility
label-font-size arrayValue 2 Label font size (1, 2, 3, 4, 5, 6)
label-right-align boolean Right Align Labels
value-visible boolean 1 Set value visibility
value-font-size arrayValue 2 Value font size (1, 2, 3, 4, 5, 6)
value-color color 000000 Value color (Use HEX)
value-label-visible boolean 1 Set value label visibility
value-label-font-size arrayValue 2 Value label font size
value-label-color color 006699 Value label color (Use HEX)
box-border-color color 006699 Box border color (Use HEX)
box-border-alpha numeric 0 Box border alpha
box-border-visible boolean 1 Set box border visibility
box-background-color color F1F1F1 Box background color (Use HEX)
box-background-visible boolean 1 Box background visibility
box-background-alpha numeric 0 Box background alpha
column-divider-color color 000000 Column divider color (Use HEX)
column-divider-alpha numeric 100 Column divider alpha
column-divider-visible boolean 1 Set column divider visibility
horizontal-divider-color color 000000 Horizontal divider color (Use HEX)
horizontal-divider-alpha numeric 100 Horizontal divider alpha
horizontal-divider-visible boolean 1 Set horizontal divider visiblity
column-color-random boolean Create random column color
column-color color 0099CC Column color (Use HEX)
column-alpha numeric 0 Column alpha
column-shadow-alpha numeric 0 Column shadow alpha
column-shadow-color color 006699 Column shadow color (Use HEX)
column-compare-color color FF0000 Compare column color (Use HEX)
column-compare-shadow-color color FF0000 Compare column shadow color (Use HEX)
file-name text Filename to parse to
jpg-quality numeric 60 JPG output quality
round-value-range boolean Round Range Values