Class: SVG::Graph::Plot
- Inherits:
-
SVG::Graph
- Object
- SVG::Graph
- SVG::Graph::Plot
- 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_integers => 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 12 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 12 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)
Additional possible notation
[ [1,2], 5,6] # A data set with 2 points: (1,2) and (5,6), mixed notation
[ [1,2], [5,6]] # A data set with 2 points: (1,2) and (5,6), nested array
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
Constant Summary
Constants included from SVG::Graph
Instance Attribute Summary collapse
-
#area_fill ⇒ Object
Fill the area under the line, default: false.
-
#max_x_value ⇒ Object
Set the maximum value of the X axis, if nil the maximum from data is chosen, default: nil.
-
#max_y_value ⇒ Object
Set the maximum value of the Y axis, if nil the maximum from data is chosen, default: nil.
-
#min_x_value ⇒ Object
Set the minimum value of the X axis, if nil the minimum from data is chosen, default: nil.
-
#min_y_value ⇒ Object
Set the minimum value of the Y axis, if nil the minimum from data is chosen, default: nil.
-
#round_popups ⇒ Object
Round value of data points in popups to integer, default: true.
-
#scale_x_divisions ⇒ Object
Determines the scaling for the X axis divisions.
-
#scale_x_integers ⇒ Object
Make the X axis labels integers, default: false.
-
#scale_y_divisions ⇒ Object
Determines the scaling for the Y axis divisions.
-
#scale_y_integers ⇒ Object
Make the Y axis labels integers, default: false.
-
#show_data_points ⇒ Object
Show a small circle on the graph where the line goes from one point to the next.
-
#show_lines ⇒ Object
Show lines connecting data points, default: true.
Instance Method Summary collapse
-
#add_data(conf) ⇒ Object
Adds data to the plot.
-
#set_defaults ⇒ Object
In addition to the defaults set by Graph::initialize, sets [show_data_points] true [area_fill] false [stacked] false, will not have any effect if true [show_lines] true [round_popups] true.
Instance Attribute Details
#area_fill ⇒ Object
Fill the area under the line, default: false
133 134 135 |
# File 'lib/SVG/Graph/Plot.rb', line 133 def area_fill @area_fill end |
#max_x_value ⇒ Object
Set the maximum value of the X axis, if nil the maximum from data is chosen, default: nil
140 141 142 |
# File 'lib/SVG/Graph/Plot.rb', line 140 def max_x_value @max_x_value end |
#max_y_value ⇒ Object
Set the maximum value of the Y axis, if nil the maximum from data is chosen, default: nil
144 145 146 |
# File 'lib/SVG/Graph/Plot.rb', line 144 def max_y_value @max_y_value end |
#min_x_value ⇒ Object
Set the minimum value of the X axis, if nil the minimum from data is chosen, default: nil
138 139 140 |
# File 'lib/SVG/Graph/Plot.rb', line 138 def min_x_value @min_x_value end |
#min_y_value ⇒ Object
Set the minimum value of the Y axis, if nil the minimum from data is chosen, default: nil
142 143 144 |
# File 'lib/SVG/Graph/Plot.rb', line 142 def min_y_value @min_y_value end |
#round_popups ⇒ Object
Round value of data points in popups to integer, default: true
148 149 150 |
# File 'lib/SVG/Graph/Plot.rb', line 148 def round_popups @round_popups end |
#scale_x_divisions ⇒ Object
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… default is automatic such that there are 10 labels
119 120 121 |
# File 'lib/SVG/Graph/Plot.rb', line 119 def scale_x_divisions @scale_x_divisions end |
#scale_x_integers ⇒ Object
Make the X axis labels integers, default: false
129 130 131 |
# File 'lib/SVG/Graph/Plot.rb', line 129 def scale_x_integers @scale_x_integers end |
#scale_y_divisions ⇒ Object
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, … default is automatic such that there are 10 labels
127 128 129 |
# File 'lib/SVG/Graph/Plot.rb', line 127 def scale_y_divisions @scale_y_divisions end |
#scale_y_integers ⇒ Object
Make the Y axis labels integers, default: false
131 132 133 |
# File 'lib/SVG/Graph/Plot.rb', line 131 def scale_y_integers @scale_y_integers end |
#show_data_points ⇒ Object
Show a small circle on the graph where the line goes from one point to the next. default: true
136 137 138 |
# File 'lib/SVG/Graph/Plot.rb', line 136 def show_data_points @show_data_points end |
#show_lines ⇒ Object
Show lines connecting data points, default: true
146 147 148 |
# File 'lib/SVG/Graph/Plot.rb', line 146 def show_lines @show_lines end |
Instance Method Details
#add_data(conf) ⇒ Object
Adds data to the plot. The data must be in X,Y pairs; EG
data_set1 = [ 1, 2 ] # A data set with 1 point: (1,2)
data_set2 = [ 1,2, 5,6] # A data set with 2 points: (1,2) and (5,6)
It’s also supported to supply nested array or a mix (flatten is applied to the array); EG
data_set2 = [[1,2], 5,6]
or
data_set2 = [[1,2], [5,6]]
graph.add_data({
:data => data_set1,
:title => 'single point'
})
graph.add_data({
:data => data_set2,
:title => 'two points'
})
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/SVG/Graph/Plot.rb', line 173 def add_data(conf) @data ||= [] raise "No data provided by #{conf.inspect}" unless conf[:data].is_a?(Array) # support array of arrays and flatten it conf[:data] = conf[:data].flatten # check that we have pairs of values raise "Data supplied must be x,y pairs! "+ "The data provided contained an odd set of "+ "data points" unless conf[:data].length % 2 == 0 # remove nil values conf[:data] = conf[:data].compact return if conf[:data].length.zero? add_data_init_or_check_optional_keys(conf, conf[:data].size / 2) x = [] y = [] conf[:data].each_index {|i| (i%2 == 0 ? x : y) << conf[:data][i] } sort(x, y, conf[:description], conf[:shape], conf[:url]) conf[:data] = [x, y] # at the end data looks like: # [ # [all x values], # [all y values] # ] @data << conf end |
#set_defaults ⇒ Object
In addition to the defaults set by Graph::initialize, sets
- show_data_points
-
true
- area_fill
-
false
- stacked
-
false, will not have any effect if true
- show_lines
-
true
- round_popups
-
true
100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/SVG/Graph/Plot.rb', line 100 def set_defaults init_with( :show_data_points => true, :area_fill => false, :stacked => false, :show_lines => true, :round_popups => true, :scale_x_integers => false, :scale_y_integers => false, ) end |