PHP Graphing LibraryPHPGraphLib

Lightweight PHP Graphing Library

Documentation Table of Contents - PHPGraphLib
Function Reference - PHPGraphLib

Below is the default form of the constructor:

Constructor & Example Usage Description

PHPGraphLib( int $width, int $height )

  • $graph=new PHPGraphLib(600,400);
PHPGraphLib object constructor. $width and $height arguments are optional. Default width=400, height=300. Recommended width is at least 200 and recommended height is at least 225.

The two other main, required functions are below:

Main Functions & Example Usage Description

addData( array $data )

  • $graph->addData(array(100,12,43,342,9));
Adds data to the graph. The array values will be the y-axis value. If the array has keys, the keys will be the corresponding x-axis values.

createGraph()

  • $graph->createGraph();
Creates and outputs the graph. Must be called last.

All other graph customization functions are below:

Axis & Data Functions Description

setupXAxis( int $percent, string $color )

  • $graph->setupXAxis(15, "red");
  • $graph->setupXAxis(5, "255,255,255");
  • $graph->setupXAxis(false);
  • $graph->setupXAxis("", "blue");
  • $graph->setupXAxis(8);
Sets the x-axis margin as a percentage of the height and the color or the x-axis. By default, the x-axis margin is 12% of the width and black. Color may be specified as one of the supported colors or as an RGB value comma separated string. You may also call the setupXAxis() function with only a false argument to hide the x-axis. You can also call it with just the first percent argument, or leave the percent argument as an empty string and call it with the color argument.

setupYAxis( int $percent, string $color )

  • $graph->setupYAxis(10, "blue");
  • $graph->setupYAxis(20, "128,128,0");
  • $graph->setupYAxis(false);
  • $graph->setupYAxis("", "purple");
  • $graph->setupYAxis(15);
Sets the y-axis margin as a percentage of the height and the color or the y-axis. By default, the y-axis margin is 8% of the width and black. Color may be specified as one of the supported colors or as an RGB value comma separated string. You may also call the setupYAxis() function with only a false argument to hide the y-axis. You can also call it with just the first percent argument, or leave the percent argument as an empty string and call it with the color argument.

setRange( int $max, int $min ) - New in PHPGraphLib 1.14

  • $graph->setRange(130,80);
  • $graph->setRange(100000,2000);

Sets the range of values to be displayed on the y-axis. This overrides the default automatic y-axis range and values that get shown on the graph. Examples of setRange() are shown below:

Graph without setRange() Graph with setRange() function

Default Range Handling

With setRange(130,80) function.

Note that setRange() has some limitations:

  • Only non-negative ranges may be set.
  • All data must be within the set range.
  • The $max/$min variables are approximate. Sometimes PHPGraphLib may slightly alter these values when faced with non-rounded values.

setXValues( boolean $x_values )

  • $graph->setXValues(false);
Display values on the x-axis. Accepts either true or false arguments. Default value is true, the x-axis values are displayed.

setYValues( boolean $y_values )

  • $graph->setYValues(false);
Display values on the y-axis. Accepts either true or false arguments. Default value is true, the y-axis values are displayed.

setXValuesHorizontal( boolean $horizontal )- New in PHPGraphLib 1.17

  • $graph->setXValuesHorizontal(true);
  • $graph->setXValuesHorizontal(false);

Display values on the x-axis orientated horizontally. Accepts either true or false arguments. Default value is false, the y-axis values are vertically oriented.

Horizontal X-Axis Values

Horizontal X-Axis Values

setXValuesVertical( boolean $vertical )- New in PHPGraphLib 1.17

  • $graph->setXValuesVertical(true);
  • $graph->setXValuesVertical(false);
Display values on the x-axis orientated vertically. Accepts either true or false arguments. Default value is true, the y-axis values are vertically oriented.
Vertical X-Axis Values

Vertical X-Axis Values

setTextColor( string $color )

  • $graph->setTextColor("maroon");
Sets the text color used to display values on the both axis. Color may be specified as one of the supported colors or as an RGB value comma separated string. Default text color is a dark gray.

setXAxisTextColor( string $color )

  • $graph->setXAxisTextColor("128,0,0");
Sets the text color used to display values on just the x-axis. Color may be specified as one of the supported colors or as an RGB value comma separated string. Default color is a dark gray.

