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