0.4.0
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
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.