annotate lib/SVG/Graph/Graph.rb @ 1524:82fac3dcf466 redmine-2.5-integration

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