annotate .svn/pristine/c0/c02eb2e34e399c7d7d5b13e4d8bbd611abc99f12.svn-base @ 1517:dffacf8a6908 redmine-2.5

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