Class: SVG::Graph::Graph

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

- SVG::Graph::BarHorizontal
- SVG::Graph::Bar
- SVG::Graph::Line
- SVG::Graph::Pie
- SVG::Graph::Plot
- SVG::Graph::TimeSeries

## Author

Sean E. Russell <serATgermaneHYPHENsoftwareDOTcom>

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

### Methods

add_data add_popup burn calculate_bottom_margin calculate_left_margin calculate_right_margin calculate_top_margin clear_data draw_graph draw_legend draw_titles draw_x_guidelines draw_x_labels draw_y_guidelines draw_y_labels field_height field_width init_with keys make_datapoint_text max_y_label_width_px new sort x_label_offset y_label_offset

### 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

new( config )

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

add_data(conf)

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'
      })

burn()

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

clear_data()

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

add_popup( x, y, label )

Adds pop-up point information to a graph.

calculate_bottom_margin()

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

calculate_left_margin()

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

calculate_right_margin()

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

calculate_top_margin()

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

draw_graph()

Draws the background, axis, and labels.

draw_legend()

Draws the legend on the graph

draw_titles()

Draws the graph title and subtitle

draw_x_guidelines( label_height, count )

Draws the X axis guidelines

draw_x_labels()

Draws the X axis labels

draw_y_guidelines( label_height, count )

Draws the Y axis guidelines

draw_y_labels()

Draws the Y axis labels

field_height()
field_width()
init_with(config)

Overwrite configuration options with supplied options. Used by subclasses.

keys()
make_datapoint_text( x, y, value, style="" )
max_y_label_width_px()

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

sort( *arrys )
x_label_offset( width )

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

y_label_offset( height )

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

[Validate]