setYAxisTextColor( string $color )

  • $graph->setYAxisTextColor("silver");
Sets the text color used to display values on just the y-Axis. Color may be specified as one of the supported colors or as an RGB value comma separated string. Default color is a dark gray.
Title Functions Description

setTitle( string $title )

  • $graph->setTitle("Performance July 2007");
Sets and displays a title for the graph.

setTitleColor( string $color )

  • $graph->setTitleColor("maroon");
  • $graph->setTitleColor("0,255,0");
Sets the title color. Color may be specified as one of the supported colors or as an RGB value comma separated string. Default title color is black.

setTitleLocation( string $location )

  • $graph->setTitleColor("left");
  • $graph->setTitleColor("right");
  • $graph->setTitleColor("center");
Sets the location of the title across the top of the graph. Valid values are "left", "right", "center". Default title location is the center.
Bar Functions Description

setBars( boolean $bars )

  • $graph->setBars(false);
Display bars on the graph - i.e. displays a bar graph. Accepts either true or false arguments. Default value is true, the bars are displayed.

setBarColor( string $color, [string $color2], [string $color3])

  • $graph->setBarColor("white");
  • $graph->setBarColor("123,65,12");

Multiple Datasets

  • $graph->setBarColor("white", "gray", "green");
  • $graph->setBarColor("123,65,12", "100,100,100");

Sets the color of the bars on the graph. Color may be specified as one of the supported colors or as an RGB value comma separated string. Default bar color is gray. PHPGraphLib will ignore this function if the setGradient() function is set.

If using multiple datasets, you may specify up to two additional colors. If you are using multiple datasets, but have not specifed more than one color, additional colors will be specified automatically.

setBarOutline( boolean $outline )

  • $graph->setBarOutline(false);
Display an outline around each bar on the graph. Accepts either true or false arguments. Default value is true, the outline is displayed.

setBarOutlineColor( string $color )

  • $graph->setBarOutlineColor("navy");
Sets the color of the outline of each bar on the graph. Color may be specified as one of the supported colors or as an RGB value comma separated string. Default bar outline color is black.

setGradient( string $color1, string $color2, [string $color3], [string $color4], [string $color5], [string $color6])

  • setGradient("red", "maroon")
  • setGradient("200,200,200", "100,100,100")

Multiple Datasets:

  • setGradient("red", "maroon", "yellow", "olive" )
  • setGradient("200,200,200", "100,100,100", "teal", "aqua", "red", "maroon")

Sets the colors for and enables the bars of the graph to be displayed as a gradient between two colors. Color arguments may be specified as one of the supported colors or as an RGB value comma separated string. By default, the gradient feature is not enabled.

If using multiple datasets, up to two more pairs of gradient colors may be specified. If you are using multiple datasets, but have not specifed more than one gradient color, additional gradient colors will be specified automatically.

Grid Functions Description

setGrid( boolean $grid )

  • $graph->setGrid(false);
Display the grid. Accepts either true or false arguments. Default value is true, the grid is displayed.

setGridColor( string $color )

  • $graph->setGridColor("black");
  • $graph->setGridColor("0,127,0");
Display the grid. Accepts either true or false arguments. Default value is true, the grid is displayed.
Graph Line Functions Description

setLine( boolean $line )

  • $graph->setLine(true);
Display the graph line. Accepts either true or false arguments. Default value is false, the graph line is not displayed.

setLineColor( string $color, [string $color2], [$string $color3])

  • $graph->setLineColor("navy");
  • $graph->setLineColor("255,255,0");

Multiple Datasets:

  • $graph->setLineColor("navy", "red","blue");
  • $graph->setLineColor("230,23,100", "red");

Sets the line color of the graph lines, if enabled, on the graph. Color may be specified as one of the supported colors or as an RGB value comma separated string. Default line color is a dark gray.

If multiple datasets are used, you can specify up to two additional line colors. If graph bars are disabled, and there are multiple datasets, but only one line color is specified, additional line colors will be generated automatically.

Data Point Functions Description

setDataPoints( boolean $points )

  • $graph->setDataPoints(true);
Display a circular data point on the graph for each value. Accepts either true or false arguments. Default value is false, data points are not displayed.

setDataPointColor( string $color )

  • $graph->setBarOutlineColor("navy");
  • $graph->setBarOutlineColor("255,255,0");
