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. FIXME: This process should be documented.

  module SVG::Graph::Graph_Type
    include SVG::Graph

    def set_defaults
      init_with( { :key => 'value' } )
    end
    def _init
      # any testing you want to do
    end
  end

In your script…

  require 'SVG/Graph/Graph_Type'

  width = 500
  height = 300
  fields = %w{ field_1 field_2 field_3 )
  graph = SVG::Graph::Graph_Type.new( {
    :fields => fields,
    # ... other config options
    :height => height
  })

  data = [ 23, 56, 32 ]
  graph.add_data( {
    :data => data,
    :title => 'Sales 2002'
  })

  config_value = graph.config_option
  graph.config_option = config_value
  graph.compress = true

  print "Content-type: image/svg+xml\r\n"
  print graph.burn

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
add_data    burn    clear_data    new   
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 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.

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.

Included modules
REXML
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
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'
  })
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
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