annotate lib/SVG/Graph/Plot.rb @ 1465:ab8bd24eeb65 bug_635

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