SVGGraph 3.0 - a PHP SVG graph library

Skip to:

SVGGraph is an object-oriented PHP library for creating simple PHP graphs, released as open source under the LGPL v3 license. Here are some examples of different types of graph supported by SVGGraph.

DoughRayMeSoFarLard01020304050
05101520253035404550DoughRayMeSoFarLard
DoughRayMeSoFarLard01020304050
Dough$30Ray$50Me$40So$25Far$45Lard$35
DoughRayMeSoFarLard01020304050
0100200300400500600-20020406080

Requirements

PHP 5.4

SVGGraph 3.0 requires a minimum PHP version of 5.4. If you must use a PHP version lower than this, stick with a SVGGraph 2.x version. The SVGGraph 1.x branch supported PHP 4, but hopefully nobody is still using that.

JSON

JSON is a standard, bundled PHP extension that SVGGraph uses for parsing its font metrics files.

iconv and mbstring

These are character encoding PHP extensions used by SVGGraph to convert between character sets. SVGGraph will work without them, but could have trouble measuring text in non-western formats.

PCRE

PCRE (the preg_* functions) is a core PHP extension and you shouldn't be able to turn it off, but I'm listing it here for the sake of completeness.

Getting SVGGraph

There are several ways you can get hold of SVGGraph:

1. Download from this site:

2. Clone, download or browse the repository on GitHub:

3. Use Composer and Packagist to download and install SVGGraph from GitHub:

Older versions of SVGGraph are available from the downloads page if you cannot use the latest version for any reason.

Example graph code

Here is a basic example:

<?php
// SVGGraph 3 uses an autoloader and namespace
require 'svggraph/autoloader.php';
$graph = new Goat1000\SVGGraph\SVGGraph(640, 480);

$graph->colours(['red','green','blue']);
$graph->values(100, 200, 150);
$graph->links('/Tom/', '/Dick/', '/Harry/');
$graph->render('BarGraph');

This will create a SVG file containing a 640x480 bar graph with three hyperlinked bars.

Graph types

At the moment these types of graph are supported by SVGGraph - the class name of the type of graph you want to generate should be passed to the $graph->render() or $graph->fetch() function.

Bar graphs

Class name Description Example
BarGraph Vertical bars, optionally hyperlinked. BarGraph
StackedBarGraph Vertical bars, with multiple datasets stacked. StackedBarGraph
GroupedBarGraph Vertical bars, with multiple datasets grouped side-by-side. GroupedBarGraph
StackedGroupedBarGraph Vertical bars, with both stacked and grouped datasets. StackedGroupedBarGraph
BarAndLineGraph Grouped bar graph with multi-line graph. BarAndLineGraph
StackedBarAndLineGraph Stacked bar graph with multi-line graph. StackedBarAndLineGraph
Histogram A series of bars that represent the spread of values. Histogram

For more details and examples, see the bar graph page.

3D Bar graphs

Class name Description Example
Bar3DGraph A 3D-looking version of the BarGraph type. Bar3DGraph
StackedBar3DGraph 3D vertical bars, with multiple datasets stacked. StackedBar3DGraph
GroupedBar3DGraph 3D vertical bars, with multiple datasets grouped side-by-side. GroupedBar3DGraph
StackedGroupedBar3DGraph 3D vertical bars, with both stacked and grouped datasets. StackedGroupedBar3DGraph
CylinderGraph Vertical 3D cylinder-shaped bars. CylinderGraph
StackedCylinderGraph Cylinder-shaped version of stacked bar graph. StackedCylinderGraph
GroupedCylinderGraph Cylinder-shaped version of grouped bar graph. GroupedCylinderGraph
StackedGroupedCylinderGraph Cylinder-shaped version of stacked and grouped bar graph. StackedGroupedCylinderGraph

For more details and examples, see the 3D bar graph page.

Horizontal bar graphs

Class name Description Example
HorizontalBarGraph Horizontal bars, optionally hyperlinked. HorizontalBarGraph
HorizontalStackedBarGraph Horizontal bars, with multiple datasets in-line. HorizontalStackedBarGraph
HorizontalGroupedBarGraph Horizontal bars, with multiple datasets grouped vertically. HorizontalGroupedBarGraph
PopulationPyramid A horizontal bar graph with the bars drawn from a central axis, used to show male and female populations and ages. PopulationPyramid

For more details and examples, see the horizontal bar graph page.

Line graphs

Class name Description Example
LineGraph A line joining the data points, with optionally hyperlinked markers at the data points. LineGraph
MultiLineGraph Multiple lines joining the data points in each data set, with optionally hyperlinked markers at the data points. MultiLineGraph
StackedLineGraph Multiple lines joining data points, with the values from different data sets added together. StackedLineGraph

For more details and examples, see the line graph page.

Scatter graphs

Class name Description Example
ScatterGraph Markers drawn at arbitrary horizontal and vertical points. ScatterGraph
MultiScatterGraph Markers from multiple data sets drawn at arbitrary horizontal and vertical points. MultiScatterGraph

For more details and examples, see the scatter graph page.

Radar graphs

Class name Description Example
RadarGraph A line joining the data points, measured from the centre point, with optionally hyperlinked markers at the data points. RadarGraph
MultiRadarGraph Multiple lines joining the data points in each data set, measured from the centre point, with optionally hyperlinked markers at the data points. MultiRadarGraph

For more details and examples, see the radar graph page.

Pie graphs

Class name Description Example
PieGraph A pie chart, with optionally hyperlinked slices and option to fade labels in/out when the pointer enters/leaves a slice. PieGraph
Pie3DGraph A 3D-looking version of the PieGraph type. Pie3DGraph
DonutGraph A pie chart, with a hole in the centre where text can be displayed. DonutGraph
SemiDonutGraph A donut graph that only uses half of the circle. SemiDonutGraph
PolarAreaGraph A pie chart where the area of the slice indicates the value instead of the slice angle. PolarAreaGraph
ExplodedPieGraph A pie chart with slices pushed out from the centre point. ExplodedPieGraph
PolarArea3DGraph A 3D version of the PolarAreaGraph. PolarArea3DGraph
ExplodedPie3DGraph A 3D version of the ExplodedPieGraph. ExplodedPie3DGraph

For more details and examples, see the pie graph page.

Other miscellaneous graphs

Class name Description
FloatingBarGraph A bar graph where the top and bottom of each bar use separate data points.
HorizontalFloatingBarGraph A horizontal version of the FloatingBarGraph.
BubbleGraph A scatter graph where the markers are replaced by filled circles, their areas coming from another data point.
BoxAndWhiskerGraph A bar-like graph for displaying statistical distributions.
EmptyGraph An empty SVG document that supports the label and shape options.

For more details and examples, see the other graphs page.

Help!

If you have trouble figuring out how to get your graph working from the details on these pages, or if you find any bugs or have suggestions for improvements, please contact me at the usual address, graham(at)goat1000.com.

Thanks to Pierre-Sylvain Luquet for his code submissions.

« Back to top of page Using SVGGraph »