SVGGraph Scatter Graphs
Skip to:
SVGGraph currently contains the ScatterGraph and MultiScatterGraph PHP classes for drawing SVG scatter graphs. An example of each type is shown below.
Example code
Here are the settings and PHP code used to generate the multi-scatter graph above:
<?php
require_once 'SVGGraph/autoloader.php';
$settings = array(
'back_colour' => '#eee',
'stroke_colour' => '#000',
'back_stroke_width' => 0,
'back_stroke_colour'=> '#eee',
'axis_colour' => '#333',
'axis_overlap' => 2,
'axis_font' => 'Arial',
'axis_font_size' => 10,
'grid_colour' => '#666',
'label_colour' => '#000',
'pad_right' => 20,
'pad_left' => 20,
'marker_colour' => array('red', 'blue', 'green', 'orange'),
'marker_type' => array('square', 'triangle', 'x', 'cross'),
'marker_size' => array(2, 3, 4, 3),
'scatter_2d' => true,
'best_fit' => 'straight',
'best_fit_dash' => '2,2',
'best_fit_colour' => array('red', 'blue', 'green', 'orange'),
);
$graph = new Goat1000\SVGGraph\SVGGraph(300, 200, $settings);
$graph->values($values);
$graph->render('MultiScatterGraph');
This is not the actual code used by the graph - most of the example graphs on these pages are generated by a single PHP script. The scatter graph data is generated at random by a simple function, not shown here.
MultiScatter styling
The marker_size
, marker_type
,
marker_colour
, marker_stroke_colour
and
marker_stroke_width
options all support the use of arrays to
specify different options for each data set, as explained on the
line graphs page and demonstrated in
the example above.
For more details and examples of the marker options, see the markers page.
Scatter and MultiScatter graph 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 Goat1000\SVGGraph\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 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 Goat1000\SVGGraph\SVGGraph(300, 200, $settings);
$graph->values($values);
$graph->render('ScatterGraph');
Note: to use data in this format you must set the scatter_2d
option to true
; none of the other graph types support data in this
format. The scatter_2d
format has been superseded by the
structured data format,
which would look like this:
$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['structure'] = array(
'key' => 0,
'value' => 1
);
$graph = new Goat1000\SVGGraph\SVGGraph(300, 200, $settings);
$graph->values($values);
$graph->render('ScatterGraph');
The $values
array is the same, but the scatter_2d
option has been replaced by the structure
option, describing how
the data values are arranged.
Best-fit or trend lines
Version 2.10 of SVGGraph added a new option for the scatter graphs,
best_fit
- this defaults to null
for no best-fit
line, though it may be set to either “straight” or “curve”
to produce either straight or curved lines. Curved line support was added in
version 3.20 and best-fit line support was added for line graphs in version
2.21.
For some extra control over the appearance of the line there are some more
options: best_fit_colour
, best_fit_width
and
best_fit_dash
. All of these options (and best_fit
itself) support using arrays to specify different options for each data set.
This example uses four datasets, each with its own best-fit line. For clarity I have used the same colour for the best-fit lines and markers from the same dataset. The options required for this are shown below.
// The datasets in order are red, green, blue and grey
$settings = array(
'best_fit' => 'straight',
//'best_fit_colour' => array('#e11', '#1b1', '#11e', '#888'),
'best_fit_colour' => 'fillColour',
'best_fit_range' => array(array(NULL, NULL), array(1, 6), array(0, 9)),
'best_fit_project' => 'both',
'best_fit_project_opacity' => 0.5,
);
Datasets 0 and 3 (red and grey) are using the full width of the grid area
for calculating the line, specified using a range of array(NULL, NULL)
.
Dataset 1 (green) is using a best_fit_range
of array(1, 6)
to limit the markers used in calculating the angle of the line. I've added a pale
green shape
to highlight the area here. Only the green markers
inside the shaded area are used to calculate the line angle, those outside are
ignored.
The best_fit_project
option is set to both
to
continue all the lines outside their ranges. For datasets 0 and 3 this makes no
difference because they have no ranges set, but for datasets 1 and 2 the
projected part is visible as the dashed line segments, with its opacity reduced
to 50% using best_fit_project_opacity
.
Curved best-fit lines
The example below shows the same data with the best_fit
option
set to “curve”. The graph is produced using random points, so
drawing a curved line through them doesn't make much sense, but I have done it
to show how it looks.
All of these lines are using the default, “quintic” curve type,
though that can be changed using the best_fit_type
option.
Using fillColour and fill
In earlier versions you needed to specify the colours of the lines explicitly,
but from version 3.5 the fillColour
value can be used to make the lines
match the datasets. You can also use fillColour
separately for the
best_fit_project_colour
option, but I don't recommend using fill
for either option (because gradients applied to vertical or horizontal stroked lines
make the lines disappear - that's a weird SVG thing, not something I've done).
Settings
These options are common to all graph types:
General options + Show table
This table lists the options that are common to all the grid-based graph types - the options for grid spacing, axis labelling, etc:
Grid-based graph options - Hide table
Option | Default | Description |
---|---|---|
axis_colour |
"rgb(0,0,0)" |
Colour of axis lines and text. |
axis_colour_h |
NULL |
Colour of horizontal axis. |
axis_colour_v |
NULL |
Colour of vertical axis. |
axis_double_x |
false |
Enables display of X axis at both sides of graph. |
axis_double_y |
false |
Enables display of Y axis at both sides of graph. |
axis_extend_bottom |
null |
Extends the bottom end of horizontal axes. |
axis_extend_left |
null |
Extends the left side of horizontal axes. |
axis_extend_right |
null |
Extends the right side of horizontal axes. |
axis_extend_top |
null |
Extends the top end of horizontal axes. |
axis_fallback_max |
1 |
Sets a fallback value for the axis maximum when the graph contains all zero values. |
axis_font |
"Courier New" |
Font for axis division labels. |
axis_font_adjust |
0.6 |
Font aspect ratio of axis division text. |
axis_font_adjust_h |
NULL |
Font aspect ratio of horizontal axis division text. |
axis_font_adjust_v |
NULL |
Font aspect ratio of vertical axis division text. |
axis_font_h |
NULL |
Font for horizontal axis division labels. |
axis_font_size |
10 |
Font size for axis division labels. |
axis_font_size_h |
NULL |
Font size for horizontal axis division labels. |
axis_font_size_v |
NULL |
Font size for vertical axis division labels. |
axis_font_v |
NULL |
Font for vertical axis division labels. |
axis_font_weight |
"normal" |
Font weight for axis division labels. |
axis_font_weight_h |
null |
Font weight for horizontal axis division labels. |
axis_font_weight_v |
null |
Font weight for vertical axis division labels. |
axis_label_position |
0.5 |
Sets position of axis label as a fraction of the axis length. |
axis_label_position_h |
null |
Sets position of horizontal axis label as a fraction of the axis length. |
axis_label_position_v |
null |
Sets position of vertical axis label as a fraction of the axis length. |
axis_levels_h |
null |
Sets the number of levels of horizontal axis division labels. |
axis_levels_v |
null |
Sets the number of levels of vertical axis division labels. |
axis_max_h |
NULL |
Maximum value for horizontal axis. |
axis_max_v |
NULL |
Maximum value for vertical axis. |
axis_min_h |
NULL |
Minimum value for horizontal axis. |
axis_min_v |
NULL |
Minimum value for vertical axis. |
axis_overlap |
5 |
Distance axes extend past grid. |
axis_pad_bottom |
0 |
Inserts space between bottom axis and grid area. |
axis_pad_left |
0 |
Inserts space between left axis and grid area. |
axis_pad_right |
0 |
Inserts space between right axis and grid area. |
axis_pad_top |
0 |
Inserts space between top axis and grid area. |
axis_right |
false |
Enables axis on right of grid. |
axis_space |
15 |
Space between axes on the right of the graph. |
axis_stroke_width |
2 |
Width of axis lines. |
axis_stroke_width_h |
NULL |
Width of horizontal axis line. |
axis_stroke_width_v |
NULL |
Width of vertical axis line. |
axis_text_align |
null |
Sets the text alignment for axis division labels. |
axis_text_align_h |
null |
Sets the text alignment for horizontal axis division labels. |
axis_text_align_v |
null |
Sets the text alignment for vertical axis division labels. |
axis_text_angle_h |
0 |
Angle of horizontal axis division text. |
axis_text_angle_v |
0 |
Angle of vertical axis division text. |
axis_text_back_colour |
null |
Background colour for axis division labels. |
axis_text_back_colour_h |
null |
Background colour for horizontal axis division labels. |
axis_text_back_colour_v |
null |
Background colour for vertical axis division labels. |
axis_text_callback |
NULL |
Callback function for axis division text labels. |
axis_text_callback_x |
NULL |
Callback function for X axis division text labels. |
axis_text_callback_y |
NULL |
Callback function for Y axis division text labels. |
axis_text_colour |
NULL |
Colour of axis division labels. |
axis_text_colour_h |
NULL |
Colour of horizontal axis division labels. |
axis_text_colour_v |
NULL |
Colour of vertical axis division labels. |
axis_text_line_spacing |
null |
Sets line spacing for axis division labels. |
axis_text_line_spacing_h |
null |
Sets line spacing for horizontal axis division labels. |
axis_text_line_spacing_v |
null |
Sets line spacing for vertical axis division labels. |
axis_text_location |
"grid" |
Position of axis text when graph contains negative numbers. |
axis_text_location_h |
null |
Position of horizontal axis text when graph contains negative numbers. |
axis_text_location_v |
null |
Position of vertical axis text when graph contains negative numbers. |
axis_text_position |
"outside" |
Position of axis division text labels. |
axis_text_position_h |
NULL |
Position of horizontal axis division text labels. |
axis_text_position_v |
NULL |
Position of vertical axis division text labels. |
axis_text_space |
2 |
Space between axis division text and axis. |
axis_text_space_h |
NULL |
Space between horizontal axis division text and axis. |
axis_text_space_v |
NULL |
Space between vertical axis division text and axis. |
axis_ticks_x |
null |
Specifies positions of axis tick marks for X-axis. |
axis_ticks_y |
null |
Specifies positions of axis tick marks for Y-axis. |
axis_tightness_x |
0 |
Sets the algorithm used to choose X-axis divisions. |
axis_tightness_y |
1 |
Sets the algorithm used to choose Y-axis divisions. |
axis_top |
false |
Switches the horizontal axis from the bottom of the grid to the top. |
axis_zero_y |
true |
Allows graph to pick a non-zero value for bottom of Y-axis. |
dataset_axis |
NULL |
Enables multiple Y-axes. |
datetime_text_format |
NULL |
Date format for date/time axis. |
datetime_week_start |
"monday" |
Week start day for date/time axis. |
decimal_digits |
NULL |
Number of decimal digits in axis text labels. |
decimal_digits_x |
NULL |
Number of decimal digits in X axis text labels. |
decimal_digits_y |
NULL |
Number of decimal digits in Y axis text labels. |
division_colour |
NULL |
Colour of axis division marks. |
division_colour_h |
NULL |
Colour of horizontal axis division marks. |
division_colour_v |
NULL |
Colour of vertical axis division marks. |
division_size |
3 |
Length of division marks. |
division_size_h |
NULL |
Size of horizontal axis division marks. |
division_size_v |
NULL |
Size of vertical axis division marks. |
division_style |
"out" |
Style of axis division marks. |
division_style_h |
NULL |
Style of horizontal axis division marks. |
division_style_v |
NULL |
Style of vertical axis division marks. |
force_block_label_x |
false |
Forces block-style labelling for X axis. |
grid_back_colour |
NULL |
Colour of grid area background. |
grid_back_opacity |
1 |
Opacity of grid background fill. |
grid_back_stripe |
false |
Displays grid background stripes. |
grid_back_stripe_colour |
"rgb(240,240,240)" |
Colour of grid background stripes. |
grid_back_stripe_opacity |
1 |
Opacity of grid background stripes. |
grid_bottom |
NULL |
Position of bottom of grid. |
grid_clip_overlap_x |
2 |
Sets the grid clipping overlap to the left and right sides. |
grid_clip_overlap_y |
10 |
Sets the grid clipping overlap above and below the grid area. |
grid_colour |
"rgb(220,220,220)" |
Colour of grid lines. |
grid_colour_h |
NULL |
Colour of vertical grid lines. |
grid_colour_v |
NULL |
Colour of horizontal grid lines. |
grid_dash |
NULL |
Grid lines dash pattern. |
grid_dash_h |
NULL |
Grid vertical lines dash pattern. |
grid_dash_v |
NULL |
Grid horizontal lines dash pattern. |
grid_division_h |
NULL |
Division interval on horizontal axis. |
grid_division_v |
NULL |
Division interval on vertical axis. |
grid_left |
NULL |
Position of left side of grid. |
grid_right |
NULL |
Position of right side of grid. |
grid_straight |
false |
Enables straight grid lines on radar graph. |
grid_stroke_width |
1 |
Sets the thickness of grid lines. |
grid_stroke_width_h |
null |
Sets the thickness of grid lines drawn from the horizontal axis. |
grid_stroke_width_v |
null |
Sets the thickness of grid lines drawn from the vertical axis. |
grid_subdivision_colour |
"rgba(220,220,220,0.5)" |
Colour of grid subdivision lines. |
grid_subdivision_colour_h |
NULL |
Colour of vertical grid subdivision lines. |
grid_subdivision_colour_v |
NULL |
Colour of horizontal grid subdivision lines. |
grid_subdivision_dash |
NULL |
Grid subdivision lines dash pattern. |
grid_subdivision_dash_h |
NULL |
Grid vertical subdivision lines dash pattern. |
grid_subdivision_dash_v |
NULL |
Grid horizontal subdivision lines dash pattern. |
grid_subdivision_stroke_width |
null |
Sets the thickness of grid subdivision lines. |
grid_subdivision_stroke_width_h |
null |
Sets the thickness of vertical grid subdivision lines. |
grid_subdivision_stroke_width_v |
null |
Sets the thickness of horizontal grid subdivision lines. |
grid_top |
NULL |
Position of top of grid. |
increment |
NULL |
X-axis increment for histograms. |
label_colour |
"rgb(0,0,0)" |
Colour for axis label text. |
label_colour_h |
NULL |
Colour for horizontal axis label text. |
label_colour_v |
NULL |
Colour for vertical axis label text. |
label_font |
"Arial" |
Font for axis labels. |
label_font_h |
NULL |
Font for horizontal axis label. |
label_font_size |
10 |
Font size for axis labels. |
label_font_size_h |
NULL |
Font size for horizontal axis label. |
label_font_size_v |
NULL |
Font size for vertical axis label. |
label_font_v |
NULL |
Font for vertical axis label. |
label_font_weight |
"normal" |
Font weight for axis labels. |
label_font_weight_h |
NULL |
Font weight for horizontal axis label. |
label_font_weight_v |
NULL |
Font weight for vertical axis label. |
label_h |
"" |
Text label for horizontal axis. |
label_line_spacing |
null |
Sets line spacing for axis labels. |
label_line_spacing_h |
null |
Sets line spacing for label on horizontal axis. |
label_line_spacing_v |
null |
Sets line spacing for label on vertical axis. |
label_space |
6 |
Spacing around axis labels. |
label_v |
"" |
Text label for vertical axis. |
label_x |
"" |
Text label for X axis. |
label_y |
"" |
Text label for Y axis. |
limit_text_angle |
true |
Limits the angle of axis text to between -90° and +90°. |
log_axis_x |
false |
Enables logarithmic scale for X-axis. |
log_axis_x_base |
10 |
Specifies logarithm base for use on logarithmic X-axis. |
log_axis_y |
false |
Enables logarithmic Y-axis divisions. |
log_axis_y_base |
10 |
Base of the logarithmic axis scale. |
minimum_grid_spacing |
15 |
Minimum distance between grid/axis divisions. |
minimum_grid_spacing_h |
NULL |
Minimum distance between horizontal axis divisions. |
minimum_grid_spacing_v |
NULL |
Minimum distance between vertical axis divisions. |
minimum_subdivision |
5 |
Minimum distance between subdivisions. |
minimum_units_y |
0 |
Minimum units to display on Y axis. |
show_axes |
true |
Enables axis display. |
show_axis_h |
true |
Enables horizontal axis line. |
show_axis_text_h |
true |
Enables labelling of horizontal axis divisions. |
show_axis_text_v |
true |
Enables labelling of vertical axis divisions. |
show_axis_v |
true |
Enables vertical axis line. |
show_divisions |
true |
Enables axis division marks. |
show_grid |
true |
Disables displaying grid. |
show_grid_h |
true |
Disables displaying horizontal grid lines. |
show_grid_subdivisions |
false |
Enables displaying grid subdivisions. |
show_grid_v |
true |
Disables displaying vertical grid lines. |
show_subdivisions |
false |
Enables axis subdivision marks. |
show_x_axis |
true |
Enables X-axis on radar graphs. |
subdivision_colour |
NULL |
Colour of axis subdivision marks. |
subdivision_colour_h |
NULL |
Colour of horizontal axis subdivision marks. |
subdivision_colour_v |
NULL |
Colour of vertical axis subdivision marks. |
subdivision_h |
NULL |
Horizontal axis subdivision interval. |
subdivision_size |
2 |
Length of axis subdivision marks. |
subdivision_size_h |
NULL |
Length of horizontal axis subdivision marks. |
subdivision_size_v |
NULL |
Length of vertical axis subdivision marks. |
subdivision_style |
"out" |
Style of axis subdivision marks. |
subdivision_style_h |
NULL |
Style of horizontal axis subdivision marks. |
subdivision_style_v |
NULL |
Style of vertical axis subdivision marks. |
subdivision_v |
NULL |
Vertical axis subdivision interval. |
text_classes_file |
null |
Specifies an INI file for use with text classes. |
units_before_x |
NULL |
Units shown before value on X-axis. |
units_before_y |
NULL |
Units shown before value on Y-axis. |
units_x |
NULL |
Units shown after value on X-axis. |
units_y |
NULL |
Units shown after value on Y-axis. |
These are the marker-related options from the options list:
Marker options - Hide table
Option | Default | Description |
---|---|---|
block_position_markers |
false |
Positions markers between grid points. |
figure |
null |
Defines a shape or group of shapes for reuse. |
gantt_milestone_colour |
null |
Sets the colour of milestone markers on Gantt charts. |
gantt_milestone_size |
6 |
Sets the size of milestone markers on Gantt charts. |
gantt_milestone_type |
"diamond" |
Sets the type of marker used for milestones on Gantt charts. |
marker_angle |
0 |
Sets a rotation angle for the markers. |
marker_colour |
NULL |
Colour of markers. |
marker_opacity |
1 |
Sets the opacity of the markers. |
marker_size |
5 |
Size of markers. |
marker_solid |
true |
Disables use of patterns and gradients in markers. |
marker_stroke_colour |
NULL |
Colour of marker outlines. |
marker_stroke_width |
1 |
Thickness of marker outlines. |
marker_type |
"circle" |
Shape of markers. |
shape |
NULL |
Adds custom shapes to the graph. |
The best-fit line options are common to all line and scatter graphs:
Best-fit options - Hide table
Option | Default | Description |
---|---|---|
best_fit |
NULL |
Enables best-fit lines. |
best_fit_above |
false |
Depth of best-fit lines. |
best_fit_colour |
"rgb(0,0,0)" |
Colour of best-fit lines. |
best_fit_dash |
NULL |
Best-fit lines dash pattern. |
best_fit_opacity |
1 |
Opacity of best-fit lines. |
best_fit_project |
NULL |
Best-fit line projections. |
best_fit_project_colour |
NULL |
Colour of best-fit line projections. |
best_fit_project_dash |
4 |
Dash pattern for best-fit line projections. |
best_fit_project_opacity |
NULL |
Opacity of best-fit line projections. |
best_fit_project_width |
NULL |
Thickness of best-fit line projections. |
best_fit_range |
NULL |
Range for best-fit lines. |
best_fit_type |
NULL |
Limits type of function used for curved best-fit lines. |
best_fit_width |
1 |
Thickness of best-fit lines. |
The options in this table are for use with the scatter graphs:
Scatter graph options - Hide table
Option | Default | Description |
---|---|---|
scatter_2d |
NULL |
If true data is in (x,y) pairs, as described above |