SVGGraph function reference
Skip to:
SVGGraph only has these few functions because the appearance of the graph is
mainly configured using the $settings
array passed to the SVGGraph
class constructor.
The functions are listed on this page in the order you would be expected to call them in a PHP script.
SVGGraph constructor
From SVGGraph 3.0 you must use the full namespace to create the SVGGraph class instance.
$graph = new Goat1000\SVGGraph\SVGGraph($width, $height, $settings /* = null */);
$width
- width of graph in pixels$height
- height of graph in pixels$settings
- array of settings to configure graph (optional)
Creates an instance of the main SVGGraph class, setting the width and height
of the SVG image. The $settings
argument is an array of named
options that determines the appearance of the graph. For details of what to put
in the settings array, see the pages for each of the graph types and the General settings page.
The width and height must be set for SVGGraph to be able to calculate the
sizes and positions of the graph elements. To create a graph that will scale
with its parent element, enable the auto_fit
setting.
values
To provide your data to SVGGraph you must use the values
function.
In earlier versions of SVGGraph it was possible to assign the data directly, but
this is now unsupported.
$graph->values($value, $value, $value, ...);
or
$graph->values($values);
$value
- a single value to display on the graph$values
- an array of values to display on the graph
Sets the list of values that are used to build the graph. Either pass in individual values separated by commas, or an array of values. See the Assigning data values section of the Using SVGGraph page for details.
links
As with the values
function, in previous versions it was
possible to directly assign the links to the graph object, but this is now not
permmitted.
$graph->links($link, $link, $link, ...);
or
$graph->links($links);
$link
- a single link that relates to a graph value$links
- an array of links for the graph values
Sets link URLs for the bars, graph markers or pie graph slices. See the Adding hyperlinks section of the Using SVGGraph page for details.
colours
Once again, it was possible to set the list of colours directly in previous versions of SVGGraph, but this will not work in SVGGraph 3.
$graph->colours($colours);
$colours
- an array of colours
Sets the array of colours, gradients and patterns to use for drawing and filling elements on the graph. See the Colours, gradients and patterns page for details of this and the other colour functions.
colourSet function
$graph->colourSet($dataset, $colours);
$dataset
- dataset index$colours
- an array of colours
Sets the list of colours for a dataset. If there are more datasets on the
graph than are specified using separate calls to colourSet
(or
the other colour functions), then colour sets will be repeated. Using a
NULL
value for the $colours
argument will remove the
colour set (or range) for that dataset.
colourRangeRGB function
$graph->colourRangeRGB($dataset, $r1, $g1, $b1, $r2, $g2, $b2);
$dataset
- dataset index$r1
- red component of start colour$g1
- green component of start colour$b1
- blue component of start colour$r2
- red component of end colour$g2
- green component of end colour$b2
- blue component of end colour
Sets a colour range for a dataset, the colours in between the start and end values being interpolated linearly between their red, green and blue values.
colourRangeHSL function
$graph->colourRangeHSL($dataset, $h1, $s1, $l1, $h2, $s2, $l2, $reverse /* = false */);
$dataset
- dataset index$h1
- hue component of start colour$s1
- saturation component of start colour$l1
- lightness component of start colour$h2
- hue component of end colour$s2
- saturation component of end colour$l2
- lightness component of end colour$reverse
- set totrue
to go the long way around the colour wheel
Sets a colour range for a dataset, using linear interpolation between the
hue, saturation and lightness values. The hue is interpolated using the shorter
arc of the colour wheel by default, setting $reverse
to
true
will interpolate using the larger arc instead.
The colour wheel to the right is actually a pie graph using a HSL range. 0° is at the top (red), yellow is at 60°, green at 120°, cyan at 180°, blue at 240° and purple at 300°.
The saturation is a value between 0 and 1, and the lightness is also a value between 0 and 1. I won't attempt to explain what they do, since there is a handy Wikipedia page to do that for me.
colourRangeRGBtoHSL function
$graph->colourRangeRGBtoHSL($dataset, $r1, $g1, $b1, $r2, $g2, $b2, $reverse /* = false */);
$dataset
- dataset index$r1
- red component of start colour$g1
- green component of start colour$b1
- blue component of start colour$r2
- red component of end colour$g2
- green component of end colour$b2
- blue component of end colour$reverse
- set totrue
to go the long way around the colour wheel
Sets a colour range using HSL interpolation from RGB values. This function translates the red, green and blue components into the hue, saturation and lightness values required for a HSL range.
colourRangeHexRGB function
$graph->colourRangeHexRGB($dataset, $hex1, $hex2);
$dataset
- dataset index$hex1
- 4 or 7 character RGB hex code of start colour$hex2
- 4 or 7 character RGB hex code of end colour
Sets a colour range using RGB interpolation from four character
(e.g. "#fff"
) or seven character (e.g. "#ff00ff"
) hex
codes. Invalid codes will be treated as black.
From version 3.7 this function supports all types of colours, not just hex codes. SVGGraph's hue, saturation and brightness filters can be used too.
colourRangeHexHSL function
$graph->colourRangeHexHSL($dataset, $hex1, $hex2, $reverse /* = false */);
$dataset
- dataset index$hex1
- 4 or 7 character RGB hex code of start colour$hex2
- 4 or 7 character RGB hex code of end colour$reverse
- set totrue
to go the long way around the colour wheel
Sets a colour range using HSL interpolation from four character
(e.g. "#fff"
) or seven character (e.g. "#ff00ff"
) hex
codes. Invalid codes will be treated as black.
From version 3.7 this function supports all types of colours, not just hex codes. SVGGraph's hue, saturation and brightness filters can be used too.
subgraph
$subgraph = $graph->subgraph($type, $x, $y, $w, $h, $settings /* = null */, $extra /* = null */);
$type
- the type of graph$x
- subgraph left$y
- subgraph top$w
- subgraph width$h
- subgraph height$settings
- array of settings$extra
- array of additional settings
Adds a subgraph to the graph, which will be output when the parent graph is rendered. For more details, please consult the subgraphs page.
render
$graph->render($type, $header /* = true */, $content_type /* = true */);
$type
- the type of graph to render$header
- set tofalse
to disable outputting the XML declaration and doctype$content_type
- set tofalse
to disable setting thecontent-type
HTTP header
Builds the graph, outputting it directly to the browser. The
$type
parameter must be one of the supported graph class
names listed in Graph types.
fetch
$svg = $graph->fetch($type, $header /* = true */, $defer_js /* = true */);
$type
- the type of graph to build$header
- set tofalse
to disable outputting the XML declaration and doctype$defer_js
- set tofalse
to return Javascript code with the SVG document
Builds the graph, returning the SVG markup as a string. The
$type
parameter must be one of the supported graph class
names listed in Graph types. By
default the Javascript generated for the graph will be stored for later
retrieval with the fetchJavascript
function.
fetchJavascript
$javascript = $graph->fetchJavascript();
…or…
$javascript = Goat1000\SVGGraph\SVGGraph::fetchJavascript();
Returns the block of Javascript code generated by previous calls to the
fetch
function as a string, including the opening and closing
<script>
tags. When there is no Javascript code to include
the function returns an empty string.
The second method calls fetchJavascript statically, which is useful when you do not have an instance of SVGGraph to use with the first method (e.g. the graph was created in a function). Support for this syntax was added in version 2.24.