Class svgGraph

svgGraph

public class svgGraph

Takes an array or multiple arrays of data and outputs a graph in SVG format. The SVG language allows for a high degree of control of the output, thus this class is intended to be extended.

Version:
1.0alpha
Author:
Herman Veluwenkamp

Field Summary
 string $dataX
          Array of data holding values for X axis
 string $dataY
          Two dimensional array holding values for Y axis.
 integer $decimalPlacesY
          Number of decimal places to show for Y axis tags.
 string $error
          Contains error messages.
 string $extraSVG
          Extra SVG to add to graph.
 integer $graphicHeight
          Total height of svg graphic.
 integer $graphicWidth
          Total width of svg graphic.
 integer $innerPaddingX
          Padding between bottom border of plot area and text (tags).
 integer $innerPaddingY
          Padding between left border of plot area and text (tags).
 string $labelX
          Label for X axis.
 string $labelY
          Label for Y axis.
 integer $maxY
          Maximum value for Y axis values.
 integer $minY
          Minimum value for Y axis values.
 integer $numGridlinesX
          Number of grid lines corresponding to X axis.
 integer $numGridlinesY
          Number of grid lines corresponding to Y axis.
 integer $offsetGridlinesX
          Offset of first X axis gridline from lower-left of plot area as a fraction of normal gridline spacing.
 integer $offsetGridlinesY
          Offset of first Y axis gridline from lower-left of plot area as a fraction of normal gridline spacing.
 integer $outerPadding
          Padding between outer border of graphic area and text (title and labels).
 integer $plotHeight
          Height of plot area.
 integer $plotOffsetX
          Offset of plot area from left of graphic area.
 integer $plotOffsetY
          Offset of plot area from top of graphic area.
 integer $plotWidth
          Width of plot area.
 integer $resolutionY
          Resolution for Y axis tags.
 integer $rotTagsX
          X axis tags rotation.
 integer $rotTagsY
          Y axis tags rotation.
 string $styleBarDefault
          Default presentation attributes for bar plots.
 string $styleBox
          Presentation attributes for box around plot area.
 string $styleBoxDefault
          Default presentation attributes for box around plot area.
 string $styleGridX
          Presentation attributes for grid corresponding to X axis.
 string $styleGridXDefault
          Default presentation attributes for grid corresponding to X axis.
 string $styleGridY
          Presentation attributes for grid corresponding to Y axis.
 string $styleGridYDefault
          Default presentation attributes for grid corresponding to X axis.
 string $styleLabelX
          Presentation attributes for label.
 string $styleLabelXDefault
          Default presentation attributes for label.
 string $styleLabelY
          Presentation attributes for label.
 string $styleLabelYDefault
          Default presentation attributes for label.
 string $styleLineDefault
          Default presentation attributes for line plots.
 string $stylePolylineDefault
          Default presentation attributes for polyline plots (inside group tag).
 string $styleTagsX
          Presentation attributes for X axis tags.
 string $styleTagsXDefault
          Default presentation attributes for X axis tags.
 string $styleTagsY
          Presentation attributes for Y axis tags.
 string $styleTagsYDefault
          Default presentation attributes for Y axis tags.
 string $styleTitle
          Presentation attributes for title.
 string $styleTitleDefault
          Default presentation attributes for title.
 string $svg
          SVG XML result.
 string $title
          Title for Graph.
 
Constructor Summary
svgGraph()
          Define static variables used in the class.
 
Method Summary
private  array _findRange(var $data, var $min, var $max, var $resolution)
          Find the maximum and minimum values for a set of data.
