annotate .svn/pristine/08/08ad4f57c782749abd613605456ebd0acf953d61.svn-base @ 1327:287f201c2802 redmine-2.2-integration

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