SVGGraph Line and Scatter Graphs

« Return to SVGGraph page

SVGGraph currently contains these classes for drawing line and scatter graphs: LineGraph; MultiLineGraph; ScatterGraph. An example of each type is shown below.

LineGraph
MultiLineGraph

ScatterGraph
MultiScatterGraph

Example code

Here are the settings and PHP code used to generate the graphs above:

require_once('includes/SVGGraph.php');
 
$settings = array(
  'back_colour' => '#eee',  'stroke_colour' => '#000',
  'back_stroke_width' => 0, 'back_stroke_colour' => '#eee',
  'back_image' => 'images/svgbg.png',
  'axis_colour' => '#333',  'axis_overlap' => 2,
  'axis_font' => 'Georgia', 'axis_font_size' => 10,
  'grid_colour' => '#666',  'label_colour' => '#000',
  'pad_right' => 20,        'pad_left' => 20,
  'link_base' => '/',       'link_target' => '_top',
  'fill_under' => array(true,false),
  'marker_size' => 3,
  'marker_type' => array('circle','square'),
  'marker_colour' => array('blue','red')
);
 
$values = array(
 array('Dough' => 30, 'Ray' => 50, 'Me' => 40, 'So' => 25, 'Far' => 45, 'Lard' => 35),
 array('Dough' => 20, 'Ray' => 30, 'Me' => 20, 'So' => 15, 'Far' => 25, 'Lard' => 35,
  'Tea' => 45)
);
 
$colours = array(array('red','yellow'), array('blue','white'));
$links = array('Dough' => 'jpegsaver.php', 'Ray' => 'crcdropper.php',
  'Me' => 'svggraph.php');
 
$graph = new SVGGraph(300, 200, $settings);
$graph->colours = $colours;
 
$graph->Values($values);
$graph->Links($links);
$graph->Render('LineGraph');

This is not the actual code used - I have adapted it to make it easier to copy and paste it into a new file to try for yourself. To change the type of graph generated, change 'LineGraph' in the final call to $graph->Render to one of the other types.

The scatter graph data is generated by a simple function, not shown here.

MultiLine and MultiScatter options

From version 2.4, SVGGraph supports passing arrays of values for the fill_under, marker_size, marker_type and marker_colour options, as shown in the example above. These allow you to control how the markers look and if the area under the line should be filled for each data set separately.

Version 2.5 makes fill_opacity, line_stroke_width and the new line_dash option support arrays too.

Scatter and MultiScatter data

The standard way to pass data to any of the graph types is by passing in a simple array:

$values = array(
  1 => 20, 3 => 40, 4 => 30, 5 => 60, 7 => 80, 9 => 70
);
 
$graph = new SVGGraph(300, 200, $settings);
$graph->Values($values);
$graph->Render('ScatterGraph');

This is fine, except that multiple points on a scatter graph may share the same x-coordinate - which is impossible to specify by using the key => value pairs of an array.

To solve this, I've added a new option in version 2.4, scatter_2d. This option makes the scatter graphs accept data as an array of pairs of coordinates:

$values = array(
  array(1,20), array(1,50), array(3,40), array(4,30),
  array(5,60), array(7,80), array(7,40), array(9,70)
);
$settings['scatter_2d'] = true;
 
$graph = new SVGGraph(300, 200, $settings);
$graph->Values($values);
$graph->Render('ScatterGraph');

Note that to use data in this format you must set the scatter_2d option to true, and that none of the other graph types support data in this format.

Line dash pattern

SVGGraph version 2.5 adds the line_dash option, which specifies a pattern of dashes to use for line graphs.

In the example below, the MultiLineGraph has two datasets, so I have used an array for line_dash containing two pattern strings. The first pattern is just the number "10" - this means 10 pixels of line, then 10 pixels of gap.

line_dash = array('10','8,2,5')

The second line uses a pattern of '8,2,5'. SVG dash patterns that have an odd number of members are doubled-up, so this becomes '8,2,5,8,2,5' ('10' in the example above becomes '10,10') - an 8 pixel line, 2 pixel gap, 5 pixel line, then the doubled part gives an 8 pixel gap, 2 pixel line, 5 pixel gap, before the pattern repeats.

Settings

These options are common to all graph types:

