Class: SVG::Graph::Plot

Inherits:
Graph
  • Object
show all
Defined in:
lib/SVG/Graph/Plot.rb

Overview

For creating SVG plots of scalar data

Synopsis

require 'SVG/Graph/Plot'

# Data sets are x,y pairs
# Note that multiple data sets can differ in length, and that the
# data in the datasets needn't be in order; they will be ordered
# by the plot along the X-axis.
projection = [
  6, 11,    0, 5,   18, 7,   1, 11,   13, 9,   1, 2,   19, 0,   3, 13,
  7, 9 
]
actual = [
  0, 18,    8, 15,    9, 4,   18, 14,   10, 2,   11, 6,  14, 12,   
  15, 6,   4, 17,   2, 12
]

graph = SVG::Graph::Plot.new({
	:height => 500,
 	:width => 300,
  :key => true,
  :scale_x_integers => true,
  :scale_y_integerrs => true,
})

graph.add_data({
	:data => projection

:title => 'Projected',

})

graph.add_data({
	:data => actual,

:title => 'Actual',

})

print graph.burn()

Description

Produces a graph of scalar data.

This object aims to allow you to easily create high quality SVG scalar plots. 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.

Examples

www.germane-software/repositories/public/SVG/test/plot.rb

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.

Unlike the other types of charts, data sets must contain x,y pairs:

[ 1, 2 ]    # A data set with 1 point: (1,2)
[ 1,2, 5,6] # A data set with 2 points: (1,2) and (5,6)

See also

  • SVG::Graph::Graph

  • SVG::Graph::BarHorizontal

  • SVG::Graph::Bar

  • SVG::Graph::Line

  • SVG::Graph::Pie

  • SVG::Graph::TimeSeries

Author

Sean E. Russell <serATgermaneHYPHENsoftwareDOTcom>

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

Direct Known Subclasses

TimeSeries

Instance Attribute Summary collapse

Attributes inherited from Graph

#add_popups, #font_size, #graph_subtitle, #graph_title, #height, #key, #key_font_size, #key_position, #min_scale_value, #no_css, #rotate_x_labels, #rotate_y_labels, #scale_divisions, #scale_integers, #show_data_values, #show_graph_subtitle, #show_graph_title, #show_x_guidelines, #show_x_labels, #show_x_title, #show_y_guidelines, #show_y_labels, #show_y_title, #stagger_x_labels, #stagger_y_labels, #step_include_first_x_label, #step_x_labels, #style_sheet, #subtitle_font_size, #title_font_size, #width, #x_label_font_size, #x_title, #x_title_font_size, #y_label_font_size, #y_title, #y_title_font_size, #y_title_text_direction

Instance Method Summary collapse

Methods inherited from Graph

#burn, #clear_data, #initialize

Constructor Details

This class inherits a constructor from SVG::Graph::Graph

Instance Attribute Details

#area_fillObject

Fill the area under the line



124
125
126
# File 'lib/SVG/Graph/Plot.rb', line 124

def area_fill
  @area_fill
end

#min_x_valueObject

Set the minimum value of the X axis



129
130
131
# File 'lib/SVG/Graph/Plot.rb', line 129

def min_x_value
  @min_x_value
end

#min_y_valueObject

Set the minimum value of the Y axis



131
132
133
# File 'lib/SVG/Graph/Plot.rb', line 131

def min_y_value
  @min_y_value
end

#scale_x_divisionsObject

Determines the scaling for the X axis divisions.

graph.scale_x_divisions = 2

would cause the graph to attempt to generate labels stepped by 2; EG: 0,2,4,6,8…



111
112
113
# File 'lib/SVG/Graph/Plot.rb', line 111

def scale_x_divisions
  @scale_x_divisions
end

#scale_x_integersObject

Make the X axis labels integers



120
121
122
# File 'lib/SVG/Graph/Plot.rb', line 120

def scale_x_integers
  @scale_x_integers
end

#scale_y_divisionsObject

Determines the scaling for the Y axis divisions.

graph.scale_y_divisions = 0.5

would cause the graph to attempt to generate labels stepped by 0.5; EG: 0, 0.5, 1, 1.5, 2, …



118
119
120
# File 'lib/SVG/Graph/Plot.rb', line 118

def scale_y_divisions
  @scale_y_divisions
end

#scale_y_integersObject

Make the Y axis labels integers



122
123
124
# File 'lib/SVG/Graph/Plot.rb', line 122

def scale_y_integers
  @scale_y_integers
end

#show_data_pointsObject

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



127
128
129
# File 'lib/SVG/Graph/Plot.rb', line 127

def show_data_points
  @show_data_points
end

Instance Method Details

#add_data(data) ⇒ Object

Adds data to the plot. The data must be in X,Y pairs; EG

[ 1, 2 ]    # A data set with 1 point: (1,2)
[ 1,2, 5,6] # A data set with 2 points: (1,2) and (5,6)


137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/SVG/Graph/Plot.rb', line 137

def add_data data
  @data = [] unless @data

  raise "No data provided by #{conf.inspect}" unless data[:data] and
  data[:data].kind_of? Array
  raise "Data supplied must be x,y pairs!  "+
    "The data provided contained an odd set of "+
    "data points" unless data[:data].length % 2 == 0
  return if data[:data].length == 0

  x = []
  y = []
  data[:data].each_index {|i|
    (i%2 == 0 ? x : y) << data[:data][i]
  }
  sort( x, y )
  data[:data] = [x,y]
  @data << data
end

#set_defaultsObject

In addition to the defaults set by Graph::initialize, sets

show_data_values

true

show_data_points

true

area_fill

false

stacked

false



95
96
97
98
99
100
101
102
103
# File 'lib/SVG/Graph/Plot.rb', line 95

def set_defaults
  init_with(
            :show_data_values  => true,
            :show_data_points  => true,
            :area_fill         => false,
            :stacked           => false
           )
           self.top_align = self.right_align = self.top_font = self.right_font = 1
end