Class: SVG::Graph::Graph
- Inherits:
-
Object
- Object
- SVG::Graph::Graph
- Includes:
- REXML
- Defined in:
- lib/SVG/Graph/Graph.rb
Overview
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.
For examples of how to subclass this class, see the existing specific subclasses, such as SVG::Graph::Pie.
Examples
For examples of how to use this package, see either the test files, or the documentation for the specific class you want to use.
- file:test/plot.rb
- file:test/single.rb
- file:test/test.rb
- file:test/timeseries.rb
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
- SVG::Graph::BarHorizontal
- SVG::Graph::Bar
- SVG::Graph::Line
- SVG::Graph::Pie
- SVG::Graph::Plot
- SVG::Graph::TimeSeries
Author
Sean E. Russell <serATgermaneHYPHENsoftwareDOTcom>
Copyright 2004 Sean E. Russell This software is available under the Ruby license[LICENSE.txt]
Constant Summary
- KEY_BOX_SIZE =
12
Instance Attribute Summary
-
- (Object) add_popups
Add popups for the data points on some graphs.
-
- (Object) font_size
Set the font size (in points) of the data point labels.
-
- (Object) graph_subtitle
What the subtitle on the graph should be.
-
- (Object) graph_title
What the title on the graph should be.
-
- (Object) height
Set the height of the graph box, this is the total height.
-
- (Object) key
Whether to show a key, defaults to false, set to.
-
- (Object) key_font_size
Set the key font size.
-
- (Object) key_position
Where the key should be positioned, defaults to.
-
- (Object) min_scale_value
The point at which the Y axis starts, defaults to ‘0’,.
-
- (Object) no_css
Do not use CSS if set to true.
-
- (Object) right_align
Returns the value of attribute right_align.
-
- (Object) right_font
Returns the value of attribute right_font.
-
- (Object) rotate_x_labels
This turns the X axis labels by 90 degrees.
-
- (Object) rotate_y_labels
This turns the Y axis labels by 90 degrees.
-
- (Object) scale_divisions
This defines the gap between markers on the Y axis,.
-
- (Object) scale_integers
Ensures only whole numbers are used as the scale divisions.
-
- (Object) show_data_values
(Bool) Show the value of each element of data on the graph.
-
- (Object) show_graph_subtitle
Whether to show a subtitle on the graph, defaults.
-
- (Object) show_graph_title
Whether to show a title on the graph, defaults.
-
- (Object) show_x_guidelines
Show guidelines for the X axis.
-
- (Object) show_x_labels
Whether to show labels on the X axis or not, defaults.
-
- (Object) show_x_title
Whether to show the title under the X axis labels,.
-
- (Object) show_y_guidelines
Show guidelines for the Y axis.
-
- (Object) show_y_labels
Whether to show labels on the Y axis or not, defaults.
-
- (Object) show_y_title
Whether to show the title under the Y axis labels,.
-
- (Object) stagger_x_labels
This puts the X labels at alternative levels so if they.
-
- (Object) stagger_y_labels
This puts the Y labels at alternative levels so if they.
-
- (Object) step_include_first_x_label
Whether to (when taking "steps" between X axis labels) step from .
-
- (Object) step_x_labels
How many "steps" to use between displayed X axis labels,.
-
- (Object) style_sheet
Set the path to an external stylesheet, set to ’’ if.
-
- (Object) subtitle_font_size
Set the subtitle font size.
-
- (Object) title_font_size
Set the title font size.
-
- (Object) top_align
Returns the value of attribute top_align.
-
- (Object) top_font
Returns the value of attribute top_font.
-
- (Object) width
Set the width of the graph box, this is the total width.
-
- (Object) x_label_font_size
Set the font size of the X axis labels.
-
- (Object) x_title
What the title under X axis should be, e.g.
-
- (Object) x_title_font_size
Set the font size of the X axis title.
-
- (Object) y_label_font_size
Set the font size of the Y axis labels.
-
- (Object) y_title
What the title under Y axis should be, e.g.
-
- (Object) y_title_font_size
Set the font size of the Y axis title.
-
- (Object) y_title_text_direction
Aligns writing mode for Y axis label.
Instance Method Summary
-
- (Object) add_data(conf)
This method allows you do add data to the graph object.
-
- (Object) burn
This method processes the template with the data and config which has been set and returns the resulting SVG.
-
- (Object) 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) initialize(config)
constructor
Initialize the graph object with the graph settings.
Constructor Details
- (Graph) initialize(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
- step_x_labels
- 1
- step_include_first_x_label
- true
- 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
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/SVG/Graph/Graph.rb', line 101 def initialize( config ) @config = config self.top_align = self.top_font = self.right_align = self.right_font = 0 init_with({ :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, :step_x_labels => 1, :step_include_first_x_label => true, :show_y_labels => true, :rotate_y_labels => false, :stagger_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 right :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, }) set_defaults if respond_to? :set_defaults init_with config end |
Instance Attribute Details
- (Object) add_popups
Add popups for the data points on some graphs
345 346 347 |
# File 'lib/SVG/Graph/Graph.rb', line 345 def add_popups @add_popups end |
- (Object) font_size
Set the font size (in points) of the data point labels
321 322 323 |
# File 'lib/SVG/Graph/Graph.rb', line 321 def font_size @font_size end |
- (Object) graph_subtitle
What the subtitle on the graph should be.
313 314 315 |
# File 'lib/SVG/Graph/Graph.rb', line 313 def graph_subtitle @graph_subtitle end |
- (Object) graph_title
What the title on the graph should be.
308 309 310 |
# File 'lib/SVG/Graph/Graph.rb', line 308 def graph_title @graph_title end |
- (Object) height
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.
232 233 234 |
# File 'lib/SVG/Graph/Graph.rb', line 232 def height @height end |
- (Object) key
Whether to show a key, defaults to false, set to
true if you want to show it.
316 317 318 |
# File 'lib/SVG/Graph/Graph.rb', line 316 def key @key end |
- (Object) key_font_size
Set the key font size
335 336 337 |
# File 'lib/SVG/Graph/Graph.rb', line 335 def key_font_size @key_font_size end |
- (Object) key_position
Where the key should be positioned, defaults to
:right, set to :bottom if you want to move it.
319 320 321 |
# File 'lib/SVG/Graph/Graph.rb', line 319 def key_position @key_position end |
- (Object) min_scale_value
The point at which the Y axis starts, defaults to ‘0’,
if set to nil it will default to the minimum data value.
248 249 250 |
# File 'lib/SVG/Graph/Graph.rb', line 248 def min_scale_value @min_scale_value end |
- (Object) no_css
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.
343 344 345 |
# File 'lib/SVG/Graph/Graph.rb', line 343 def no_css @no_css end |
- (Object) right_align
Returns the value of attribute right_align
362 363 364 |
# File 'lib/SVG/Graph/Graph.rb', line 362 def right_align @right_align end |
- (Object) right_font
Returns the value of attribute right_font
362 363 364 |
# File 'lib/SVG/Graph/Graph.rb', line 362 def right_font @right_font end |
- (Object) rotate_x_labels
This turns the X axis labels by 90 degrees.
Default it false, to turn on set to true.
262 263 264 |
# File 'lib/SVG/Graph/Graph.rb', line 262 def rotate_x_labels @rotate_x_labels end |
- (Object) rotate_y_labels
This turns the Y axis labels by 90 degrees.
Default it false, to turn on set to true.
265 266 267 |
# File 'lib/SVG/Graph/Graph.rb', line 265 def rotate_y_labels @rotate_y_labels end |
- (Object) scale_divisions
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.
289 290 291 |
# File 'lib/SVG/Graph/Graph.rb', line 289 def scale_divisions @scale_divisions end |
- (Object) scale_integers
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.
283 284 285 |
# File 'lib/SVG/Graph/Graph.rb', line 283 def scale_integers @scale_integers end |
- (Object) show_data_values
(Bool) Show the value of each element of data on the graph
245 246 247 |
# File 'lib/SVG/Graph/Graph.rb', line 245 def show_data_values @show_data_values end |
- (Object) show_graph_subtitle
Whether to show a subtitle on the graph, defaults
to false, set to true to show.
311 312 313 |
# File 'lib/SVG/Graph/Graph.rb', line 311 def show_graph_subtitle @show_graph_subtitle end |
- (Object) show_graph_title
Whether to show a title on the graph, defaults
to false, set to true to show.
306 307 308 |
# File 'lib/SVG/Graph/Graph.rb', line 306 def show_graph_title @show_graph_title end |
- (Object) show_x_guidelines
Show guidelines for the X axis
337 338 339 |
# File 'lib/SVG/Graph/Graph.rb', line 337 def show_x_guidelines @show_x_guidelines end |
- (Object) show_x_labels
Whether to show labels on the X axis or not, defaults
to true, set to false if you want to turn them off.
251 252 253 |
# File 'lib/SVG/Graph/Graph.rb', line 251 def show_x_labels @show_x_labels end |
- (Object) show_x_title
Whether to show the title under the X axis labels,
default is false, set to true to show.
292 293 294 |
# File 'lib/SVG/Graph/Graph.rb', line 292 def show_x_title @show_x_title end |
- (Object) show_y_guidelines
Show guidelines for the Y axis
339 340 341 |
# File 'lib/SVG/Graph/Graph.rb', line 339 def show_y_guidelines @show_y_guidelines end |
- (Object) show_y_labels
Whether to show labels on the Y axis or not, defaults
to true, set to false if you want to turn them off.
279 280 281 |
# File 'lib/SVG/Graph/Graph.rb', line 279 def show_y_labels @show_y_labels end |
- (Object) show_y_title
Whether to show the title under the Y axis labels,
default is false, set to true to show.
297 298 299 |
# File 'lib/SVG/Graph/Graph.rb', line 297 def show_y_title @show_y_title end |
- (Object) stagger_x_labels
This puts the X 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.
255 256 257 |
# File 'lib/SVG/Graph/Graph.rb', line 255 def stagger_x_labels @stagger_x_labels end |
- (Object) stagger_y_labels
This puts the Y 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.
259 260 261 |
# File 'lib/SVG/Graph/Graph.rb', line 259 def stagger_y_labels @stagger_y_labels end |
- (Object) step_include_first_x_label
Whether to (when taking "steps" between X axis labels) step from
the first label (i.e. always include the first label) or step from the X axis origin (i.e. start with a gap if step_x_labels is greater than one).
276 277 278 |
# File 'lib/SVG/Graph/Graph.rb', line 276 def step_include_first_x_label @step_include_first_x_label end |
- (Object) step_x_labels
How many "steps" to use between displayed X axis labels,
a step of one means display every label, a step of two results in every other label being displayed (label <gap> label <gap> label), a step of three results in every third label being displayed (label <gap> <gap> label <gap> <gap> label) and so on.
271 272 273 |
# File 'lib/SVG/Graph/Graph.rb', line 271 def step_x_labels @step_x_labels end |
- (Object) style_sheet
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.
243 244 245 |
# File 'lib/SVG/Graph/Graph.rb', line 243 def style_sheet @style_sheet end |
- (Object) subtitle_font_size
Set the subtitle font size
333 334 335 |
# File 'lib/SVG/Graph/Graph.rb', line 333 def subtitle_font_size @subtitle_font_size end |
- (Object) title_font_size
Set the title font size
331 332 333 |
# File 'lib/SVG/Graph/Graph.rb', line 331 def title_font_size @title_font_size end |
- (Object) top_align
Returns the value of attribute top_align
362 363 364 |
# File 'lib/SVG/Graph/Graph.rb', line 362 def top_align @top_align end |
- (Object) top_font
Returns the value of attribute top_font
362 363 364 |
# File 'lib/SVG/Graph/Graph.rb', line 362 def top_font @top_font end |
- (Object) width
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.
236 237 238 |
# File 'lib/SVG/Graph/Graph.rb', line 236 def width @width end |
- (Object) x_label_font_size
Set the font size of the X axis labels
323 324 325 |
# File 'lib/SVG/Graph/Graph.rb', line 323 def x_label_font_size @x_label_font_size end |
- (Object) x_title
What the title under X axis should be, e.g. ‘Months’.
294 295 296 |
# File 'lib/SVG/Graph/Graph.rb', line 294 def x_title @x_title end |
- (Object) x_title_font_size
Set the font size of the X axis title
325 326 327 |
# File 'lib/SVG/Graph/Graph.rb', line 325 def x_title_font_size @x_title_font_size end |
- (Object) y_label_font_size
Set the font size of the Y axis labels
327 328 329 |
# File 'lib/SVG/Graph/Graph.rb', line 327 def y_label_font_size @y_label_font_size end |
- (Object) y_title
What the title under Y axis should be, e.g. ‘Sales in thousands’.
303 304 305 |
# File 'lib/SVG/Graph/Graph.rb', line 303 def y_title @y_title end |
- (Object) y_title_font_size
Set the font size of the Y axis title
329 330 331 |
# File 'lib/SVG/Graph/Graph.rb', line 329 def y_title_font_size @y_title_font_size end |
- (Object) y_title_text_direction
Aligns writing mode for Y axis label.
Defaults to :bt (Bottom to Top). Change to :tb (Top to Bottom) to reverse.
301 302 303 |
# File 'lib/SVG/Graph/Graph.rb', line 301 def y_title_text_direction @y_title_text_direction end |
Instance Method Details
- (Object) 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' })
168 169 170 171 172 173 174 175 176 |
# File 'lib/SVG/Graph/Graph.rb', line 168 def add_data conf @data = [] unless defined? @data if conf[:data] and conf[:data].kind_of? Array @data << conf else raise "No data provided by #{conf.inspect}" end end |
- (Object) 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
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/SVG/Graph/Graph.rb', line 195 def burn raise "No data available" unless @data.size > 0 calculations if respond_to? :calculations start_svg calculate_graph_dimensions @foreground = Element.new( "g" ) draw_graph draw_titles draw_legend draw_data @graph.add_element( @foreground ) style data = "" @doc.write( data, 0 ) if @config[:compress] if @@__have_zlib inp, out = IO.pipe gz = Zlib::GzipWriter.new( out ) gz.write data gz.close data = inp.read else data << "<!-- Ruby Zlib not available for SVGZ -->"; end end return data end |
- (Object) 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
183 184 185 |
# File 'lib/SVG/Graph/Graph.rb', line 183 def clear_data @data = [] end |