annotate .svn/pristine/f3/f3e5e994ff2f624af7f4980e43ec0d7f29545774.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

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