Class SVG::Graph::Graph
In: temp/Graph/Graph.rb
Parent: Object

Base object for generating SVG Graphs

Synopsis

This class is only used as a superclass of specialized charts. Do not attempt to use this class directly, unless creating a new chart type.

For examples of how to subclass this class, see the existing specific subclasses, such as SVG::Graph::Pie.

Examples

For examples of how to use this package, see either the test files, or the documentation for the specific class you want to use.

  • file:test/plot.rb
  • file:test/single.rb
  • file:test/test.rb
  • file:test/timeseries.rb

Description

This package should be used as a base for creating SVG graphs.

Acknowledgements

Leo Lapworth for creating the SVG::TT::Graph package which this Ruby port is based on.

Stephen Morgan for creating the TT template and SVG.

See

Author

Sean E. Russell <serATgermaneHYPHENsoftwareDOTcom>

Copyright 2004 Sean E. Russell This software is available under the Ruby license

Methods

Included Modules

REXML

Constants

KEY_BOX_SIZE = 12

Attributes

add_popups  [RW]  Add popups for the data points on some graphs
font_size  [RW]  Set the font size (in points) of the data point labels
graph_subtitle  [RW]  What the subtitle on the graph should be.
graph_title  [RW]  What the title on the graph should be.
height  [RW]  Set the height of the graph box, this is the total height of the SVG box created - not the graph it self which auto scales to fix the space.
key  [RW]  Whether to show a key, defaults to false, set to true if you want to show it.
key_font_size  [RW]  Set the key font size
key_position  [RW]  Where the key should be positioned, defaults to :right, set to :bottom if you want to move it.
min_scale_value  [RW]  The point at which the Y axis starts, defaults to ‘0’, if set to nil it will default to the minimum data value.
no_css  [RW]  Do not use CSS if set to true. Many SVG viewers do not support CSS, but not using CSS can result in larger SVGs as well as making it impossible to change colors after the chart is generated. Defaults to false.
right_align  [RW] 
right_font  [RW] 
rotate_x_labels  [RW]  This turns the X axis labels by 90 degrees. Default it false, to turn on set to true.
rotate_y_labels  [RW]  This turns the Y axis labels by 90 degrees. Default it false, to turn on set to true.
scale_divisions  [RW]  This defines the gap between markers on the Y axis, default is a 10th of the max_value, e.g. you will have 10 markers on the Y axis. NOTE: do not set this too low - you are limited to 999 markers, after that the graph won‘t generate.
scale_integers  [RW]  Ensures only whole numbers are used as the scale divisions. Default it false, to turn on set to true. This has no effect if scale divisions are less than 1.
show_data_values  [RW]  (Bool) Show the value of each element of data on the graph
show_graph_subtitle  [RW]  Whether to show a subtitle on the graph, defaults to false, set to true to show.
show_graph_title  [RW]  Whether to show a title on the graph, defaults to false, set to true to show.
show_x_guidelines  [RW]  Show guidelines for the X axis
show_x_labels  [RW]  Whether to show labels on the X axis or not, defaults to true, set to false if you want to turn them off.
show_x_title  [RW]  Whether to show the title under the X axis labels, default is false, set to true to show.
show_y_guidelines  [RW]  Show guidelines for the Y axis
show_y_labels  [RW]  Whether to show labels on the Y axis or not, defaults to true, set to false if you want to turn them off.
show_y_title  [RW]  Whether to show the title under the Y axis labels, default is false, set to true to show.
stagger_x_labels  [RW]  This puts the X labels at alternative levels so if they are long field names they will not overlap so easily. Default it false, to turn on set to true.
stagger_y_labels  [RW]  This puts the Y labels at alternative levels so if they are long field names they will not overlap so easily. Default it false, to turn on set to true.
step_include_first_x_label  [RW]  Whether to (when taking "steps" between X axis labels) step from the first label (i.e. always include the first label) or step from the X axis origin (i.e. start with a gap if step_x_labels is greater than one).
step_x_labels  [RW]  How many "steps" to use between displayed X axis labels, a step of one means display every label, a step of two results in every other label being displayed (label <gap> label <gap> label), a step of three results in every third label being displayed (label <gap> <gap> label <gap> <gap> label) and so on.
style_sheet  [RW]  Set the path to an external stylesheet, set to ’’ if you want to revert back to using the defaut internal version.

To create an external stylesheet create a graph using the default internal version and copy the stylesheet section to an external file and edit from there.

subtitle_font_size  [RW]  Set the subtitle font size
title_font_size  [RW]  Set the title font size
top_align  [RW] 
top_font  [RW] 
width  [RW]  Set the width of the graph box, this is the total width of the SVG box created - not the graph it self which auto scales to fix the space.
x_label_font_size  [RW]  Set the font size of the X axis labels
x_title  [RW]  What the title under X axis should be, e.g. ‘Months’.
x_title_font_size  [RW]  Set the font size of the X axis title
y_label_font_size  [RW]  Set the font size of the Y axis labels
y_title  [RW]  What the title under Y axis should be, e.g. ‘Sales in thousands’.
y_title_font_size  [RW]  Set the font size of the Y axis title
y_title_text_direction  [RW]  Aligns writing mode for Y axis label. Defaults to :bt (Bottom to Top). Change to :tb (Top to Bottom) to reverse.

Public Class methods

Initialize the graph object with the graph settings. You won‘t instantiate this class directly; see the subclass for options.

width
500
height
300
show_x_guidelines
false
show_y_guidelines
true
show_data_values
true
min_scale_value
0
show_x_labels
true
stagger_x_labels
false
rotate_x_labels
false
step_x_labels
1
step_include_first_x_label
true
show_y_labels
true
rotate_y_labels
false
scale_integers
false
show_x_title
false
x_title
‘X Field names‘
show_y_title
false
y_title_text_direction
:bt
y_title
‘Y Scale‘
show_graph_title
false
graph_title
Graph Title‘
show_graph_subtitle
false
graph_subtitle
Graph Sub Title‘
key
true,
key_position
:right, # bottom or righ
font_size
12
title_font_size
16
subtitle_font_size
14
x_label_font_size
12
x_title_font_size
14
y_label_font_size
12
y_title_font_size
14
key_font_size
10
no_css
false
add_popups
false

Public Instance methods

This method allows you do add data to the graph object. It can be called several times to add more data sets in.

  data_sales_02 = [12, 45, 21];

  graph.add_data({
    :data => data_sales_02,
    :title => 'Sales 2002'
  })

This method processes the template with the data and config which has been set and returns the resulting SVG.

This method will croak unless at least one data set has been added to the graph object.

  print graph.burn

This method removes all data from the object so that you can reuse it to create a new graph but with the same config options.

  graph.clear_data

Protected Instance methods

Adds pop-up point information to a graph.

Override this (and call super) to change the margin to the bottom of the plot area. Results in @border_bottom being set.

Override this (and call super) to change the margin to the left of the plot area. Results in @border_left being set.

Override this (and call super) to change the margin to the right of the plot area. Results in @border_right being set.

Override this (and call super) to change the margin to the top of the plot area. Results in @border_top being set.

Draws the background, axis, and labels.

Draws the legend on the graph

Draws the graph title and subtitle

Draws the X axis guidelines

Draws the X axis labels

Draws the Y axis guidelines

Draws the Y axis labels

Overwrite configuration options with supplied options. Used by subclasses.

Calculates the width of the widest Y label. This will be the character height if the Y labels are rotated

Where in the X area the label is drawn Centered in the field, should be width/2. Start, 0.

Where in the Y area the label is drawn Centered in the field, should be width/2. Start, 0.

[Validate]