OptionDefaultDescription
title NULL Contents of title tag, or NULL for none
description NULL Contents of desc tag, or NULL for none
stroke_colour rgb(0,0,0) Colour of graph lines
stroke_width 1 Thickness of graph lines, 0 disables line drawing
back_colour rgb(240,240,240) Background colour of graph
back_round 0 Radius of rounded background edge
back_stroke_width 1 Thickness of background edge
back_stroke_colour rgb(0,0,0) Colour of background edge
back_image NULL Image to use for background (NULL for none). See the SVGGraph background image page for details.
back_image_width 100% Width of background image
back_image_height 100% Height of background image
back_image_opacity 1.0 Opacity of background image (0.0-1.0)
back_image_top 0 Top offset of background image
back_image_left 0 Left offset of background image
back_image_mode auto Background image display method. Options are:
auto
image will be uniformly scaled to fit in width and height;
tile
image will be tiled in blocks using width and height;
stretch
image will be stretched to fit width and height
pad_top 10 Space at top of graph
pad_bottom 10 Space at bottom of graph
pad_left 10 Space to left of graph
pad_right 10 Space to right of graph
link_base Prepended to all links
link_target _blank Link target frame
namespace false Option to use the svg: namespace prefix
doctype false Option to output the DOCTYPE
show_tooltips true Enables display of tooltips over graph markers
tooltip_colour black Tooltip text/border colour
tooltip_stroke_width 1 Tooltip border thickness
tooltip_back_colour #ffffcc Tooltip rectangle background colour
tooltip_font sans-serif Font for tooltips
tooltip_font_weight normal Tooltip font weight
tooltip_font_size 10 Tooltip font size
tooltip_padding 3 Tooltip rectangle padding
tooltip_round 0 Radius of rounded tooltip corner
tooltip_shadow_opacity 0.3 Opacity of tooltip shadow (0.0-1.0, 0 disables shadow)
tooltip_offset 10 Distance between cursor and tooltip
compat_events false Set to TRUE to fall back to inline event handlers, required by Adobe IE plugin
graph_title NULL Title displayed on the graph
graph_title_font sans-serif Font for graph title
graph_title_font_size 12 Font size for graph title
graph_title_font_weight normal Font weight for graph title
graph_title_space 10 Spacing around graph title
graph_title_colour rgb(0,0,0) Colour of graph title
graph_title_position top Where to show graph title ("top", "bottom", "left", "right")
legend_entries NULL Array of entries for the legend
legend_position top right Position of the legend. Supports "top", "bottom", "left", "right", "inner", "outer" and pairs of X and Y offsets
legend_stroke_width 1 Thickness of legend border
legend_stroke_colour rgb(0,0,0) Colour of legend border
legend_back_colour rgb(255,255,255) Colour of legend background
legend_round 0 Radius of rounded corners for legend border
legend_font sans-serif Font for legend entries
legend_font_weight normal Font weight for legend entries
legend_font_size 10 Font size for legend entries
legend_font_adjust 0.6 Ratio of font width to height
legend_colour rgb(0,0,0) Colour of legend entries text
legend_padding 5 Amount of spacing between entries in legend
legend_entry_width 20 Width of legend entry box
legend_entry_height 20 Height of legend entry box
legend_title Title for legend
legend_title_font NULL Font for legend title (NULL = use legend_font)
legend_title_font_size NULL Font size for legend title (NULL = use legend_font_size)
legend_title_font_weight bold Font weight for legend title
legend_title_font_adjust NULL Ratio of font width to height (NULL = use legend_font_adjust)
legend_title_colour NULL Colour of legend title (NULL = use legend_colour
legend_text_side right Which side of the entry box the text should be on ("left", "right")
legend_draggable true Makes the legend draggable with the mouse
legend_autohide false Makes the legend hide when the cursor is over it
Global options

The following table lists the other options relevant to the line and scatter graph types - many of these options are also used by the bar graph types:

OptionDefaultDescription
show_grid true Grid on/off option
show_axes true Axes on/off option
show_divisions true Axis division points on/off
show_axis_text_h true Horizontal axis division numbering or labels on/off
show_axis_text_v true Vertical axis division numbering on/off
grid_colour rgb(220,220,220) Colour of grid lines
axis_colour rgb(0,0,0) Colour of axis lines
axis_font monospace Font for axis division text
axis_font_size 10 Axis division font size
axis_font_adjust 0.6 Approx ratio of font width to height
axis_overlap 5 Amount to extend axes past graph area
axis_text_colour rgb(0,0,0) Colour of axis division text
division_size 3 Length of division lines
division_colour NULL Colour of division lines, or NULL to use axis colour
minimum_grid_spacing 15 Minimum distance between grid/axis lines
minimum_grid_spacing_h NULL Minimum distance between divisions on horizontal axis
minimum_grid_spacing_v NULL Minimum distance between divisions on vertical axis
marker_size 5 Size of point markers
marker_type circle Type of marker to use (circle, square, triangle, cross, x, pentagon)
marker_colour NULL Colour of marker (NULL to use same as line)
line_stroke_width[1] 2 Thickness of graph line
line_dash[1] NULL Line dash pattern
fill_under[1] false If true, the area under the line is filled with colour or gradient
fill_opacity[2] 1 Opacity of the filled area
grid_enclose_h[3] false If true, the horizontal grid will enclose all data points
grid_enclose_v[3] true If true, the vertical grid will enclose all data points
axis_min_h[4] NULL Minimum extent of X-axis
axis_max_h[4] NULL Maximum extent of X-axis
axis_min_v[4] NULL Minimum extent of Y-axis
axis_max_v[4] NULL Maximum extent of Y-axis
grid_division_h[4] NULL Grid interval on X-axis
grid_division_v[4] NULL Grid interval on Y-axis
show_subdivisions false Enables axis subdivisions
subdivision_size 2 Size of axis subdivision markings
subdivision_colour division_colour Colour of axis subdivision markings
show_grid_subdivision false Enables grid subdivisions
grid_subdivision_colour rgba(220,220,220,0.5) Colour of grid subdivision lines
minimum_subdivision 5 Minimum distance between subdivisions
subdivision_h NULL Subdivision grid interval on X-axis
subdivision_v NULL Subdivision grid interval on Y-axis
label_h[5] Horizontal axis label
label_v[5] Vertical axis label
label_x[5] X-axis label
label_y[5] Y-axis label
label_space[5] 6 Spacing around axis labels
label_font[5] sans-serif Font for axis labels
label_font_size[5] 10 Font size for axis labels
label_font_weight[5] normal Font weight for axis labels
label_colour[5] rgb(0,0,0) Colour for axis label text
scatter_2d[6] NULL If true data is in (x,y) pairs, as described above
Line and scatter graph options

« Back to top of page Pie graphs »