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

SVG::TT::Graph::Line

0.3.1

Create presentation quality SVG line graphs easily

Synopsis

  require 'SVG/Graph/Line'

  fields = %w(Jan Feb Mar);
  data_sales_02 = %w(12 45 21);
  data_sales_03 = %w(15 30 40);
  graph = SVG::Graph::Line.new({
          :height => 500,
          :width => 300,
    :fields => fields,
  })

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

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

Description

This object aims to allow you to easily create high quality SVG line graphs. You can either use the default style sheet or supply your own. Either way there are many options which can be configured to give you control over how the graph is generated - with or without a key, data elements at each point, title, subtitle etc.

Notes

The default stylesheet handles upto 10 data sets, if you use more you must create your own stylesheet and add the additional settings for the extra data sets. You will know if you go over 10 data sets as they will have no style and be in black.

See also

Methods
calculate_left_margin    draw_data    get_css    get_x_labels    get_y_labels    new    right_align    set_defaults    top_align    top_font   
Attributes
area_fill  [RW] 

Fill in the area under the plot if true

show_data_points  [RW] 

Show a small circle on the graph where the line goes from one point to the next.

stacked  [RW] 
 Accumulates each data set. (i.e. Each point increased by sum of

all previous series at same point). Default is 0, set to ‘1’ to show.

Public Class methods
new(config)
  require 'SVG/Graph/Line'

  # Field names along the X axis
  fields = %w(Jan Feb Mar);
  graph = SVG::Graph::Line.new({
    # Required
    :fields => fields,

    # Optional - defaults shown
    :height            => '500',
    :width             => '300',
    'show_data_points: => true,
    :show_data_values  => true,
    :stacked           => false,
    :min_scale_value   => false,
    :area_fill         => false,
    :show_x_labels     => true,
    :stagger_x_labels  => false,
    :rotate_x_labels   => false,
    :show_y_labels     => true,
    :scale_integers    => false,
    :scale_divisions   => 20,

    :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                   => false,
    :key_position          => :right,
    # Optional - defaults to using internal stylesheet
    :style_sheet       => '/includes/graph.css',
  });

The constructor takes a hash reference, fields (the names for each field on the X axis) MUST be set, all other values are defaulted to those shown above - with the exception of style_sheet which defaults to using the internal style sheet.

Public Instance methods
set_defaults()
get_x_labels()
calculate_left_margin()
get_y_labels()
right_align()
top_align()
This method is also aliased as top_font
top_font()

Alias for top_align

draw_data()
get_css()