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
Instance Attribute Summary collapse
-
#add_popups ⇒ Object
Add popups for the data points on some graphs, default is false.
-
#font_size ⇒ Object
Set the font size (in points) of the data point labels.
-
#graph_subtitle ⇒ Object
What the subtitle on the graph should be.
-
#graph_title ⇒ Object
What the title on the graph should be.
-
#height ⇒ Object
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.
-
#key ⇒ Object
Whether to show a key (legend), defaults to true, set to false if you want to hide it.
-
#key_font_size ⇒ Object
Set the key font size.
-
#key_position ⇒ Object
Where the key should be positioned, defaults to :right, set to :bottom if you want to move it.
-
#min_scale_value ⇒ Object
The point at which the Y axis starts, defaults to nil, if set to nil it will default to the minimum data value.
-
#no_css ⇒ Object
Do not use CSS if set to true.
-
#number_format ⇒ Object
Number format values and Y axis representation like 1.2345667 represent as 1.23, Any valid format accepted by sprintf can be specified.
-
#popup_radius ⇒ Object
Customize popup radius.
-
#rotate_x_labels ⇒ Object
This turns the X axis labels by 90 degrees.
-
#rotate_y_labels ⇒ Object
This turns the Y axis labels by 90 degrees.
-
#scale_divisions ⇒ Object
This defines the gap between markers on the Y axis, default is a 10th of the max_value, e.g.
-
#scale_integers ⇒ Object
Ensures only whole numbers are used as the scale divisions.
-
#show_data_values ⇒ Object
(Bool) Show the value of each element of data on the graph.
-
#show_graph_subtitle ⇒ Object
Whether to show a subtitle on the graph, defaults to false, set to true to show.
-
#show_graph_title ⇒ Object
Whether to show a title on the graph, defaults to false, set to true to show.
-
#show_x_guidelines ⇒ Object
Show guidelines for the X axis, default is false.
-
#show_x_labels ⇒ Object
Whether to show labels on the X axis or not, defaults to true, set to false if you want to turn them off.
-
#show_x_title ⇒ Object
Whether to show the title under the X axis labels, default is false, set to true to show.
-
#show_y_guidelines ⇒ Object
Show guidelines for the Y axis, default is true.
-
#show_y_labels ⇒ Object
Whether to show labels on the Y axis or not, defaults to true, set to false if you want to turn them off.
-
#show_y_title ⇒ Object
Whether to show the title under the Y axis labels, default is false, set to true to show.
-
#stagger_x_labels ⇒ Object
This puts the X labels at alternative levels so if they are long field names they will not overlap so easily.
-
#stagger_y_labels ⇒ Object
This puts the Y labels at alternative levels so if they are long field names they will not overlap so easily.
-
#step_include_first_x_label ⇒ Object
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).
-
#step_x_labels ⇒ Object
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.
-
#style_sheet ⇒ Object
Set the path to an external stylesheet, set to ” if you want to revert back to using the defaut internal version.
-
#subtitle_font_size ⇒ Object
Set the subtitle font size.
-
#title_font_size ⇒ Object
Set the title font size.
-
#width ⇒ Object
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.
-
#x_axis_position ⇒ Object
By default (nil/undefined) the x-axis is at the bottom of the graph.
-
#x_label_font_size ⇒ Object
Set the font size of the X axis labels.
-
#x_title ⇒ Object
What the title under X axis should be, e.g.
-
#x_title_font_size ⇒ Object
Set the font size of the X axis title.
-
#x_title_location ⇒ Object
Where the x_title should be positioned, either in the :middle of the axis or at the :end of the axis.
-
#y_axis_position ⇒ Object
By default (nil/undefined) the y-axis is the left border of the graph.
-
#y_label_font_size ⇒ Object
Set the font size of the Y axis labels.
-
#y_title ⇒ Object
What the title under Y axis should be, e.g.
-
#y_title_font_size ⇒ Object
Set the font size of the Y axis title.
-
#y_title_location ⇒ Object
Where the y_title should be positioned, either in the :middle of the axis or at the :end of the axis.
-
#y_title_text_direction ⇒ Object
Aligns writing mode for Y axis label.
Instance Method Summary collapse
-
#add_data(conf) ⇒ Object
This method allows you do add data to the graph object.
-
#burn ⇒ Object
This method processes the template with the data and config which has been set and returns the resulting SVG.
-
#burn_svg_only ⇒ String
Burns the graph but returns only the <svg> node as String without the Doctype and XML Declaration.
-
#clear_data ⇒ Object
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.
-
#initialize(config) ⇒ Graph
constructor
Initialize the graph object with the graph settings.
Constructor Details
#initialize(config) ⇒ Graph
Initialize the graph object with the graph settings. You won’t instantiate this class directly; see the subclass for options.
- width
-
500
- height
-
300
- x_axis_position
-
nil
- y_axis_position
-
nil
- 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’
- x_title_location
-
:middle | :end
- show_y_title
-
false
- y_title_text_direction
-
:bt | :tb
- y_title
-
‘Y Scale’
- y_title_location
-
:middle | :end
- 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
- number_format
-
‘%.2f’
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 157 158 159 160 161 162 163 164 |
# File 'lib/SVG/Graph/Graph.rb', line 104 def initialize( config ) @config = config @data = [] #self.top_align = self.top_font = 0 #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, :x_axis_position => nil, :y_axis_position => nil, :min_scale_value => nil, :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', :x_title_location => :middle, # or :end :show_y_title => false, :y_title_text_direction => :bt, # other option is :tb :y_title => 'Y Scale', :y_title_location => :middle, # or :end :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, :y_label_font_size =>12, :x_title_font_size =>14, :y_title_font_size =>14, :key_font_size =>10, :no_css =>false, :add_popups =>false, :number_format => '%.2f' }) set_defaults if self.respond_to? :set_defaults init_with config end |
Instance Attribute Details
#add_popups ⇒ Object
Add popups for the data points on some graphs, default is false.
394 395 396 |
# File 'lib/SVG/Graph/Graph.rb', line 394 def add_popups @add_popups end |
#font_size ⇒ Object
Set the font size (in points) of the data point labels. Defaults to 12.
363 364 365 |
# File 'lib/SVG/Graph/Graph.rb', line 363 def font_size @font_size end |
#graph_subtitle ⇒ Object
What the subtitle on the graph should be.
354 355 356 |
# File 'lib/SVG/Graph/Graph.rb', line 354 def graph_subtitle @graph_subtitle end |
#graph_title ⇒ Object
What the title on the graph should be.
349 350 351 |
# File 'lib/SVG/Graph/Graph.rb', line 349 def graph_title @graph_title end |
#height ⇒ Object
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.
254 255 256 |
# File 'lib/SVG/Graph/Graph.rb', line 254 def height @height end |
#key ⇒ Object
Whether to show a key (legend), defaults to true, set to
false if you want to hide it.
357 358 359 |
# File 'lib/SVG/Graph/Graph.rb', line 357 def key @key end |
#key_font_size ⇒ Object
Set the key font size. Defaults to 10.
384 385 386 |
# File 'lib/SVG/Graph/Graph.rb', line 384 def key_font_size @key_font_size end |
#key_position ⇒ Object
Where the key should be positioned, defaults to
:right, set to :bottom if you want to move it.
360 361 362 |
# File 'lib/SVG/Graph/Graph.rb', line 360 def key_position @key_position end |
#min_scale_value ⇒ Object
The point at which the Y axis starts, defaults to nil,
if set to nil it will default to the minimum data value.
283 284 285 |
# File 'lib/SVG/Graph/Graph.rb', line 283 def min_scale_value @min_scale_value end |
#no_css ⇒ Object
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.
392 393 394 |
# File 'lib/SVG/Graph/Graph.rb', line 392 def no_css @no_css end |
#number_format ⇒ Object
Number format values and Y axis representation like 1.2345667 represent as 1.23, Any valid format accepted by sprintf can be specified. If you don’t want to change the format in any way you can use “%s”. Defaults to “%.2f”
400 401 402 |
# File 'lib/SVG/Graph/Graph.rb', line 400 def number_format @number_format end |
#popup_radius ⇒ Object
Customize popup radius
396 397 398 |
# File 'lib/SVG/Graph/Graph.rb', line 396 def popup_radius @popup_radius end |
#rotate_x_labels ⇒ Object
This turns the X axis labels by 90 degrees.
Default is false, to turn on set to true.
297 298 299 |
# File 'lib/SVG/Graph/Graph.rb', line 297 def rotate_x_labels @rotate_x_labels end |
#rotate_y_labels ⇒ Object
This turns the Y axis labels by 90 degrees.
Default is true, to turn on set to false.
300 301 302 |
# File 'lib/SVG/Graph/Graph.rb', line 300 def rotate_y_labels @rotate_y_labels end |
#scale_divisions ⇒ Object
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.
324 325 326 |
# File 'lib/SVG/Graph/Graph.rb', line 324 def scale_divisions @scale_divisions end |
#scale_integers ⇒ Object
Ensures only whole numbers are used as the scale divisions.
Default is false, to turn on set to true. This has no effect if
scale divisions are less than 1.
318 319 320 |
# File 'lib/SVG/Graph/Graph.rb', line 318 def scale_integers @scale_integers end |
#show_data_values ⇒ Object
(Bool) Show the value of each element of data on the graph
267 268 269 |
# File 'lib/SVG/Graph/Graph.rb', line 267 def show_data_values @show_data_values end |
#show_graph_subtitle ⇒ Object
Whether to show a subtitle on the graph, defaults
to false, set to true to show.
352 353 354 |
# File 'lib/SVG/Graph/Graph.rb', line 352 def show_graph_subtitle @show_graph_subtitle end |
#show_graph_title ⇒ Object
Whether to show a title on the graph, defaults
to false, set to true to show.
347 348 349 |
# File 'lib/SVG/Graph/Graph.rb', line 347 def show_graph_title @show_graph_title end |
#show_x_guidelines ⇒ Object
Show guidelines for the X axis, default is false
386 387 388 |
# File 'lib/SVG/Graph/Graph.rb', line 386 def show_x_guidelines @show_x_guidelines end |
#show_x_labels ⇒ Object
Whether to show labels on the X axis or not, defaults
to true, set to false if you want to turn them off.
286 287 288 |
# File 'lib/SVG/Graph/Graph.rb', line 286 def show_x_labels @show_x_labels end |
#show_x_title ⇒ Object
Whether to show the title under the X axis labels,
default is false, set to true to show.
327 328 329 |
# File 'lib/SVG/Graph/Graph.rb', line 327 def show_x_title @show_x_title end |
#show_y_guidelines ⇒ Object
Show guidelines for the Y axis, default is true
388 389 390 |
# File 'lib/SVG/Graph/Graph.rb', line 388 def show_y_guidelines @show_y_guidelines end |
#show_y_labels ⇒ Object
Whether to show labels on the Y axis or not, defaults
to true, set to false if you want to turn them off.
314 315 316 |
# File 'lib/SVG/Graph/Graph.rb', line 314 def show_y_labels @show_y_labels end |
#show_y_title ⇒ Object
Whether to show the title under the Y axis labels,
default is false, set to true to show.
335 336 337 |
# File 'lib/SVG/Graph/Graph.rb', line 335 def show_y_title @show_y_title end |
#stagger_x_labels ⇒ Object
This puts the X labels at alternative levels so if they
are long field names they will not overlap so easily.
Default is false, to turn on set to true.
290 291 292 |
# File 'lib/SVG/Graph/Graph.rb', line 290 def stagger_x_labels @stagger_x_labels end |
#stagger_y_labels ⇒ Object
This puts the Y labels at alternative levels so if they
are long field names they will not overlap so easily.
Default is false, to turn on set to true.
294 295 296 |
# File 'lib/SVG/Graph/Graph.rb', line 294 def stagger_y_labels @stagger_y_labels end |
#step_include_first_x_label ⇒ Object
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).
311 312 313 |
# File 'lib/SVG/Graph/Graph.rb', line 311 def step_include_first_x_label @step_include_first_x_label end |
#step_x_labels ⇒ Object
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.
306 307 308 |
# File 'lib/SVG/Graph/Graph.rb', line 306 def step_x_labels @step_x_labels end |
#style_sheet ⇒ Object
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.
265 266 267 |
# File 'lib/SVG/Graph/Graph.rb', line 265 def style_sheet @style_sheet end |
#subtitle_font_size ⇒ Object
Set the subtitle font size. Defaults to 14.
381 382 383 |
# File 'lib/SVG/Graph/Graph.rb', line 381 def subtitle_font_size @subtitle_font_size end |
#title_font_size ⇒ Object
Set the title font size. Defaults to 16.
378 379 380 |
# File 'lib/SVG/Graph/Graph.rb', line 378 def title_font_size @title_font_size end |
#width ⇒ Object
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.
258 259 260 |
# File 'lib/SVG/Graph/Graph.rb', line 258 def width @width end |
#x_axis_position ⇒ Object
By default (nil/undefined) the x-axis is at the bottom of the graph.
With this property a custom position for the x-axis can be defined.
Valid values are between :min_scale_value and maximum value of the
data.
Default: nil
273 274 275 |
# File 'lib/SVG/Graph/Graph.rb', line 273 def x_axis_position @x_axis_position end |
#x_label_font_size ⇒ Object
Set the font size of the X axis labels. Defaults to 12.
366 367 368 |
# File 'lib/SVG/Graph/Graph.rb', line 366 def x_label_font_size @x_label_font_size end |
#x_title ⇒ Object
What the title under X axis should be, e.g. ‘Months’.
329 330 331 |
# File 'lib/SVG/Graph/Graph.rb', line 329 def x_title @x_title end |
#x_title_font_size ⇒ Object
Set the font size of the X axis title. Defaults to 14.
369 370 371 |
# File 'lib/SVG/Graph/Graph.rb', line 369 def x_title_font_size @x_title_font_size end |
#x_title_location ⇒ Object
Where the x_title should be positioned, either in the :middle of the axis or
at the :end of the axis. Defaults to :middle
332 333 334 |
# File 'lib/SVG/Graph/Graph.rb', line 332 def x_title_location @x_title_location end |
#y_axis_position ⇒ Object
By default (nil/undefined) the y-axis is the left border of the graph.
With this property a custom position for the y-axis can be defined.
Valid values are any values in the range of x-values (in case of a
Plot) or any of the :fields values (in case of Line/Bar Graphs, note
the '==' operator is used to find at which value to draw the axis).
Default: nil
280 281 282 |
# File 'lib/SVG/Graph/Graph.rb', line 280 def y_axis_position @y_axis_position end |
#y_label_font_size ⇒ Object
Set the font size of the Y axis labels. Defaults to 12.
372 373 374 |
# File 'lib/SVG/Graph/Graph.rb', line 372 def y_label_font_size @y_label_font_size end |
#y_title ⇒ Object
What the title under Y axis should be, e.g. ‘Sales in thousands’.
341 342 343 |
# File 'lib/SVG/Graph/Graph.rb', line 341 def y_title @y_title end |
#y_title_font_size ⇒ Object
Set the font size of the Y axis title. Defaults to 14.
375 376 377 |
# File 'lib/SVG/Graph/Graph.rb', line 375 def y_title_font_size @y_title_font_size end |
#y_title_location ⇒ Object
Where the y_title should be positioned, either in the :middle of the axis or
at the :end of the axis. Defaults to :middle
344 345 346 |
# File 'lib/SVG/Graph/Graph.rb', line 344 def y_title_location @y_title_location end |
#y_title_text_direction ⇒ Object
Aligns writing mode for Y axis label.
Defaults to :bt (Bottom to Top).
Change to :tb (Top to Bottom) to reverse.
339 340 341 |
# File 'lib/SVG/Graph/Graph.rb', line 339 def y_title_text_direction @y_title_text_direction end |
Instance Method Details
#add_data(conf) ⇒ Object
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'
})
176 177 178 179 180 181 182 183 184 |
# File 'lib/SVG/Graph/Graph.rb', line 176 def add_data conf @data = [] unless (defined? @data and !@data.nil?) if conf[:data] and conf[:data].kind_of? Array @data << conf else raise "No data provided by #{conf.inspect}" end end |
#burn ⇒ Object
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
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/SVG/Graph/Graph.rb', line 204 def burn raise "No data available" unless @data.size > 0 start_svg calculate_graph_dimensions @foreground = Element.new( "g" ) draw_graph draw_titles draw_legend draw_data # this method needs to be implemented by child classes @graph.add_element( @foreground ) style data = "" @doc.write( data, 0 ) if @config[:compress] if defined?(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 |
#burn_svg_only ⇒ String
Burns the graph but returns only the <svg> node as String without the Doctype and XML Declaration. This allows easy integration into existing xml documents.
240 241 242 243 244 245 246 247 248 |
# File 'lib/SVG/Graph/Graph.rb', line 240 def burn_svg_only # initialize all instance variables by burning the graph burn f = REXML::Formatters::Pretty.new(0) f.compact = true out = '' f.write(@root, out) return out end |
#clear_data ⇒ Object
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
191 192 193 |
# File 'lib/SVG/Graph/Graph.rb', line 191 def clear_data @data = [] end |