annotate lib/SVG/Graph/.svn/text-base/Graph.rb.svn-base @ 1174:928c0d0f624e bug_365

Close obsolete branch bug_365
author Chris Cannam
date Fri, 03 Feb 2012 14:03:51 +0000
parents 513646585e45
children
rev   line source
Chris@0 1 begin
Chris@0 2 require 'zlib'
Chris@0 3 @@__have_zlib = true
Chris@0 4 rescue
Chris@0 5 @@__have_zlib = false
Chris@0 6 end
Chris@0 7
Chris@0 8 require 'rexml/document'
Chris@0 9
Chris@0 10 module SVG
Chris@0 11 module Graph
Chris@0 12 VERSION = '@ANT_VERSION@'
Chris@0 13
Chris@0 14 # === Base object for generating SVG Graphs
Chris@0 15 #
Chris@0 16 # == Synopsis
Chris@0 17 #
Chris@0 18 # This class is only used as a superclass of specialized charts. Do not
Chris@0 19 # attempt to use this class directly, unless creating a new chart type.
Chris@0 20 #
Chris@0 21 # For examples of how to subclass this class, see the existing specific
Chris@0 22 # subclasses, such as SVG::Graph::Pie.
Chris@0 23 #
Chris@0 24 # == Examples
Chris@0 25 #
Chris@0 26 # For examples of how to use this package, see either the test files, or
Chris@0 27 # the documentation for the specific class you want to use.
Chris@0 28 #
Chris@0 29 # * file:test/plot.rb
Chris@0 30 # * file:test/single.rb
Chris@0 31 # * file:test/test.rb
Chris@0 32 # * file:test/timeseries.rb
Chris@0 33 #
Chris@0 34 # == Description
Chris@0 35 #
Chris@0 36 # This package should be used as a base for creating SVG graphs.
Chris@0 37 #
Chris@0 38 # == Acknowledgements
Chris@0 39 #
Chris@0 40 # Leo Lapworth for creating the SVG::TT::Graph package which this Ruby
Chris@0 41 # port is based on.
Chris@0 42 #
Chris@0 43 # Stephen Morgan for creating the TT template and SVG.
Chris@0 44 #
Chris@0 45 # == See
Chris@0 46 #
Chris@0 47 # * SVG::Graph::BarHorizontal
Chris@0 48 # * SVG::Graph::Bar
Chris@0 49 # * SVG::Graph::Line
Chris@0 50 # * SVG::Graph::Pie
Chris@0 51 # * SVG::Graph::Plot
Chris@0 52 # * SVG::Graph::TimeSeries
Chris@0 53 #
Chris@0 54 # == Author
Chris@0 55 #
Chris@0 56 # Sean E. Russell <serATgermaneHYPHENsoftwareDOTcom>
Chris@0 57 #
Chris@0 58 # Copyright 2004 Sean E. Russell
Chris@0 59 # This software is available under the Ruby license[LICENSE.txt]
Chris@0 60 #
Chris@0 61 class Graph
Chris@0 62 include REXML
Chris@0 63
Chris@0 64 # Initialize the graph object with the graph settings. You won't
Chris@0 65 # instantiate this class directly; see the subclass for options.
Chris@0 66 # [width] 500
Chris@0 67 # [height] 300
Chris@0 68 # [show_x_guidelines] false
Chris@0 69 # [show_y_guidelines] true
Chris@0 70 # [show_data_values] true
Chris@0 71 # [min_scale_value] 0
Chris@0 72 # [show_x_labels] true
Chris@0 73 # [stagger_x_labels] false
Chris@0 74 # [rotate_x_labels] false
Chris@0 75 # [step_x_labels] 1
Chris@0 76 # [step_include_first_x_label] true
Chris@0 77 # [show_y_labels] true
Chris@0 78 # [rotate_y_labels] false
Chris@0 79 # [scale_integers] false
Chris@0 80 # [show_x_title] false
Chris@0 81 # [x_title] 'X Field names'
Chris@0 82 # [show_y_title] false
Chris@0 83 # [y_title_text_direction] :bt
Chris@0 84 # [y_title] 'Y Scale'
Chris@0 85 # [show_graph_title] false
Chris@0 86 # [graph_title] 'Graph Title'
Chris@0 87 # [show_graph_subtitle] false
Chris@0 88 # [graph_subtitle] 'Graph Sub Title'
Chris@0 89 # [key] true,
Chris@0 90 # [key_position] :right, # bottom or righ
Chris@0 91 # [font_size] 12
Chris@0 92 # [title_font_size] 16
Chris@0 93 # [subtitle_font_size] 14
Chris@0 94 # [x_label_font_size] 12
Chris@0 95 # [x_title_font_size] 14
Chris@0 96 # [y_label_font_size] 12
Chris@0 97 # [y_title_font_size] 14
Chris@0 98 # [key_font_size] 10
Chris@0 99 # [no_css] false
Chris@0 100 # [add_popups] false
Chris@0 101 def initialize( config )
Chris@0 102 @config = config
Chris@0 103
Chris@0 104 self.top_align = self.top_font = self.right_align = self.right_font = 0
Chris@0 105
Chris@0 106 init_with({
Chris@0 107 :width => 500,
Chris@0 108 :height => 300,
Chris@0 109 :show_x_guidelines => false,
Chris@0 110 :show_y_guidelines => true,
Chris@0 111 :show_data_values => true,
Chris@0 112
Chris@0 113 # :min_scale_value => 0,
Chris@0 114
Chris@0 115 :show_x_labels => true,
Chris@0 116 :stagger_x_labels => false,
Chris@0 117 :rotate_x_labels => false,
Chris@0 118 :step_x_labels => 1,
Chris@0 119 :step_include_first_x_label => true,
Chris@0 120
Chris@0 121 :show_y_labels => true,
Chris@0 122 :rotate_y_labels => false,
Chris@0 123 :stagger_y_labels => false,
Chris@0 124 :scale_integers => false,
Chris@0 125
Chris@0 126 :show_x_title => false,
Chris@0 127 :x_title => 'X Field names',
Chris@0 128
Chris@0 129 :show_y_title => false,
Chris@0 130 :y_title_text_direction => :bt,
Chris@0 131 :y_title => 'Y Scale',
Chris@0 132
Chris@0 133 :show_graph_title => false,
Chris@0 134 :graph_title => 'Graph Title',
Chris@0 135 :show_graph_subtitle => false,
Chris@0 136 :graph_subtitle => 'Graph Sub Title',
Chris@0 137 :key => true,
Chris@0 138 :key_position => :right, # bottom or right
Chris@0 139
Chris@0 140 :font_size =>12,
Chris@0 141 :title_font_size =>16,
Chris@0 142 :subtitle_font_size =>14,
Chris@0 143 :x_label_font_size =>12,
Chris@0 144 :x_title_font_size =>14,
Chris@0 145 :y_label_font_size =>12,
Chris@0 146 :y_title_font_size =>14,
Chris@0 147 :key_font_size =>10,
Chris@0 148
Chris@0 149 :no_css =>false,
Chris@0 150 :add_popups =>false,
Chris@0 151 })
Chris@0 152
Chris@0 153 set_defaults if respond_to? :set_defaults
Chris@0 154
Chris@0 155 init_with config
Chris@0 156 end
Chris@0 157
Chris@0 158
Chris@0 159 # This method allows you do add data to the graph object.
Chris@0 160 # It can be called several times to add more data sets in.
Chris@0 161 #
Chris@0 162 # data_sales_02 = [12, 45, 21];
Chris@0 163 #
Chris@0 164 # graph.add_data({
Chris@0 165 # :data => data_sales_02,
Chris@0 166 # :title => 'Sales 2002'
Chris@0 167 # })
Chris@0 168 def add_data conf
Chris@0 169 @data = [] unless defined? @data
Chris@0 170
Chris@0 171 if conf[:data] and conf[:data].kind_of? Array
Chris@0 172 @data << conf
Chris@0 173 else
Chris@0 174 raise "No data provided by #{conf.inspect}"
Chris@0 175 end
Chris@0 176 end
Chris@0 177
Chris@0 178
Chris@0 179 # This method removes all data from the object so that you can
Chris@0 180 # reuse it to create a new graph but with the same config options.
Chris@0 181 #
Chris@0 182 # graph.clear_data
Chris@0 183 def clear_data
Chris@0 184 @data = []
Chris@0 185 end
Chris@0 186
Chris@0 187
Chris@0 188 # This method processes the template with the data and
Chris@0 189 # config which has been set and returns the resulting SVG.
Chris@0 190 #
Chris@0 191 # This method will croak unless at least one data set has
Chris@0 192 # been added to the graph object.
Chris@0 193 #
Chris@0 194 # print graph.burn
Chris@0 195 def burn
Chris@0 196 raise "No data available" unless @data.size > 0
Chris@0 197
Chris@0 198 calculations if respond_to? :calculations
Chris@0 199
Chris@0 200 start_svg
Chris@0 201 calculate_graph_dimensions
Chris@0 202 @foreground = Element.new( "g" )
Chris@0 203 draw_graph
Chris@0 204 draw_titles
Chris@0 205 draw_legend
Chris@0 206 draw_data
Chris@0 207 @graph.add_element( @foreground )
Chris@0 208 style
Chris@0 209
Chris@0 210 data = ""
Chris@0 211 @doc.write( data, 0 )
Chris@0 212
Chris@0 213 if @config[:compress]
Chris@0 214 if @@__have_zlib
Chris@0 215 inp, out = IO.pipe
Chris@0 216 gz = Zlib::GzipWriter.new( out )
Chris@0 217 gz.write data
Chris@0 218 gz.close
Chris@0 219 data = inp.read
Chris@0 220 else
Chris@0 221 data << "<!-- Ruby Zlib not available for SVGZ -->";
Chris@0 222 end
Chris@0 223 end
Chris@0 224
Chris@0 225 return data
Chris@0 226 end
Chris@0 227
Chris@0 228
Chris@0 229 # Set the height of the graph box, this is the total height
Chris@0 230 # of the SVG box created - not the graph it self which auto
Chris@0 231 # scales to fix the space.
Chris@0 232 attr_accessor :height
Chris@0 233 # Set the width of the graph box, this is the total width
Chris@0 234 # of the SVG box created - not the graph it self which auto
Chris@0 235 # scales to fix the space.
Chris@0 236 attr_accessor :width
Chris@0 237 # Set the path to an external stylesheet, set to '' if
Chris@0 238 # you want to revert back to using the defaut internal version.
Chris@0 239 #
Chris@0 240 # To create an external stylesheet create a graph using the
Chris@0 241 # default internal version and copy the stylesheet section to
Chris@0 242 # an external file and edit from there.
Chris@0 243 attr_accessor :style_sheet
Chris@0 244 # (Bool) Show the value of each element of data on the graph
Chris@0 245 attr_accessor :show_data_values
Chris@0 246 # The point at which the Y axis starts, defaults to '0',
Chris@0 247 # if set to nil it will default to the minimum data value.
Chris@0 248 attr_accessor :min_scale_value
Chris@0 249 # Whether to show labels on the X axis or not, defaults
Chris@0 250 # to true, set to false if you want to turn them off.
Chris@0 251 attr_accessor :show_x_labels
Chris@0 252 # This puts the X labels at alternative levels so if they
Chris@0 253 # are long field names they will not overlap so easily.
Chris@0 254 # Default it false, to turn on set to true.
Chris@0 255 attr_accessor :stagger_x_labels
Chris@0 256 # This puts the Y labels at alternative levels so if they
Chris@0 257 # are long field names they will not overlap so easily.
Chris@0 258 # Default it false, to turn on set to true.
Chris@0 259 attr_accessor :stagger_y_labels
Chris@0 260 # This turns the X axis labels by 90 degrees.
Chris@0 261 # Default it false, to turn on set to true.
Chris@0 262 attr_accessor :rotate_x_labels
Chris@0 263 # This turns the Y axis labels by 90 degrees.
Chris@0 264 # Default it false, to turn on set to true.
Chris@0 265 attr_accessor :rotate_y_labels
Chris@0 266 # How many "steps" to use between displayed X axis labels,
Chris@0 267 # a step of one means display every label, a step of two results
Chris@0 268 # in every other label being displayed (label <gap> label <gap> label),
Chris@0 269 # a step of three results in every third label being displayed
Chris@0 270 # (label <gap> <gap> label <gap> <gap> label) and so on.
Chris@0 271 attr_accessor :step_x_labels
Chris@0 272 # Whether to (when taking "steps" between X axis labels) step from
Chris@0 273 # the first label (i.e. always include the first label) or step from
Chris@0 274 # the X axis origin (i.e. start with a gap if step_x_labels is greater
Chris@0 275 # than one).
Chris@0 276 attr_accessor :step_include_first_x_label
Chris@0 277 # Whether to show labels on the Y axis or not, defaults
Chris@0 278 # to true, set to false if you want to turn them off.
Chris@0 279 attr_accessor :show_y_labels
Chris@0 280 # Ensures only whole numbers are used as the scale divisions.
Chris@0 281 # Default it false, to turn on set to true. This has no effect if
Chris@0 282 # scale divisions are less than 1.
Chris@0 283 attr_accessor :scale_integers
Chris@0 284 # This defines the gap between markers on the Y axis,
Chris@0 285 # default is a 10th of the max_value, e.g. you will have
Chris@0 286 # 10 markers on the Y axis. NOTE: do not set this too
Chris@0 287 # low - you are limited to 999 markers, after that the
Chris@0 288 # graph won't generate.
Chris@0 289 attr_accessor :scale_divisions
Chris@0 290 # Whether to show the title under the X axis labels,
Chris@0 291 # default is false, set to true to show.
Chris@0 292 attr_accessor :show_x_title
Chris@0 293 # What the title under X axis should be, e.g. 'Months'.
Chris@0 294 attr_accessor :x_title
Chris@0 295 # Whether to show the title under the Y axis labels,
Chris@0 296 # default is false, set to true to show.
Chris@0 297 attr_accessor :show_y_title
Chris@0 298 # Aligns writing mode for Y axis label.
Chris@0 299 # Defaults to :bt (Bottom to Top).
Chris@0 300 # Change to :tb (Top to Bottom) to reverse.
Chris@0 301 attr_accessor :y_title_text_direction
Chris@0 302 # What the title under Y axis should be, e.g. 'Sales in thousands'.
Chris@0 303 attr_accessor :y_title
Chris@0 304 # Whether to show a title on the graph, defaults
Chris@0 305 # to false, set to true to show.
Chris@0 306 attr_accessor :show_graph_title
Chris@0 307 # What the title on the graph should be.
Chris@0 308 attr_accessor :graph_title
Chris@0 309 # Whether to show a subtitle on the graph, defaults
Chris@0 310 # to false, set to true to show.
Chris@0 311 attr_accessor :show_graph_subtitle
Chris@0 312 # What the subtitle on the graph should be.
Chris@0 313 attr_accessor :graph_subtitle
Chris@0 314 # Whether to show a key, defaults to false, set to
Chris@0 315 # true if you want to show it.
Chris@0 316 attr_accessor :key
Chris@0 317 # Where the key should be positioned, defaults to
Chris@0 318 # :right, set to :bottom if you want to move it.
Chris@0 319 attr_accessor :key_position
Chris@0 320 # Set the font size (in points) of the data point labels
Chris@0 321 attr_accessor :font_size
Chris@0 322 # Set the font size of the X axis labels
Chris@0 323 attr_accessor :x_label_font_size
Chris@0 324 # Set the font size of the X axis title
Chris@0 325 attr_accessor :x_title_font_size
Chris@0 326 # Set the font size of the Y axis labels
Chris@0 327 attr_accessor :y_label_font_size
Chris@0 328 # Set the font size of the Y axis title
Chris@0 329 attr_accessor :y_title_font_size
Chris@0 330 # Set the title font size
Chris@0 331 attr_accessor :title_font_size
Chris@0 332 # Set the subtitle font size
Chris@0 333 attr_accessor :subtitle_font_size
Chris@0 334 # Set the key font size
Chris@0 335 attr_accessor :key_font_size
Chris@0 336 # Show guidelines for the X axis
Chris@0 337 attr_accessor :show_x_guidelines
Chris@0 338 # Show guidelines for the Y axis
Chris@0 339 attr_accessor :show_y_guidelines
Chris@0 340 # Do not use CSS if set to true. Many SVG viewers do not support CSS, but
Chris@0 341 # not using CSS can result in larger SVGs as well as making it impossible to
Chris@0 342 # change colors after the chart is generated. Defaults to false.
Chris@0 343 attr_accessor :no_css
Chris@0 344 # Add popups for the data points on some graphs
Chris@0 345 attr_accessor :add_popups
Chris@0 346
Chris@0 347
Chris@0 348 protected
Chris@0 349
Chris@0 350 def sort( *arrys )
Chris@0 351 sort_multiple( arrys )
Chris@0 352 end
Chris@0 353
Chris@0 354 # Overwrite configuration options with supplied options. Used
Chris@0 355 # by subclasses.
Chris@0 356 def init_with config
Chris@0 357 config.each { |key, value|
Chris@0 358 self.send((key.to_s+"=").to_sym, value ) if respond_to? key.to_sym
Chris@0 359 }
Chris@0 360 end
Chris@0 361
Chris@0 362 attr_accessor :top_align, :top_font, :right_align, :right_font
Chris@0 363
Chris@0 364 KEY_BOX_SIZE = 12
Chris@0 365
Chris@0 366 # Override this (and call super) to change the margin to the left
Chris@0 367 # of the plot area. Results in @border_left being set.
Chris@0 368 def calculate_left_margin
Chris@0 369 @border_left = 7
Chris@0 370 # Check for Y labels
Chris@0 371 max_y_label_height_px = rotate_y_labels ?
Chris@0 372 y_label_font_size :
Chris@0 373 get_y_labels.max{|a,b|
Chris@0 374 a.to_s.length<=>b.to_s.length
Chris@0 375 }.to_s.length * y_label_font_size * 0.6
Chris@0 376 @border_left += max_y_label_height_px if show_y_labels
Chris@0 377 @border_left += max_y_label_height_px + 10 if stagger_y_labels
Chris@0 378 @border_left += y_title_font_size + 5 if show_y_title
Chris@0 379 end
Chris@0 380
Chris@0 381
Chris@0 382 # Calculates the width of the widest Y label. This will be the
Chris@0 383 # character height if the Y labels are rotated
Chris@0 384 def max_y_label_width_px
Chris@0 385 return font_size if rotate_y_labels
Chris@0 386 end
Chris@0 387
Chris@0 388
Chris@0 389 # Override this (and call super) to change the margin to the right
Chris@0 390 # of the plot area. Results in @border_right being set.
Chris@0 391 def calculate_right_margin
Chris@0 392 @border_right = 7
Chris@0 393 if key and key_position == :right
Chris@0 394 val = keys.max { |a,b| a.length <=> b.length }
Chris@0 395 @border_right += val.length * key_font_size * 0.6
Chris@0 396 @border_right += KEY_BOX_SIZE
Chris@0 397 @border_right += 10 # Some padding around the box
Chris@0 398 end
Chris@0 399 end
Chris@0 400
Chris@0 401
Chris@0 402 # Override this (and call super) to change the margin to the top
Chris@0 403 # of the plot area. Results in @border_top being set.
Chris@0 404 def calculate_top_margin
Chris@0 405 @border_top = 5
Chris@0 406 @border_top += title_font_size if show_graph_title
Chris@0 407 @border_top += 5
Chris@0 408 @border_top += subtitle_font_size if show_graph_subtitle
Chris@0 409 end
Chris@0 410
Chris@0 411
Chris@0 412 # Adds pop-up point information to a graph.
Chris@0 413 def add_popup( x, y, label )
Chris@0 414 txt_width = label.length * font_size * 0.6 + 10
Chris@0 415 tx = (x+txt_width > width ? x-5 : x+5)
Chris@0 416 t = @foreground.add_element( "text", {
Chris@0 417 "x" => tx.to_s,
Chris@0 418 "y" => (y - font_size).to_s,
Chris@0 419 "visibility" => "hidden",
Chris@0 420 })
Chris@0 421 t.attributes["style"] = "fill: #000; "+
Chris@0 422 (x+txt_width > width ? "text-anchor: end;" : "text-anchor: start;")
Chris@0 423 t.text = label.to_s
Chris@0 424 t.attributes["id"] = t.object_id.to_s
Chris@0 425
Chris@0 426 @foreground.add_element( "circle", {
Chris@0 427 "cx" => x.to_s,
Chris@0 428 "cy" => y.to_s,
Chris@0 429 "r" => "10",
Chris@0 430 "style" => "opacity: 0",
Chris@0 431 "onmouseover" =>
Chris@0 432 "document.getElementById(#{t.object_id}).setAttribute('visibility', 'visible' )",
Chris@0 433 "onmouseout" =>
Chris@0 434 "document.getElementById(#{t.object_id}).setAttribute('visibility', 'hidden' )",
Chris@0 435 })
Chris@0 436
Chris@0 437 end
Chris@0 438
Chris@0 439
Chris@0 440 # Override this (and call super) to change the margin to the bottom
Chris@0 441 # of the plot area. Results in @border_bottom being set.
Chris@0 442 def calculate_bottom_margin
Chris@0 443 @border_bottom = 7
Chris@0 444 if key and key_position == :bottom
Chris@0 445 @border_bottom += @data.size * (font_size + 5)
Chris@0 446 @border_bottom += 10
Chris@0 447 end
Chris@0 448 if show_x_labels
Chris@0 449 max_x_label_height_px = (not rotate_x_labels) ?
Chris@0 450 x_label_font_size :
Chris@0 451 get_x_labels.max{|a,b|
Chris@0 452 a.to_s.length<=>b.to_s.length
Chris@0 453 }.to_s.length * x_label_font_size * 0.6
Chris@0 454 @border_bottom += max_x_label_height_px
Chris@0 455 @border_bottom += max_x_label_height_px + 10 if stagger_x_labels
Chris@0 456 end
Chris@0 457 @border_bottom += x_title_font_size + 5 if show_x_title
Chris@0 458 end
Chris@0 459
Chris@0 460
Chris@0 461 # Draws the background, axis, and labels.
Chris@0 462 def draw_graph
Chris@0 463 @graph = @root.add_element( "g", {
Chris@0 464 "transform" => "translate( #@border_left #@border_top )"
Chris@0 465 })
Chris@0 466
Chris@0 467 # Background
Chris@0 468 @graph.add_element( "rect", {
Chris@0 469 "x" => "0",
Chris@0 470 "y" => "0",
Chris@0 471 "width" => @graph_width.to_s,
Chris@0 472 "height" => @graph_height.to_s,
Chris@0 473 "class" => "graphBackground"
Chris@0 474 })
Chris@0 475
Chris@0 476 # Axis
Chris@0 477 @graph.add_element( "path", {
Chris@0 478 "d" => "M 0 0 v#@graph_height",
Chris@0 479 "class" => "axis",
Chris@0 480 "id" => "xAxis"
Chris@0 481 })
Chris@0 482 @graph.add_element( "path", {
Chris@0 483 "d" => "M 0 #@graph_height h#@graph_width",
Chris@0 484 "class" => "axis",
Chris@0 485 "id" => "yAxis"
Chris@0 486 })
Chris@0 487
Chris@0 488 draw_x_labels
Chris@0 489 draw_y_labels
Chris@0 490 end
Chris@0 491
Chris@0 492
Chris@0 493 # Where in the X area the label is drawn
Chris@0 494 # Centered in the field, should be width/2. Start, 0.
Chris@0 495 def x_label_offset( width )
Chris@0 496 0
Chris@0 497 end
Chris@0 498
Chris@0 499 def make_datapoint_text( x, y, value, style="" )
Chris@0 500 if show_data_values
Chris@0 501 @foreground.add_element( "text", {
Chris@0 502 "x" => x.to_s,
Chris@0 503 "y" => y.to_s,
Chris@0 504 "class" => "dataPointLabel",
Chris@0 505 "style" => "#{style} stroke: #fff; stroke-width: 2;"
Chris@0 506 }).text = value.to_s
Chris@0 507 text = @foreground.add_element( "text", {
Chris@0 508 "x" => x.to_s,
Chris@0 509 "y" => y.to_s,
Chris@0 510 "class" => "dataPointLabel"
Chris@0 511 })
Chris@0 512 text.text = value.to_s
Chris@0 513 text.attributes["style"] = style if style.length > 0
Chris@0 514 end
Chris@0 515 end
Chris@0 516
Chris@0 517
Chris@0 518 # Draws the X axis labels
Chris@0 519 def draw_x_labels
Chris@0 520 stagger = x_label_font_size + 5
Chris@0 521 if show_x_labels
Chris@0 522 label_width = field_width
Chris@0 523
Chris@0 524 count = 0
Chris@0 525 for label in get_x_labels
Chris@0 526 if step_include_first_x_label == true then
Chris@0 527 step = count % step_x_labels
Chris@0 528 else
Chris@0 529 step = (count + 1) % step_x_labels
Chris@0 530 end
Chris@0 531
Chris@0 532 if step == 0 then
Chris@0 533 text = @graph.add_element( "text" )
Chris@0 534 text.attributes["class"] = "xAxisLabels"
Chris@0 535 text.text = label.to_s
Chris@0 536
Chris@0 537 x = count * label_width + x_label_offset( label_width )
Chris@0 538 y = @graph_height + x_label_font_size + 3
Chris@0 539 t = 0 - (font_size / 2)
Chris@0 540
Chris@0 541 if stagger_x_labels and count % 2 == 1
Chris@0 542 y += stagger
Chris@0 543 @graph.add_element( "path", {
Chris@0 544 "d" => "M#{x} #@graph_height v#{stagger}",
Chris@0 545 "class" => "staggerGuideLine"
Chris@0 546 })
Chris@0 547 end
Chris@0 548
Chris@0 549 text.attributes["x"] = x.to_s
Chris@0 550 text.attributes["y"] = y.to_s
Chris@0 551 if rotate_x_labels
Chris@0 552 text.attributes["transform"] =
Chris@0 553 "rotate( 90 #{x} #{y-x_label_font_size} )"+
Chris@0 554 " translate( 0 -#{x_label_font_size/4} )"
Chris@0 555 text.attributes["style"] = "text-anchor: start"
Chris@0 556 else
Chris@0 557 text.attributes["style"] = "text-anchor: middle"
Chris@0 558 end
Chris@0 559 end
Chris@0 560
Chris@0 561 draw_x_guidelines( label_width, count ) if show_x_guidelines
Chris@0 562 count += 1
Chris@0 563 end
Chris@0 564 end
Chris@0 565 end
Chris@0 566
Chris@0 567
Chris@0 568 # Where in the Y area the label is drawn
Chris@0 569 # Centered in the field, should be width/2. Start, 0.
Chris@0 570 def y_label_offset( height )
Chris@0 571 0
Chris@0 572 end
Chris@0 573
Chris@0 574
Chris@0 575 def field_width
Chris@0 576 (@graph_width.to_f - font_size*2*right_font) /
Chris@0 577 (get_x_labels.length - right_align)
Chris@0 578 end
Chris@0 579
Chris@0 580
Chris@0 581 def field_height
Chris@0 582 (@graph_height.to_f - font_size*2*top_font) /
Chris@0 583 (get_y_labels.length - top_align)
Chris@0 584 end
Chris@0 585
Chris@0 586
Chris@0 587 # Draws the Y axis labels
Chris@0 588 def draw_y_labels
Chris@0 589 stagger = y_label_font_size + 5
Chris@0 590 if show_y_labels
Chris@0 591 label_height = field_height
Chris@0 592
Chris@0 593 count = 0
Chris@0 594 y_offset = @graph_height + y_label_offset( label_height )
Chris@0 595 y_offset += font_size/1.2 unless rotate_y_labels
Chris@0 596 for label in get_y_labels
Chris@0 597 y = y_offset - (label_height * count)
Chris@0 598 x = rotate_y_labels ? 0 : -3
Chris@0 599
Chris@0 600 if stagger_y_labels and count % 2 == 1
Chris@0 601 x -= stagger
Chris@0 602 @graph.add_element( "path", {
Chris@0 603 "d" => "M#{x} #{y} h#{stagger}",
Chris@0 604 "class" => "staggerGuideLine"
Chris@0 605 })
Chris@0 606 end
Chris@0 607
Chris@0 608 text = @graph.add_element( "text", {
Chris@0 609 "x" => x.to_s,
Chris@0 610 "y" => y.to_s,
Chris@0 611 "class" => "yAxisLabels"
Chris@0 612 })
Chris@0 613 text.text = label.to_s
Chris@0 614 if rotate_y_labels
Chris@0 615 text.attributes["transform"] = "translate( -#{font_size} 0 ) "+
Chris@0 616 "rotate( 90 #{x} #{y} ) "
Chris@0 617 text.attributes["style"] = "text-anchor: middle"
Chris@0 618 else
Chris@0 619 text.attributes["y"] = (y - (y_label_font_size/2)).to_s
Chris@0 620 text.attributes["style"] = "text-anchor: end"
Chris@0 621 end
Chris@0 622 draw_y_guidelines( label_height, count ) if show_y_guidelines
Chris@0 623 count += 1
Chris@0 624 end
Chris@0 625 end
Chris@0 626 end
Chris@0 627
Chris@0 628
Chris@0 629 # Draws the X axis guidelines
Chris@0 630 def draw_x_guidelines( label_height, count )
Chris@0 631 if count != 0
Chris@0 632 @graph.add_element( "path", {
Chris@0 633 "d" => "M#{label_height*count} 0 v#@graph_height",
Chris@0 634 "class" => "guideLines"
Chris@0 635 })
Chris@0 636 end
Chris@0 637 end
Chris@0 638
Chris@0 639
Chris@0 640 # Draws the Y axis guidelines
Chris@0 641 def draw_y_guidelines( label_height, count )
Chris@0 642 if count != 0
Chris@0 643 @graph.add_element( "path", {
Chris@0 644 "d" => "M0 #{@graph_height-(label_height*count)} h#@graph_width",
Chris@0 645 "class" => "guideLines"
Chris@0 646 })
Chris@0 647 end
Chris@0 648 end
Chris@0 649
Chris@0 650
Chris@0 651 # Draws the graph title and subtitle
Chris@0 652 def draw_titles
Chris@0 653 if show_graph_title
Chris@0 654 @root.add_element( "text", {
Chris@0 655 "x" => (width / 2).to_s,
Chris@0 656 "y" => (title_font_size).to_s,
Chris@0 657 "class" => "mainTitle"
Chris@0 658 }).text = graph_title.to_s
Chris@0 659 end
Chris@0 660
Chris@0 661 if show_graph_subtitle
Chris@0 662 y_subtitle = show_graph_title ?
Chris@0 663 title_font_size + 10 :
Chris@0 664 subtitle_font_size
Chris@0 665 @root.add_element("text", {
Chris@0 666 "x" => (width / 2).to_s,
Chris@0 667 "y" => (y_subtitle).to_s,
Chris@0 668 "class" => "subTitle"
Chris@0 669 }).text = graph_subtitle.to_s
Chris@0 670 end
Chris@0 671
Chris@0 672 if show_x_title
Chris@0 673 y = @graph_height + @border_top + x_title_font_size
Chris@0 674 if show_x_labels
Chris@0 675 y += x_label_font_size + 5 if stagger_x_labels
Chris@0 676 y += x_label_font_size + 5
Chris@0 677 end
Chris@0 678 x = width / 2
Chris@0 679
Chris@0 680 @root.add_element("text", {
Chris@0 681 "x" => x.to_s,
Chris@0 682 "y" => y.to_s,
Chris@0 683 "class" => "xAxisTitle",
Chris@0 684 }).text = x_title.to_s
Chris@0 685 end
Chris@0 686
Chris@0 687 if show_y_title
Chris@0 688 x = y_title_font_size + (y_title_text_direction==:bt ? 3 : -3)
Chris@0 689 y = height / 2
Chris@0 690
Chris@0 691 text = @root.add_element("text", {
Chris@0 692 "x" => x.to_s,
Chris@0 693 "y" => y.to_s,
Chris@0 694 "class" => "yAxisTitle",
Chris@0 695 })
Chris@0 696 text.text = y_title.to_s
Chris@0 697 if y_title_text_direction == :bt
Chris@0 698 text.attributes["transform"] = "rotate( -90, #{x}, #{y} )"
Chris@0 699 else
Chris@0 700 text.attributes["transform"] = "rotate( 90, #{x}, #{y} )"
Chris@0 701 end
Chris@0 702 end
Chris@0 703 end
Chris@0 704
Chris@0 705 def keys
Chris@0 706 return @data.collect{ |d| d[:title] }
Chris@0 707 end
Chris@0 708
Chris@0 709 # Draws the legend on the graph
Chris@0 710 def draw_legend
Chris@0 711 if key
Chris@0 712 group = @root.add_element( "g" )
Chris@0 713
Chris@0 714 key_count = 0
Chris@0 715 for key_name in keys
Chris@0 716 y_offset = (KEY_BOX_SIZE * key_count) + (key_count * 5)
Chris@0 717 group.add_element( "rect", {
Chris@0 718 "x" => 0.to_s,
Chris@0 719 "y" => y_offset.to_s,
Chris@0 720 "width" => KEY_BOX_SIZE.to_s,
Chris@0 721 "height" => KEY_BOX_SIZE.to_s,
Chris@0 722 "class" => "key#{key_count+1}"
Chris@0 723 })
Chris@0 724 group.add_element( "text", {
Chris@0 725 "x" => (KEY_BOX_SIZE + 5).to_s,
Chris@0 726 "y" => (y_offset + KEY_BOX_SIZE).to_s,
Chris@0 727 "class" => "keyText"
Chris@0 728 }).text = key_name.to_s
Chris@0 729 key_count += 1
Chris@0 730 end
Chris@0 731
Chris@0 732 case key_position
Chris@0 733 when :right
Chris@0 734 x_offset = @graph_width + @border_left + 10
Chris@0 735 y_offset = @border_top + 20
Chris@0 736 when :bottom
Chris@0 737 x_offset = @border_left + 20
Chris@0 738 y_offset = @border_top + @graph_height + 5
Chris@0 739 if show_x_labels
Chris@0 740 max_x_label_height_px = (not rotate_x_labels) ?
Chris@0 741 x_label_font_size :
Chris@0 742 get_x_labels.max{|a,b|
Chris@0 743 a.to_s.length<=>b.to_s.length
Chris@0 744 }.to_s.length * x_label_font_size * 0.6
Chris@0 745 x_label_font_size
Chris@0 746 y_offset += max_x_label_height_px
Chris@0 747 y_offset += max_x_label_height_px + 5 if stagger_x_labels
Chris@0 748 end
Chris@0 749 y_offset += x_title_font_size + 5 if show_x_title
Chris@0 750 end
Chris@0 751 group.attributes["transform"] = "translate(#{x_offset} #{y_offset})"
Chris@0 752 end
Chris@0 753 end
Chris@0 754
Chris@0 755
Chris@0 756 private
Chris@0 757
Chris@0 758 def sort_multiple( arrys, lo=0, hi=arrys[0].length-1 )
Chris@0 759 if lo < hi
Chris@0 760 p = partition(arrys,lo,hi)
Chris@0 761 sort_multiple(arrys, lo, p-1)
Chris@0 762 sort_multiple(arrys, p+1, hi)
Chris@0 763 end
Chris@0 764 arrys
Chris@0 765 end
Chris@0 766
Chris@0 767 def partition( arrys, lo, hi )
Chris@0 768 p = arrys[0][lo]
Chris@0 769 l = lo
Chris@0 770 z = lo+1
Chris@0 771 while z <= hi
Chris@0 772 if arrys[0][z] < p
Chris@0 773 l += 1
Chris@0 774 arrys.each { |arry| arry[z], arry[l] = arry[l], arry[z] }
Chris@0 775 end
Chris@0 776 z += 1
Chris@0 777 end
Chris@0 778 arrys.each { |arry| arry[lo], arry[l] = arry[l], arry[lo] }
Chris@0 779 l
Chris@0 780 end
Chris@0 781
Chris@0 782 def style
Chris@0 783 if no_css
Chris@0 784 styles = parse_css
Chris@0 785 @root.elements.each("//*[@class]") { |el|
Chris@0 786 cl = el.attributes["class"]
Chris@0 787 style = styles[cl]
Chris@0 788 style += el.attributes["style"] if el.attributes["style"]
Chris@0 789 el.attributes["style"] = style
Chris@0 790 }
Chris@0 791 end
Chris@0 792 end
Chris@0 793
Chris@0 794 def parse_css
Chris@0 795 css = get_style
Chris@0 796 rv = {}
Chris@0 797 while css =~ /^(\.(\w+)(?:\s*,\s*\.\w+)*)\s*\{/m
Chris@0 798 names_orig = names = $1
Chris@0 799 css = $'
Chris@0 800 css =~ /([^}]+)\}/m
Chris@0 801 content = $1
Chris@0 802 css = $'
Chris@0 803
Chris@0 804 nms = []
Chris@0 805 while names =~ /^\s*,?\s*\.(\w+)/
Chris@0 806 nms << $1
Chris@0 807 names = $'
Chris@0 808 end
Chris@0 809
Chris@0 810 content = content.tr( "\n\t", " ")
Chris@0 811 for name in nms
Chris@0 812 current = rv[name]
Chris@0 813 current = current ? current+"; "+content : content
Chris@0 814 rv[name] = current.strip.squeeze(" ")
Chris@0 815 end
Chris@0 816 end
Chris@0 817 return rv
Chris@0 818 end
Chris@0 819
Chris@0 820
Chris@0 821 # Override and place code to add defs here
Chris@0 822 def add_defs defs
Chris@0 823 end
Chris@0 824
Chris@0 825
Chris@0 826 def start_svg
Chris@0 827 # Base document
Chris@0 828 @doc = Document.new
Chris@0 829 @doc << XMLDecl.new
Chris@0 830 @doc << DocType.new( %q{svg PUBLIC "-//W3C//DTD SVG 1.0//EN" } +
Chris@0 831 %q{"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd"} )
Chris@0 832 if style_sheet && style_sheet != ''
Chris@0 833 @doc << Instruction.new( "xml-stylesheet",
Chris@0 834 %Q{href="#{style_sheet}" type="text/css"} )
Chris@0 835 end
Chris@0 836 @root = @doc.add_element( "svg", {
Chris@0 837 "width" => width.to_s,
Chris@0 838 "height" => height.to_s,
Chris@0 839 "viewBox" => "0 0 #{width} #{height}",
Chris@0 840 "xmlns" => "http://www.w3.org/2000/svg",
Chris@0 841 "xmlns:xlink" => "http://www.w3.org/1999/xlink",
Chris@0 842 "xmlns:a3" => "http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/",
Chris@0 843 "a3:scriptImplementation" => "Adobe"
Chris@0 844 })
Chris@0 845 @root << Comment.new( " "+"\\"*66 )
Chris@0 846 @root << Comment.new( " Created with SVG::Graph " )
Chris@0 847 @root << Comment.new( " SVG::Graph by Sean E. Russell " )
Chris@0 848 @root << Comment.new( " Losely based on SVG::TT::Graph for Perl by"+
Chris@0 849 " Leo Lapworth & Stephan Morgan " )
Chris@0 850 @root << Comment.new( " "+"/"*66 )
Chris@0 851
Chris@0 852 defs = @root.add_element( "defs" )
Chris@0 853 add_defs defs
Chris@0 854 if not(style_sheet && style_sheet != '') and !no_css
Chris@0 855 @root << Comment.new(" include default stylesheet if none specified ")
Chris@0 856 style = defs.add_element( "style", {"type"=>"text/css"} )
Chris@0 857 style << CData.new( get_style )
Chris@0 858 end
Chris@0 859
Chris@0 860 @root << Comment.new( "SVG Background" )
Chris@0 861 @root.add_element( "rect", {
Chris@0 862 "width" => width.to_s,
Chris@0 863 "height" => height.to_s,
Chris@0 864 "x" => "0",
Chris@0 865 "y" => "0",
Chris@0 866 "class" => "svgBackground"
Chris@0 867 })
Chris@0 868 end
Chris@0 869
Chris@0 870
Chris@0 871 def calculate_graph_dimensions
Chris@0 872 calculate_left_margin
Chris@0 873 calculate_right_margin
Chris@0 874 calculate_bottom_margin
Chris@0 875 calculate_top_margin
Chris@0 876 @graph_width = width - @border_left - @border_right
Chris@0 877 @graph_height = height - @border_top - @border_bottom
Chris@0 878 end
Chris@0 879
Chris@0 880 def get_style
Chris@0 881 return <<EOL
Chris@0 882 /* Copy from here for external style sheet */
Chris@0 883 .svgBackground{
Chris@0 884 fill:#ffffff;
Chris@0 885 }
Chris@0 886 .graphBackground{
Chris@0 887 fill:#f0f0f0;
Chris@0 888 }
Chris@0 889
Chris@0 890 /* graphs titles */
Chris@0 891 .mainTitle{
Chris@0 892 text-anchor: middle;
Chris@0 893 fill: #000000;
Chris@0 894 font-size: #{title_font_size}px;
Chris@0 895 font-family: "Arial", sans-serif;
Chris@0 896 font-weight: normal;
Chris@0 897 }
Chris@0 898 .subTitle{
Chris@0 899 text-anchor: middle;
Chris@0 900 fill: #999999;
Chris@0 901 font-size: #{subtitle_font_size}px;
Chris@0 902 font-family: "Arial", sans-serif;
Chris@0 903 font-weight: normal;
Chris@0 904 }
Chris@0 905
Chris@0 906 .axis{
Chris@0 907 stroke: #000000;
Chris@0 908 stroke-width: 1px;
Chris@0 909 }
Chris@0 910
Chris@0 911 .guideLines{
Chris@0 912 stroke: #666666;
Chris@0 913 stroke-width: 1px;
Chris@0 914 stroke-dasharray: 5 5;
Chris@0 915 }
Chris@0 916
Chris@0 917 .xAxisLabels{
Chris@0 918 text-anchor: middle;
Chris@0 919 fill: #000000;
Chris@0 920 font-size: #{x_label_font_size}px;
Chris@0 921 font-family: "Arial", sans-serif;
Chris@0 922 font-weight: normal;
Chris@0 923 }
Chris@0 924
Chris@0 925 .yAxisLabels{
Chris@0 926 text-anchor: end;
Chris@0 927 fill: #000000;
Chris@0 928 font-size: #{y_label_font_size}px;
Chris@0 929 font-family: "Arial", sans-serif;
Chris@0 930 font-weight: normal;
Chris@0 931 }
Chris@0 932
Chris@0 933 .xAxisTitle{
Chris@0 934 text-anchor: middle;
Chris@0 935 fill: #ff0000;
Chris@0 936 font-size: #{x_title_font_size}px;
Chris@0 937 font-family: "Arial", sans-serif;
Chris@0 938 font-weight: normal;
Chris@0 939 }
Chris@0 940
Chris@0 941 .yAxisTitle{
Chris@0 942 fill: #ff0000;
Chris@0 943 text-anchor: middle;
Chris@0 944 font-size: #{y_title_font_size}px;
Chris@0 945 font-family: "Arial", sans-serif;
Chris@0 946 font-weight: normal;
Chris@0 947 }
Chris@0 948
Chris@0 949 .dataPointLabel{
Chris@0 950 fill: #000000;
Chris@0 951 text-anchor:middle;
Chris@0 952 font-size: 10px;
Chris@0 953 font-family: "Arial", sans-serif;
Chris@0 954 font-weight: normal;
Chris@0 955 }
Chris@0 956
Chris@0 957 .staggerGuideLine{
Chris@0 958 fill: none;
Chris@0 959 stroke: #000000;
Chris@0 960 stroke-width: 0.5px;
Chris@0 961 }
Chris@0 962
Chris@0 963 #{get_css}
Chris@0 964
Chris@0 965 .keyText{
Chris@0 966 fill: #000000;
Chris@0 967 text-anchor:start;
Chris@0 968 font-size: #{key_font_size}px;
Chris@0 969 font-family: "Arial", sans-serif;
Chris@0 970 font-weight: normal;
Chris@0 971 }
Chris@0 972 /* End copy for external style sheet */
Chris@0 973 EOL
Chris@0 974 end
Chris@0 975
Chris@0 976 end
Chris@0 977 end
Chris@0 978 end