The $resolution variable is used for rounding maximum and minimum values.
If maximum value is 8645 then
If $resolution is 0, then maximum value becomes 9000.
If $resolution is 1, then maximum value becomes 8700.
If $resolution is 2, then maximum value becomes 8650.
If $resolution is 3, then maximum value becomes 8645.
 boolean bar(var $whichDataSet)
          Draw a bar for each data point from the data set selected.
 void drawBox()
          Draws the box around the plotting area.
 void drawGraph()
          Calls functions to draw title, labels, tags, grid lines, and box of graph.
 void drawGridX()
          Draws the grid lines from top to bottom in the plotting area.
 void drawGridY()
          Draws the grid lines from right to left in the plotting area.
 void drawOuterText()
          Draw the title and axis labels around the outside of the graphic area.
 void drawTagsX()
          Draws the axis tag text outside the plotting area on the x axis.
 void drawTagsY()
          Draws the axis tag text outside the plotting area on the y axis.
 void generateSVG()
          Generate SVG for entire graph.
 boolean init()
          Initialises the variables used for drawing points, lines, grid, and ticks in the plotting area.
 void line(var $whichDataSet)
          Draw line from one point to the next stopping at each.
 void outputSVG()
          Output SVG as XML text including appropriate HTTP header information.
 boolean polyLine(var $whichDataSet)
          Draw a line from one point to the next continuously without stopping to draw markers.
 

Field Detail

$dataX

public string $dataX
Array of data holding values for X axis

$dataY

public string $dataY
Two dimensional array holding values for Y axis. The key for each array must be unique.

$decimalPlacesY

public integer $decimalPlacesY
Number of decimal places to show for Y axis tags.

$error

public string $error
Contains error messages.

$extraSVG

public string $extraSVG
Extra SVG to add to graph. e.g. Filters, Defs, Title. Note: Title is useful to add if image is viewed out of context.
$svg
String holding SVG text.

$graphicHeight

public integer $graphicHeight
Total height of svg graphic.

$graphicWidth

public integer $graphicWidth
Total width of svg graphic.

$innerPaddingX

public integer $innerPaddingX
Padding between bottom border of plot area and text (tags).

$innerPaddingY

public integer $innerPaddingY
Padding between left border of plot area and text (tags).

$labelX

public string $labelX
Label for X axis.

$labelY

public string $labelY
Label for Y axis.

$maxY

public integer $maxY
Maximum value for Y axis values. If a higher value is found in data then this value is not used.

$minY

public integer $minY
Minimum value for Y axis values. If a lower value is found in data then this value is not used.

$numGridlinesX

public integer $numGridlinesX
Number of grid lines corresponding to X axis.

$numGridlinesY

public integer $numGridlinesY
Number of grid lines corresponding to Y axis.

$offsetGridlinesX

public integer $offsetGridlinesX
Offset of first X axis gridline from lower-left of plot area as a fraction of normal gridline spacing.

$offsetGridlinesY

public integer $offsetGridlinesY
Offset of first Y axis gridline from lower-left of plot area as a fraction of normal gridline spacing.

$outerPadding

public integer $outerPadding
Padding between outer border of graphic area and text (title and labels).

$plotHeight

public integer $plotHeight
Height of plot area.

$plotOffsetX

public integer $plotOffsetX
Offset of plot area from left of graphic area.

$plotOffsetY

public integer $plotOffsetY
Offset of plot area from top of graphic area.

$plotWidth

public integer $plotWidth
Width of plot area.

$resolutionY

public integer $resolutionY
Resolution for Y axis tags. See notes for method _findRange.

$rotTagsX

public integer $rotTagsX
X axis tags rotation. Negative/Anticlockwise. Increase innerPaddingX to prevent overlap with plot area.

$rotTagsY

public integer $rotTagsY
Y axis tags rotation. Negative/Anticlockwise. Increase innerPaddingY to prevent overlap with plot area.

$styleBarDefault

public string $styleBarDefault
Default presentation attributes for bar plots.

$styleBox

public string $styleBox
Presentation attributes for box around plot area.

$styleBoxDefault

public string $styleBoxDefault
Default presentation attributes for box around plot area.

$styleGridX

public string $styleGridX
Presentation attributes for grid corresponding to X axis.

$styleGridXDefault

public string $styleGridXDefault
Default presentation attributes for grid corresponding to X axis.

$styleGridY

public string $styleGridY
Presentation attributes for grid corresponding to Y axis.

$styleGridYDefault

public string $styleGridYDefault
Default presentation attributes for grid corresponding to X axis.

$styleLabelX

public string $styleLabelX
Presentation attributes for label.

$styleLabelXDefault

public string $styleLabelXDefault
Default presentation attributes for label.

