PHPGraphLib Function Reference

Below is the default form of the constructor:

Constructor

PHPGraphLib ( int $width, int $height, [string $filename] )
required
  • $graph = new PHPGraphLib(600,400);
  • $graph = new PHPGraphLib(600,400, "image.png");

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.

If you specify the optional filename argument, PHPGraphLib will not display to the screen, but instead create a .png file in the same directory as the script where PHPGraphLib is being called.

The two other main, required functions are below:

Required Functions

addData( $data )
required
  • $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 ( )
required
  • $graph->createGraph();

Creates and outputs the graph. Must be called last.

All other graph customization functions are below:

Axis & Data Functions

setupXAxis ( int $percent, string $color )
  • $graph->setupXAxis(15);
  • $graph->setupXAxis(10, "blue");
  • $graph->setupXAxis(20, "128,128,0");
  • $graph->setupXAxis("", "purple");
  • $graph->setupXAxis(false);

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 8% of the width and black. Color may be specified as one of the supported colors, an RGB value comma separated string, or a hex value. 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 argument, or leave the percent argument as an empty string and call it with the color argument.

setupYAxis ( int $percent, string $color )
  • $graph->setupYAxis(15);
  • $graph->setupYAxis(10, "blue");
  • $graph->setupYAxis(20, "128,128,0");
  • $graph->setupYAxis("", "purple");
  • $graph->setupYAxis(false);

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, an RGB value comma separated string, or a hex value. 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 argument, or leave the percent argument as an empty string and call it with the color argument.

setRange ( int $max, int $min )
new in 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 - 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.

setXValuesInterval ( integer $interval)
new in GitHub Commit Jun 19, 2013
  • $graph->setXValuesInterval(1);
  • $graph->setXValuesInterval(3);

Skip every $interval display on the x-axis. This makes it easier to see x-axis labels on charts with many datapoints. If the value of $interval is 1, labels will be displayed every other data point. If the value of $interval is 3, labels will be shown every third datapoint. Etc.

setXValuesHorizontal ( boolean $vertical )
new in 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 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");
  • $graph->setTextColor("#006600");

Sets the text color used to display values on the both axis. Color may be specified as one of the supported colors, an RGB value comma separated string, or a hex value. Default text color is a dark gray.

setXAxisTextColor ( string $color )
  • $graph->setXAxisTextColor("128,0,0");
  • $graph->setXAxisTextColor("#333333");

Sets the text color used to display values on just the x-axis. Color may be specified as one of the supported colors, an RGB value comma separated string, or a hex value. 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, an RGB value comma separated string, or a hex value. Default color is a dark gray.

 

Title Functions

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");
  • $graph->setTitleColor("#660000");

Sets the title color. Color may be specified as one of the supported colors, an RGB value comma separated string, or a hex value. 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

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");
  • $graph->setBarColor("#660000");
  • //Multiple Datasets
  • $graph->setBarColor("123,65,12", "100,100,100");
  • $graph->setBarColor("#333333", "#888888");
  • $graph->setBarColor("white", "gray", "green");

Sets the color of the bars on the graph. Color may be specified as one of the supported colors, an RGB value comma separated string, or a hex value. 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, an RGB value comma separated string, or a hex value. Default bar outline color is black.

setGradient ( string $color1, string $color2, [string $color3], [string $color4], [string $color5], [string $color6] )
  • $graph->setGradient("red", "maroon");
  • $graph->setGradient("200,200,200", "100,100,100");
  • $graph->setGradient("#990000", "#CC0000");
  • //Multiple Datasets
  • $graph->setGradient("red", "maroon", "yellow", "olive" );
  • $graph->setGradient("3,33,33", "8,100,3", "teal", "aqua", "red", "blue");

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

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");
  • $graph->setGridColor("#0000AA");

Display the grid. Accepts either true or false arguments. Default value is true, the grid is displayed.

 

Line Functions

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");
  • $graph->setLineColor("#339900");
  • //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, an RGB value comma separated string, or a hex value. 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

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, an RGB value comma separated string, or a hex value. Default data point color is black.

setDataPointSize ( int $size )
new in 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

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, an RGB value comma separated string, or a hex value. Default data value color is a dark gray.

setDataFormat ( string $format )
  • $graph->setDataFormat("percent");
  • $graph->setDataFormat("degrees");
  • $graph->setDataFormat("comma");
  • $graph->setDataFormat("XXX");

Formats data points on both the y axis and the actual graph (if setDataPoints(true); is set). Current supported formats include "percent" which appends a % to the end of each value, "degrees" which appends a ° to the end of each value, and "comma" which groups values with commas (3,232,109). You can call as many setDataFormat() functions as you want.

If the format argument is not in the above list of supported formats, the value specified will be appended to the end of the data value displayed. For example if you have a data value of 23 and you call $graph->setDataFormat(" Apples"), the data value will be displayed as: 23 Apples.

setDataCurrency ( string $currency )
  • $graph->setDataCurrency("dollar");
  • $graph->setDataCurrency("yen");
  • $graph->setDataCurrency("pound");
  • $graph->setDataCurrency("lira");
  • $graph->setDataCurrency("XXX");

Formats data points on both the y axis and the actual graph (if setDataPoints(true); is set) by adding currency symbols in front of the data value displayed. Currently the following formats are supported:

dollar $
pound £
yen ¥
lira £

If the currency argument is not in the above list, it will be appended to the front of the data value. For example if you have a data value of 2300 and you call $graph->setDataCurrency("*^* "), the data value will be displayed as: *^* 2300.

 

Goal Line Functions

setGoalLine ( string $goalValue, [string $color], [string $style] )
modified in 2.31
  • $graph->setGoalLine(90);
  • $graph->setGoalLine(14, "red", "solid");
  • $graph->setGoalLine(.023, "#FF0000", "dashed");

Displays a singular horizontal line to display a goal or y value needing emphasis. $color and $style arguments are optional. The default color is black if not specified and the default style is solid. Current possible values of the $style argument are "solid" and "dashed". Goal 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 )
deprecated
  • $graph->setGoalLineColor("red");
  • $graph->setGoalLineColor("255,0,0");

This function is deprecated in favor of specifying a color in the setGoalLine() function. Please use setGoalLine() in new implementations.

Sets the color of the goal line, if present. Color may be specified as one of the supported colors, an RGB value comma separated string, or a hex value. Default goal line color is black.

 

Legend Functions

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");
  • $graph->setLegendColor("#660000");

Sets the background color of the legend, if present. Color may be specified as one of the supported colors, an RGB value comma separated string, or a hex value. 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, an RGB value comma separated string, or a hex value. 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, an RGB value comma separated string, or a hex value. 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, an RGB value comma separated string, or a hex value. Default color is a dark gray.

 

Background Functions

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, an RGB value comma separated string, or a hex value. 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.

Supported 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