SVGGraph function reference

« Return to SVGGraph page

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 */);

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);

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.

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);

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);

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);

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);

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 */);

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 */);

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);

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.

colourRangeHexHSL function

$graph->colourRangeHexHSL($dataset, $hex1, $hex2, $reverse /* = false */);

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.

subgraph

$subgraph = $graph->subgraph($type, $x, $y, $w, $h, $settings /* = null */, $extra /* = null */);

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 */);

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 */);

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.

« Back to top of page Colours, gradients and patterns »