$styleLabelY

public string $styleLabelY
Presentation attributes for label.

$styleLabelYDefault

public string $styleLabelYDefault
Default presentation attributes for label.

$styleLineDefault

public string $styleLineDefault
Default presentation attributes for line plots.

$stylePolylineDefault

public string $stylePolylineDefault
Default presentation attributes for polyline plots (inside group tag).

$styleTagsX

public string $styleTagsX
Presentation attributes for X axis tags.

$styleTagsXDefault

public string $styleTagsXDefault
Default presentation attributes for X axis tags.

$styleTagsY

public string $styleTagsY
Presentation attributes for Y axis tags.

$styleTagsYDefault

public string $styleTagsYDefault
Default presentation attributes for Y axis tags.

$styleTitle

public string $styleTitle
Presentation attributes for title.

$styleTitleDefault

public string $styleTitleDefault
Default presentation attributes for title.

$svg

public string $svg
SVG XML result.

$title

public string $title
Title for Graph.
Constructor Detail

svgGraph

public svgGraph()
Define static variables used in the class.
Method Detail

_findRange

private array _findRange(var $data,
                         var $min,
                         var $max,
                         var $resolution)
Find the maximum and minimum values for a set of data.
The $resolution variable is used for rounding maximum and minimum values.
If maximum value is 8645 then
If $resolution is 0, then maximum value becomes 9000.
If $resolution is 1, then maximum value becomes 8700.
If $resolution is 2, then maximum value becomes 8650.
If $resolution is 3, then maximum value becomes 8645.
Parameters:
$data - Data to find the range for
$min - Minimum value to start at. If a lower number is found then this value is not used.
$max - Maximum value to start at. If a larger number is found then this value is not used.
$resolution - Resolution for range.

bar

public boolean bar(var $whichDataSet)
Draw a bar for each data point from the data set selected.
Parameters:
$whichDataSet - Which set of data to draw. This is the index of the data array to be used.

The format parameter array for the selected dataset can have three members:
'style' - Style for bar,
'barWidth' - Width of bar as fraction of distance between gridlines. Values greater than 1 will result in bars overlapping.
'barOffset' - Offset of the bar as fraction of bar width. By default it is centered on the gridline.
Returns:
BOOLEAN FALSE if style, barWidth, or barOffset parameters are missing.

drawBox

public void drawBox()
Draws the box around the plotting area.

drawGraph

public void drawGraph()
Calls functions to draw title, labels, tags, grid lines, and box of graph.

drawGridX

public void drawGridX()
Draws the grid lines from top to bottom in the plotting area.

drawGridY

public void drawGridY()
Draws the grid lines from right to left in the plotting area.

drawOuterText

public void drawOuterText()
Draw the title and axis labels around the outside of the graphic area.

drawTagsX

public void drawTagsX()
Draws the axis tag text outside the plotting area on the x axis.

drawTagsY

public void drawTagsY()
Draws the axis tag text outside the plotting area on the y axis.

generateSVG

public void generateSVG()
Generate SVG for entire graph.

init

public boolean init()
Initialises the variables used for drawing points, lines, grid, and ticks in the plotting area.
Returns:
Boolean - FALSE if an error was encountered while processing data.

line

public void line(var $whichDataSet)
Draw line from one point to the next stopping at each. This method is used for drawing lines with markers at each plot point.

The format parameter array for the selected dataset can have two members:
'style' - Style for line,
'attributes' - Attributes to place inside line tag.
Parameters:
$whichDataSet - Which set of data to draw. This is the index of the data array to be used.

outputSVG

public void outputSVG()
Output SVG as XML text including appropriate HTTP header information.

polyLine

public boolean polyLine(var $whichDataSet)
Draw a line from one point to the next continuously without stopping to draw markers. This method is used for drawing lines with markers on the end, for example, an arrow indicating trend.

The format parameter array for the selected dataset can have two members:
'style' - Style for line,
'attributes' - Attributes to place inside polyline tag.
Parameters:
$whichDataSet - Which set of data to draw. This is the index of the data array to be used.
Returns:
BOOLEAN FALSE if no style is defined for the data set selected.