annotate .svn/pristine/62/6230957c0e4d32e6d5ac196562d742563883d7c0.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

Merge from redmine-2.3 branch to create new branch redmine-2.3-integration
author Chris Cannam
date Fri, 14 Jun 2013 09:28:30 +0100
parents cbb26bc654de
children
rev   line source
Chris@909 1 require 'SVG/Graph/Graph'
Chris@909 2
Chris@909 3 module SVG
Chris@909 4 module Graph
Chris@909 5 # === For creating SVG plots of scalar data
Chris@909 6 #
Chris@909 7 # = Synopsis
Chris@909 8 #
Chris@909 9 # require 'SVG/Graph/Plot'
Chris@909 10 #
Chris@909 11 # # Data sets are x,y pairs
Chris@909 12 # # Note that multiple data sets can differ in length, and that the
Chris@909 13 # # data in the datasets needn't be in order; they will be ordered
Chris@909 14 # # by the plot along the X-axis.
Chris@909 15 # projection = [
Chris@909 16 # 6, 11, 0, 5, 18, 7, 1, 11, 13, 9, 1, 2, 19, 0, 3, 13,
Chris@909 17 # 7, 9
Chris@909 18 # ]
Chris@909 19 # actual = [
Chris@909 20 # 0, 18, 8, 15, 9, 4, 18, 14, 10, 2, 11, 6, 14, 12,
Chris@909 21 # 15, 6, 4, 17, 2, 12
Chris@909 22 # ]
Chris@909 23 #
Chris@909 24 # graph = SVG::Graph::Plot.new({
Chris@909 25 # :height => 500,
Chris@909 26 # :width => 300,
Chris@909 27 # :key => true,
Chris@909 28 # :scale_x_integers => true,
Chris@909 29 # :scale_y_integerrs => true,
Chris@909 30 # })
Chris@909 31 #
Chris@909 32 # graph.add_data({
Chris@909 33 # :data => projection
Chris@909 34 # :title => 'Projected',
Chris@909 35 # })
Chris@909 36 #
Chris@909 37 # graph.add_data({
Chris@909 38 # :data => actual,
Chris@909 39 # :title => 'Actual',
Chris@909 40 # })
Chris@909 41 #
Chris@909 42 # print graph.burn()
Chris@909 43 #
Chris@909 44 # = Description
Chris@909 45 #
Chris@909 46 # Produces a graph of scalar data.
Chris@909 47 #
Chris@909 48 # This object aims to allow you to easily create high quality
Chris@909 49 # SVG[http://www.w3c.org/tr/svg] scalar plots. You can either use the
Chris@909 50 # default style sheet or supply your own. Either way there are many options
Chris@909 51 # which can be configured to give you control over how the graph is
Chris@909 52 # generated - with or without a key, data elements at each point, title,
Chris@909 53 # subtitle etc.
Chris@909 54 #
Chris@909 55 # = Examples
Chris@909 56 #
Chris@909 57 # http://www.germane-software/repositories/public/SVG/test/plot.rb
Chris@909 58 #
Chris@909 59 # = Notes
Chris@909 60 #
Chris@909 61 # The default stylesheet handles upto 10 data sets, if you
Chris@909 62 # use more you must create your own stylesheet and add the
Chris@909 63 # additional settings for the extra data sets. You will know
Chris@909 64 # if you go over 10 data sets as they will have no style and
Chris@909 65 # be in black.
Chris@909 66 #
Chris@909 67 # Unlike the other types of charts, data sets must contain x,y pairs:
Chris@909 68 #
Chris@909 69 # [ 1, 2 ] # A data set with 1 point: (1,2)
Chris@909 70 # [ 1,2, 5,6] # A data set with 2 points: (1,2) and (5,6)
Chris@909 71 #
Chris@909 72 # = See also
Chris@909 73 #
Chris@909 74 # * SVG::Graph::Graph
Chris@909 75 # * SVG::Graph::BarHorizontal
Chris@909 76 # * SVG::Graph::Bar
Chris@909 77 # * SVG::Graph::Line
Chris@909 78 # * SVG::Graph::Pie
Chris@909 79 # * SVG::Graph::TimeSeries
Chris@909 80 #
Chris@909 81 # == Author
Chris@909 82 #
Chris@909 83 # Sean E. Russell <serATgermaneHYPHENsoftwareDOTcom>
Chris@909 84 #
Chris@909 85 # Copyright 2004 Sean E. Russell
Chris@909 86 # This software is available under the Ruby license[LICENSE.txt]
Chris@909 87 #
Chris@909 88 class Plot < Graph
Chris@909 89
Chris@909 90 # In addition to the defaults set by Graph::initialize, sets
Chris@909 91 # [show_data_values] true
Chris@909 92 # [show_data_points] true
Chris@909 93 # [area_fill] false
Chris@909 94 # [stacked] false
Chris@909 95 def set_defaults
Chris@909 96 init_with(
Chris@909 97 :show_data_values => true,
Chris@909 98 :show_data_points => true,
Chris@909 99 :area_fill => false,
Chris@909 100 :stacked => false
Chris@909 101 )
Chris@909 102 self.top_align = self.right_align = self.top_font = self.right_font = 1
Chris@909 103 end
Chris@909 104
Chris@909 105 # Determines the scaling for the X axis divisions.
Chris@909 106 #
Chris@909 107 # graph.scale_x_divisions = 2
Chris@909 108 #
Chris@909 109 # would cause the graph to attempt to generate labels stepped by 2; EG:
Chris@909 110 # 0,2,4,6,8...
Chris@909 111 attr_accessor :scale_x_divisions
Chris@909 112 # Determines the scaling for the Y axis divisions.
Chris@909 113 #
Chris@909 114 # graph.scale_y_divisions = 0.5
Chris@909 115 #
Chris@909 116 # would cause the graph to attempt to generate labels stepped by 0.5; EG:
Chris@909 117 # 0, 0.5, 1, 1.5, 2, ...
Chris@909 118 attr_accessor :scale_y_divisions
Chris@909 119 # Make the X axis labels integers
Chris@909 120 attr_accessor :scale_x_integers
Chris@909 121 # Make the Y axis labels integers
Chris@909 122 attr_accessor :scale_y_integers
Chris@909 123 # Fill the area under the line
Chris@909 124 attr_accessor :area_fill
Chris@909 125 # Show a small circle on the graph where the line
Chris@909 126 # goes from one point to the next.
Chris@909 127 attr_accessor :show_data_points
Chris@909 128 # Set the minimum value of the X axis
Chris@909 129 attr_accessor :min_x_value
Chris@909 130 # Set the minimum value of the Y axis
Chris@909 131 attr_accessor :min_y_value
Chris@909 132
Chris@909 133
Chris@909 134 # Adds data to the plot. The data must be in X,Y pairs; EG
Chris@909 135 # [ 1, 2 ] # A data set with 1 point: (1,2)
Chris@909 136 # [ 1,2, 5,6] # A data set with 2 points: (1,2) and (5,6)
Chris@909 137 def add_data data
Chris@909 138 @data = [] unless @data
Chris@909 139
Chris@909 140 raise "No data provided by #{conf.inspect}" unless data[:data] and
Chris@909 141 data[:data].kind_of? Array
Chris@909 142 raise "Data supplied must be x,y pairs! "+
Chris@909 143 "The data provided contained an odd set of "+
Chris@909 144 "data points" unless data[:data].length % 2 == 0
Chris@909 145 return if data[:data].length == 0
Chris@909 146
Chris@909 147 x = []
Chris@909 148 y = []
Chris@909 149 data[:data].each_index {|i|
Chris@909 150 (i%2 == 0 ? x : y) << data[:data][i]
Chris@909 151 }
Chris@909 152 sort( x, y )
Chris@909 153 data[:data] = [x,y]
Chris@909 154 @data << data
Chris@909 155 end
Chris@909 156
Chris@909 157
Chris@909 158 protected
Chris@909 159
Chris@909 160 def keys
Chris@909 161 @data.collect{ |x| x[:title] }
Chris@909 162 end
Chris@909 163
Chris@909 164 def calculate_left_margin
Chris@909 165 super
Chris@909 166 label_left = get_x_labels[0].to_s.length / 2 * font_size * 0.6
Chris@909 167 @border_left = label_left if label_left > @border_left
Chris@909 168 end
Chris@909 169
Chris@909 170 def calculate_right_margin
Chris@909 171 super
Chris@909 172 label_right = get_x_labels[-1].to_s.length / 2 * font_size * 0.6
Chris@909 173 @border_right = label_right if label_right > @border_right
Chris@909 174 end
Chris@909 175
Chris@909 176
Chris@909 177 X = 0
Chris@909 178 Y = 1
Chris@909 179 def x_range
Chris@909 180 max_value = @data.collect{|x| x[:data][X][-1] }.max
Chris@909 181 min_value = @data.collect{|x| x[:data][X][0] }.min
Chris@909 182 min_value = min_value<min_x_value ? min_value : min_x_value if min_x_value
Chris@909 183
Chris@909 184 range = max_value - min_value
Chris@909 185 right_pad = range == 0 ? 10 : range / 20.0
Chris@909 186 scale_range = (max_value + right_pad) - min_value
Chris@909 187
Chris@909 188 scale_division = scale_x_divisions || (scale_range / 10.0)
Chris@909 189
Chris@909 190 if scale_x_integers
Chris@909 191 scale_division = scale_division < 1 ? 1 : scale_division.round
Chris@909 192 end
Chris@909 193
Chris@909 194 [min_value, max_value, scale_division]
Chris@909 195 end
Chris@909 196
Chris@909 197 def get_x_values
Chris@909 198 min_value, max_value, scale_division = x_range
Chris@909 199 rv = []
Chris@909 200 min_value.step( max_value, scale_division ) {|v| rv << v}
Chris@909 201 return rv
Chris@909 202 end
Chris@909 203 alias :get_x_labels :get_x_values
Chris@909 204
Chris@909 205 def field_width
Chris@909 206 values = get_x_values
Chris@909 207 max = @data.collect{|x| x[:data][X][-1]}.max
Chris@909 208 dx = (max - values[-1]).to_f / (values[-1] - values[-2])
Chris@909 209 (@graph_width.to_f - font_size*2*right_font) /
Chris@909 210 (values.length + dx - right_align)
Chris@909 211 end
Chris@909 212
Chris@909 213
Chris@909 214 def y_range
Chris@909 215 max_value = @data.collect{|x| x[:data][Y].max }.max
Chris@909 216 min_value = @data.collect{|x| x[:data][Y].min }.min
Chris@909 217 min_value = min_value<min_y_value ? min_value : min_y_value if min_y_value
Chris@909 218
Chris@909 219 range = max_value - min_value
Chris@909 220 top_pad = range == 0 ? 10 : range / 20.0
Chris@909 221 scale_range = (max_value + top_pad) - min_value
Chris@909 222
Chris@909 223 scale_division = scale_y_divisions || (scale_range / 10.0)
Chris@909 224
Chris@909 225 if scale_y_integers
Chris@909 226 scale_division = scale_division < 1 ? 1 : scale_division.round
Chris@909 227 end
Chris@909 228
Chris@909 229 return [min_value, max_value, scale_division]
Chris@909 230 end
Chris@909 231
Chris@909 232 def get_y_values
Chris@909 233 min_value, max_value, scale_division = y_range
Chris@909 234 rv = []
Chris@909 235 min_value.step( max_value, scale_division ) {|v| rv << v}
Chris@909 236 return rv
Chris@909 237 end
Chris@909 238 alias :get_y_labels :get_y_values
Chris@909 239
Chris@909 240 def field_height
Chris@909 241 values = get_y_values
Chris@909 242 max = @data.collect{|x| x[:data][Y].max }.max
Chris@909 243 if values.length == 1
Chris@909 244 dx = values[-1]
Chris@909 245 else
Chris@909 246 dx = (max - values[-1]).to_f / (values[-1] - values[-2])
Chris@909 247 end
Chris@909 248 (@graph_height.to_f - font_size*2*top_font) /
Chris@909 249 (values.length + dx - top_align)
Chris@909 250 end
Chris@909 251
Chris@909 252 def draw_data
Chris@909 253 line = 1
Chris@909 254
Chris@909 255 x_min, x_max, x_div = x_range
Chris@909 256 y_min, y_max, y_div = y_range
Chris@909 257 x_step = (@graph_width.to_f - font_size*2) / (x_max-x_min)
Chris@909 258 y_step = (@graph_height.to_f - font_size*2) / (y_max-y_min)
Chris@909 259
Chris@909 260 for data in @data
Chris@909 261 x_points = data[:data][X]
Chris@909 262 y_points = data[:data][Y]
Chris@909 263
Chris@909 264 lpath = "L"
Chris@909 265 x_start = 0
Chris@909 266 y_start = 0
Chris@909 267 x_points.each_index { |idx|
Chris@909 268 x = (x_points[idx] - x_min) * x_step
Chris@909 269 y = @graph_height - (y_points[idx] - y_min) * y_step
Chris@909 270 x_start, y_start = x,y if idx == 0
Chris@909 271 lpath << "#{x} #{y} "
Chris@909 272 }
Chris@909 273
Chris@909 274 if area_fill
Chris@909 275 @graph.add_element( "path", {
Chris@909 276 "d" => "M#{x_start} #@graph_height #{lpath} V#@graph_height Z",
Chris@909 277 "class" => "fill#{line}"
Chris@909 278 })
Chris@909 279 end
Chris@909 280
Chris@909 281 @graph.add_element( "path", {
Chris@909 282 "d" => "M#{x_start} #{y_start} #{lpath}",
Chris@909 283 "class" => "line#{line}"
Chris@909 284 })
Chris@909 285
Chris@909 286 if show_data_points || show_data_values
Chris@909 287 x_points.each_index { |idx|
Chris@909 288 x = (x_points[idx] - x_min) * x_step
Chris@909 289 y = @graph_height - (y_points[idx] - y_min) * y_step
Chris@909 290 if show_data_points
Chris@909 291 @graph.add_element( "circle", {
Chris@909 292 "cx" => x.to_s,
Chris@909 293 "cy" => y.to_s,
Chris@909 294 "r" => "2.5",
Chris@909 295 "class" => "dataPoint#{line}"
Chris@909 296 })
Chris@909 297 add_popup(x, y, format( x_points[idx], y_points[idx] )) if add_popups
Chris@909 298 end
Chris@909 299 make_datapoint_text( x, y-6, y_points[idx] ) if show_data_values
Chris@909 300 }
Chris@909 301 end
Chris@909 302 line += 1
Chris@909 303 end
Chris@909 304 end
Chris@909 305
Chris@909 306 def format x, y
Chris@909 307 "(#{(x * 100).to_i / 100}, #{(y * 100).to_i / 100})"
Chris@909 308 end
Chris@909 309
Chris@909 310 def get_css
Chris@909 311 return <<EOL
Chris@909 312 /* default line styles */
Chris@909 313 .line1{
Chris@909 314 fill: none;
Chris@909 315 stroke: #ff0000;
Chris@909 316 stroke-width: 1px;
Chris@909 317 }
Chris@909 318 .line2{
Chris@909 319 fill: none;
Chris@909 320 stroke: #0000ff;
Chris@909 321 stroke-width: 1px;
Chris@909 322 }
Chris@909 323 .line3{
Chris@909 324 fill: none;
Chris@909 325 stroke: #00ff00;
Chris@909 326 stroke-width: 1px;
Chris@909 327 }
Chris@909 328 .line4{
Chris@909 329 fill: none;
Chris@909 330 stroke: #ffcc00;
Chris@909 331 stroke-width: 1px;
Chris@909 332 }
Chris@909 333 .line5{
Chris@909 334 fill: none;
Chris@909 335 stroke: #00ccff;
Chris@909 336 stroke-width: 1px;
Chris@909 337 }
Chris@909 338 .line6{
Chris@909 339 fill: none;
Chris@909 340 stroke: #ff00ff;
Chris@909 341 stroke-width: 1px;
Chris@909 342 }
Chris@909 343 .line7{
Chris@909 344 fill: none;
Chris@909 345 stroke: #00ffff;
Chris@909 346 stroke-width: 1px;
Chris@909 347 }
Chris@909 348 .line8{
Chris@909 349 fill: none;
Chris@909 350 stroke: #ffff00;
Chris@909 351 stroke-width: 1px;
Chris@909 352 }
Chris@909 353 .line9{
Chris@909 354 fill: none;
Chris@909 355 stroke: #ccc6666;
Chris@909 356 stroke-width: 1px;
Chris@909 357 }
Chris@909 358 .line10{
Chris@909 359 fill: none;
Chris@909 360 stroke: #663399;
Chris@909 361 stroke-width: 1px;
Chris@909 362 }
Chris@909 363 .line11{
Chris@909 364 fill: none;
Chris@909 365 stroke: #339900;
Chris@909 366 stroke-width: 1px;
Chris@909 367 }
Chris@909 368 .line12{
Chris@909 369 fill: none;
Chris@909 370 stroke: #9966FF;
Chris@909 371 stroke-width: 1px;
Chris@909 372 }
Chris@909 373 /* default fill styles */
Chris@909 374 .fill1{
Chris@909 375 fill: #cc0000;
Chris@909 376 fill-opacity: 0.2;
Chris@909 377 stroke: none;
Chris@909 378 }
Chris@909 379 .fill2{
Chris@909 380 fill: #0000cc;
Chris@909 381 fill-opacity: 0.2;
Chris@909 382 stroke: none;
Chris@909 383 }
Chris@909 384 .fill3{
Chris@909 385 fill: #00cc00;
Chris@909 386 fill-opacity: 0.2;
Chris@909 387 stroke: none;
Chris@909 388 }
Chris@909 389 .fill4{
Chris@909 390 fill: #ffcc00;
Chris@909 391 fill-opacity: 0.2;
Chris@909 392 stroke: none;
Chris@909 393 }
Chris@909 394 .fill5{
Chris@909 395 fill: #00ccff;
Chris@909 396 fill-opacity: 0.2;
Chris@909 397 stroke: none;
Chris@909 398 }
Chris@909 399 .fill6{
Chris@909 400 fill: #ff00ff;
Chris@909 401 fill-opacity: 0.2;
Chris@909 402 stroke: none;
Chris@909 403 }
Chris@909 404 .fill7{
Chris@909 405 fill: #00ffff;
Chris@909 406 fill-opacity: 0.2;
Chris@909 407 stroke: none;
Chris@909 408 }
Chris@909 409 .fill8{
Chris@909 410 fill: #ffff00;
Chris@909 411 fill-opacity: 0.2;
Chris@909 412 stroke: none;
Chris@909 413 }
Chris@909 414 .fill9{
Chris@909 415 fill: #cc6666;
Chris@909 416 fill-opacity: 0.2;
Chris@909 417 stroke: none;
Chris@909 418 }
Chris@909 419 .fill10{
Chris@909 420 fill: #663399;
Chris@909 421 fill-opacity: 0.2;
Chris@909 422 stroke: none;
Chris@909 423 }
Chris@909 424 .fill11{
Chris@909 425 fill: #339900;
Chris@909 426 fill-opacity: 0.2;
Chris@909 427 stroke: none;
Chris@909 428 }
Chris@909 429 .fill12{
Chris@909 430 fill: #9966FF;
Chris@909 431 fill-opacity: 0.2;
Chris@909 432 stroke: none;
Chris@909 433 }
Chris@909 434 /* default line styles */
Chris@909 435 .key1,.dataPoint1{
Chris@909 436 fill: #ff0000;
Chris@909 437 stroke: none;
Chris@909 438 stroke-width: 1px;
Chris@909 439 }
Chris@909 440 .key2,.dataPoint2{
Chris@909 441 fill: #0000ff;
Chris@909 442 stroke: none;
Chris@909 443 stroke-width: 1px;
Chris@909 444 }
Chris@909 445 .key3,.dataPoint3{
Chris@909 446 fill: #00ff00;
Chris@909 447 stroke: none;
Chris@909 448 stroke-width: 1px;
Chris@909 449 }
Chris@909 450 .key4,.dataPoint4{
Chris@909 451 fill: #ffcc00;
Chris@909 452 stroke: none;
Chris@909 453 stroke-width: 1px;
Chris@909 454 }
Chris@909 455 .key5,.dataPoint5{
Chris@909 456 fill: #00ccff;
Chris@909 457 stroke: none;
Chris@909 458 stroke-width: 1px;
Chris@909 459 }
Chris@909 460 .key6,.dataPoint6{
Chris@909 461 fill: #ff00ff;
Chris@909 462 stroke: none;
Chris@909 463 stroke-width: 1px;
Chris@909 464 }
Chris@909 465 .key7,.dataPoint7{
Chris@909 466 fill: #00ffff;
Chris@909 467 stroke: none;
Chris@909 468 stroke-width: 1px;
Chris@909 469 }
Chris@909 470 .key8,.dataPoint8{
Chris@909 471 fill: #ffff00;
Chris@909 472 stroke: none;
Chris@909 473 stroke-width: 1px;
Chris@909 474 }
Chris@909 475 .key9,.dataPoint9{
Chris@909 476 fill: #cc6666;
Chris@909 477 stroke: none;
Chris@909 478 stroke-width: 1px;
Chris@909 479 }
Chris@909 480 .key10,.dataPoint10{
Chris@909 481 fill: #663399;
Chris@909 482 stroke: none;
Chris@909 483 stroke-width: 1px;
Chris@909 484 }
Chris@909 485 .key11,.dataPoint11{
Chris@909 486 fill: #339900;
Chris@909 487 stroke: none;
Chris@909 488 stroke-width: 1px;
Chris@909 489 }
Chris@909 490 .key12,.dataPoint12{
Chris@909 491 fill: #9966FF;
Chris@909 492 stroke: none;
Chris@909 493 stroke-width: 1px;
Chris@909 494 }
Chris@909 495 EOL
Chris@909 496 end
Chris@909 497
Chris@909 498 end
Chris@909 499 end
Chris@909 500 end