Sets the color of the data point circle, if enabled, on the graph. Color may be specified as one of the supported colors or as an RGB value comma separated string. Default data point color is black.

setDataPointSize( int $size ) - New in PHPGraphLib 1.13

  • $graph->setDataPointSize(4);
Sets the diameter of the data point circle, if enabled on the graph. Default size is 6 pixels.
Data Value Functions Description

setDataValues( boolean $values )

  • $graph->setDataValues(true);

Display each numeric data value, as passed in the data value array, on the graph. Accepts either true or false arguments. Default value is false, data values are not displayed.

Graph with data values

setDataValueColor( string $color )

  • $graph->setBarOutlineColor("navy");
  • $graph->setBarOutlineColor("255,255,0");
Sets the color of the data value display, if enabled, on the graph. Color may be specified as one of the supported colors or as an RGB value comma separated string. Default data value color is a dark gray.
Goal Line Functions Description

setGoalLine( string $goalValue )

  • $graph->setGoalLine(90);
  • $graph->setGoalLine(.023);

Displays a singular horizontal grid line in addition to the regular grid lines (if present). Can be used to display a goal or y value needing emphasis. Value must be within bounds of the maximum and minimum y-axis values. The setGoalLine() function can be called multiple times to add multiple goal lines, if needed.

Graph with goal line

setGoalLineColor( string $color )

  • $graph->setGoalLineColor("red");
  • $graph->setGoalLineColor("255,0,0");
Sets the color of the goal line, if present. Color may be specified as one of the supported colors or as an RGB value comma separated string. Default goal line color is black.
Legend Functions Description

setLegend( boolean $legend )

  • $graph->setLegend(true);
  • $graph->setLegend(false);
Display a legend with color swatches matching the corresponding colors on the graph. Accepts either true or false arguments. Default value is false, the legend is not displayed.

setLegendTitle( string $title, [string $title2], [string $title3])

  • $graph->setLegendTitle("Apples");

Multiple Datasets

  • $graph->setLegendTitle("Apples", "Pears");
  • $graph->setLegendTitle("Apples", "Pears", "Oranges" )
Sets the title of the dataset on the legend. If using more than one dataset, specify titles in the order of datasets specified in the addData() function.

setLegendColor( string $color)

  • $graph->setLegendColor("red");
  • $graph->setLegendColor("120,60,30");

Sets the background color of the legend, if present. Color may be specified as one of the supported colors or as an RGB value comma separated string. Default legend background color is white.

Legend Examples

setLegendTextColor( string $color)

  • $graph->setLegendTextColor("olive");
  • $graph->setLegendTextColor("101,45,12");
Sets text color in the legend. Color may be specified as one of the supported colors or as an RGB value comma separated string. Default color is dark gray.

setLegendOutlineColor( string $color)

  • $graph->setLegendOutlineColor("yellow");
  • $graph->setLegendOutlineColor("255,255,0");
Sets border color of the legend. Color may be specified as one of the supported colors or as an RGB value comma separated string. Default color is light gray.

setSwatchOutlineColor( string $color)

  • $graph->setSwatchOutlineColor("teal");
  • $graph->setSwatchOutlineColor("0,122,255");
Sets the border color of the color swatches in the legend. Color may be specified as one of the supported colors or as an RGB value comma separated string. Default color is a dark gray.
Background Functions Description

setBackgroundColor( string $color )

  • $graph->setBackgroundColor("black");
  • $graph->setBackgroundColor("0,0,255");
Sets the background color for the graph. Color may be specified as one of the supported colors or as an RGB value comma separated string. There is no need to enable the background by calling a boolean function - setting a color for it enables the background. Default background color is white.

BulletSupported Colors

The following is a list of colors supported in name by PHPGraphLib. The colors are from the W3C's list of 16 W3C Standard Colors.

Color Name Swatch RGB Value Color Name Swatch RGB Value
black black 0,0,0 green green 0,128,0
silver silver 192,192,192 lime lime 0,255,0
gray gray 128,128,128 olive olive 128,128,0
white white 255,255,255 yellow yellow 255,255,0
maroon maroon 128,0,0 navy navy 0,0,128
red red 255,0,0 blue blue 0,0,255
purple purple 128,0,128 teal teal 0,128,128
fuscia fuscia 255,0,255 aqua aqua 0,255,255