Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: f5367da14a
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 557 lines (469 sloc) 12.986 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
#!/usr/bin/ruby

require File.dirname(__FILE__) + "/gruff_test_case"

class TestGruffLine < GruffTestCase

  # TODO Delete old output files once when starting tests

  def test_should_render_with_transparent_theme
    g = Gruff::Line.new(400)
    g.title = "Transparent Background"
    g.theme = {
      :colors => ['black', 'grey'],
      :marker_color => 'grey',
      :font_color => 'black',
      :background_colors => 'transparent'
    }
    
    g.labels = {
      0 => '5/6',
      1 => '5/15',
      2 => '5/24',
      3 => '5/30',
    }
    g.data(:apples, [-1, 0, 4, -4])
    g.data(:peaches, [10, 8, 6, 3])
    g.write("test/output/line_transparent.png")
  end
  
  def test_line_graph_with_themes
    line_graph_with_themes()
    line_graph_with_themes(400)
  end
  
  def test_one_value
    g = Gruff::Line.new
    g.title = "One Value"
    g.labels = {
      0 => '1',
      1 => '2'
    }
    g.data('one', 1)

    g.write("test/output/line_one_value.png")
  end

  def test_one_value_array
    g = Gruff::Line.new
    g.title = "One Value in an Array"
    g.labels = {
      0 => '1',
      1 => '2'
    }
    g.data('one', [1])

    g.write("test/output/line_one_value_array.png")
  end


  def test_should_not_hang_with_0_0_100
    g = Gruff::Line.new(320)
    g.title = "Hang Value Graph Test"
    g.data('test', [0,0,100])

    g.write("test/output/line_hang_value.png")
  end

  # TODO
  # def test_fix_crash
  # g = Gruff::Line.new(370)
  # g.title = "Crash Test"
  # g.data "ichi", [5]
  # g.data "ni", [0]
  # g.data "san", [0]
  # g.data "shi", [0]
  # g.write("test/output/line_crash_fix_test.png")
  # end

  
  def test_line_small_values
    @datasets = [
      [:small, [0.1, 0.14356, 0.0, 0.5674839, 0.456]],
      [:small2, [0.2, 0.3, 0.1, 0.05, 0.9]]
      ]

    g = Gruff::Line.new
    g.title = "Small Values Line Graph Test"
    @datasets.each do |data|
      g.data(data[0], data[1])
    end
    g.write("test/output/line_small.png")

    g = Gruff::Line.new(400)
    g.title = "Small Values Line Graph Test 400px"
    @datasets.each do |data|
      g.data(data[0], data[1])
    end
    g.write("test/output/line_small_small.png")
  end

  def test_line_starts_with_zero
    @datasets = [
      [:first0, [0, 5, 10, 8, 18]],
      [:normal, [1, 2, 3, 4, 5]]
      ]

    g = Gruff::Line.new
    g.title = "Small Values Line Graph Test"
    @datasets.each do |data|
      g.data(data[0], data[1])
    end
    g.write("test/output/line_small_zero.png")

    g = Gruff::Line.new(400)
    g.title = "Small Values Line Graph Test 400px"
    @datasets.each do |data|
      g.data(data[0], data[1])
    end
    g.write("test/output/line_small_small_zero.png")
  end

    
  def test_line_large_values
    @datasets = [
      [:large, [100_005, 35_000, 28_000, 27_000]],
      [:large2, [35_000, 28_000, 27_000, 100_005]],
      [:large3, [28_000, 27_000, 100_005, 35_000]],
      [:large4, [1_238, 39_092, 27_938, 48_876]]
      ]

    g = Gruff::Line.new
    g.title = "Very Large Values Line Graph Test"
    g.baseline_value = 50_000
    g.baseline_color = 'green'
    g.dot_radius = 15
    g.line_width = 3
    @datasets.each do |data|
      g.data(data[0], data[1])
    end

    g.write("test/output/line_large.png")
  end
    
  # def test_long_title
  #
  # end
  #
  # def test_add_colors
  #
  # end
  #

  def test_request_too_many_colors
    g = Gruff::Line.new
    g.title = "More Sets Than in Color Array"
# g.theme = {} # Sets theme with only black and white
    @datasets.each do |data|
      g.data(data[0], data[1])
    end
    @datasets.each do |data|
      g.data("#{data[0]}-B", data[1].map {|d| d + 20})
    end
    g.write("test/output/line_more_sets_than_colors.png")
  end
  
  #
  # def test_add_data
  #
  # end

  def test_many_datapoints
    g = Gruff::Line.new
    g.title = "Many Multi-Line Graph Test"
    g.labels = {
      0 => 'June',
      10 => 'July',
      30 => 'August',
      50 => 'September',
    }
    g.data('many points', (0..50).collect {|i| rand(100) })
    g.x_axis_label = "Months"

    # Default theme
    g.write("test/output/line_many.png")
  end


  def test_similar_high_end_values
    @dataset = %w(29.43 29.459 29.498 29.53 29.548 29.589 29.619 29.66 29.689 29.849 29.878 29.74 29.769 29.79 29.808 29.828).collect {|i| i.to_f}

    g = Gruff::Line.new
    g.title = "Similar High End Values Test"
    g.data('similar points', @dataset )
    g.write("test/output/line_similar_high_end_values.png")
    
    g = Gruff::Line.new
    g.title = "Similar High End Values With Floor"
    g.data('similar points', @dataset )
    g.minimum_value = 0
    g.y_axis_label = "Barometric Pressure"
    g.write("test/output/line_similar_high_end_values_with_floor.png")
  end

  def test_many_lines_graph_small
    g = Gruff::Line.new(400)
    g.title = "Many Values Line Test 400px"
    g.labels = {
      0 => '5/6',
      10 => '5/15',
      20 => '5/24',
      30 => '5/30',
      40 => '6/4',
      50 => '6/16'
    }
    %w{jimmy jane philip arthur julie bert}.each do |student_name|
      g.data(student_name, (0..50).collect { |i| rand 100 })
    end

    # Default theme
    g.write("test/output/line_many_lines_small.png")
  end

  def test_graph_tiny
    g = Gruff::Line.new(300)
    g.title = "Tiny Test 300px"
    g.labels = {
      0 => '5/6',
      10 => '5/15',
      20 => '5/24',
      30 => '5/30',
      40 => '6/4',
      50 => '6/16'
    }
    %w{jimmy jane philip arthur julie bert}.each do |student_name|
      g.data(student_name, (0..50).collect { |i| rand 100 })
    end

    # Default theme
    g.write("test/output/line_tiny.png")
  end

  def test_no_data
    g = Gruff::Line.new(400)
    g.title = "No Data"
    # Default theme
    g.write("test/output/line_no_data.png")
    
    g = Gruff::Line.new(400)
    g.title = "No Data Title"
    g.no_data_message = 'There is no data'
    g.write("test/output/line_no_data_msg.png")
  end


  def test_all_zeros
    g = Gruff::Line.new(400)
    g.title = "All Zeros"

    g.data(:gus, [0,0,0,0])

    # Default theme
    g.write("test/output/line_no_data_other.png")
  end


  def test_some_nil_points
    g = Gruff::Line.new
    g.title = "Some Nil Points"

    @datasets = [
      [:data1, [1, 2, 3, nil, 3, 5, 6]],
      [:data2, [5, nil, nil, nil, nil, nil, 5]],
      [:data3, [4, nil, 2, 1, 0]],
      [:data4, [nil, nil, 3, 1, 2]]
      ]

    @datasets.each do |data|
      g.data(*data)
    end

    # Default theme
    g.write("test/output/line_some_nil_points.png")
  end

  def test_no_title
    g = Gruff::Line.new(400)
    g.labels = @labels
    @datasets.each do |data|
      g.data(data[0], data[1])
    end

    g.write("test/output/line_no_title.png")
  end

  def test_no_line_markers
    g = setup_basic_graph(400)
    g.title = "No Line Markers"
    g.hide_line_markers = true
    g.write("test/output/line_no_line_markers.png")
  end

  def test_no_legend
    g = setup_basic_graph(400)
    g.title = "No Legend"
    g.hide_legend = true
    g.write("test/output/line_no_legend.png")
  end

  def test_nothing_but_the_graph
    g = setup_basic_graph(400)
    g.title = "THIS TITLE SHOULD NOT DISPLAY!!!"
    g.hide_line_markers = true
    g.hide_legend = true
    g.hide_title = true
    g.write("test/output/line_nothing_but_the_graph.png")
  end

  def test_baseline_larger_than_data
    g = setup_basic_graph(400)
    g.title = "Baseline Larger Than Data"
    g.baseline_value = 150
    g.write("test/output/line_large_baseline.png")
  end


  def test_hide_dots
    g = setup_basic_graph(400)
    g.title = "Hide Dots"
    g.hide_dots = true
    g.write("test/output/line_hide_dots.png")
  end

  def test_hide_lines
    g = setup_basic_graph(400)
    g.title = "Hide Lines"
    g.hide_lines = true
    g.write("test/output/line_hide_lines.png")
  end

  def test_wide_graph
    g = setup_basic_graph('800x400')
    g.title = "Wide Graph"
    g.write("test/output/line_wide_graph.png")

    g = setup_basic_graph('400x200')
    g.title = "Wide Graph Small"
    g.write("test/output/line_wide_graph_small.png")
  end

  def test_negative
    g = setup_pos_neg(800)
    g.write("test/output/line_pos_neg.png")
    
    g = setup_pos_neg(400)
    g.title = 'Pos/Neg Line Test Small'
    g.write("test/output/line_pos_neg_400.png")
  end

  def test_all_negative
    g = setup_all_neg(800)
    g.write("test/output/line_all_neg.png")
    
    g = setup_all_neg(400)
    g.title = 'All Neg Line Test Small'
    g.write("test/output/line_all_neg_400.png")
  end

  def test_many_numbers
    g = Gruff::Line.new('400x170')
    g.title = "Line Test, Many Numbers"

    data = [
      { :date => '01',
        :wpm => 0,
        :errors => 0,
        :accuracy => 0 },
      { :date => '02',
        :wpm => 10,
        :errors => 2,
        :accuracy => 80 },
      { :date => '03',
        :wpm => 15,
        :errors => 0,
        :accuracy => 100 },
      { :date => '04',
        :wpm => 16,
        :errors => 2,
        :accuracy => 87 },
      { :date => '05',
          :wpm => nil,
          :errors => nil,
          :accuracy => nil },
      { :date => '06',
        :wpm => 18,
        :errors => 1,
        :accuracy => 94 },
      { :date => '07'},
      { :date => '08' },
      { :date => '09',
         :wpm => 21,
         :errors => 1,
         :accuracy => 95 },
      { :date => '10'},
      { :date => '11'},
      { :date => '12'},
      { :date => '13'},
      { :date => '14'},
      { :date => '15'},
      { :date => '16'},
      { :date => '17'},
      { :date => '18'},
      { :date => '19',
         :wpm => 28,
         :errors => 5,
         :accuracy => 82 },
      { :date => '20'},
      { :date => '21'},
      { :date => '22'},
      { :date => '23'},
      { :date => '24'},
      { :date => '25'},
      { :date => '26'},
      { :date => '27',
         :wpm => 37,
         :errors => 3,
         :accuracy => 92 },
    ]

    [:wpm, :errors, :accuracy].each do |field|
      g.data(field.to_s, data.collect {|d| d[field] })
    end

    labels = Hash.new
    data.each_with_index do |d, i|
      labels[i] = d[:date]
    end
    g.labels = labels

    g.write('test/output/line_many_numbers.png')
  end

  def test_no_hide_line_no_labels
    g = Gruff::Line.new
    g.title = "No Hide Line No Labels"
    @datasets.each do |data|
      g.data(data[0], data[1])
    end
    g.hide_line_markers = false
    g.write('test/output/line_no_hide.png')
  end
  

  def test_jruby_error
    g = Gruff::Line.new
    g.theme = {
      :colors => ['#7F0099', '#2F85ED', '#2FED09','#EC962F'],
      :marker_color => '#aaa',
      :background_colors => ['#E8E8E8','#B9FD6C']
    }
    g.hide_title = true

    g.legend_font_size = 12
    g.marker_font_size = 16
    g.hide_dots = false
    g.label_max_decimals = 1

    g.write('test/output/line_jruby_error.png')
  end

private
  
  def bmi(params={})
    g = basic_graph()

    g.y_axis_label = 'BMI'

    bmis = [24.3, 23.9, 23.7, 23.7, 23.6, 23.9, 23.6, 23.7, 23.4, 23.4, 23.4, 22.9]

    g.data( 'BMI', bmis )
    g.hide_legend = true
    return g
  end

  # TODO Reset data after each theme
  def line_graph_with_themes(size=nil)
    g = Gruff::Line.new(size)
    g.title = "Multi-Line Graph Test #{size}"
    g.labels = @labels
    g.baseline_value = 90
    @datasets.each do |data|
      g.data(data[0], data[1])
    end
    # Default theme
    g.write("test/output/line_theme_keynote_#{size}.png")
  
    g = Gruff::Line.new(size)
    g.title = "Multi-Line Graph Test #{size}"
    g.labels = @labels
    g.baseline_value = 90
    g.theme_37signals
    @datasets.each do |data|
      g.data(data[0], data[1])
    end
    g.write("test/output/line_theme_37signals_#{size}.png")
  
    g = Gruff::Line.new(size)
    g.title = "Multi-Line Graph Test #{size}"
    g.labels = @labels
    g.baseline_value = 90
    g.theme_rails_keynote
    @datasets.each do |data|
      g.data(data[0], data[1])
    end
    g.write("test/output/line_theme_rails_keynote_#{size}.png")
  
    g = Gruff::Line.new(size)
    g.title = "Multi-Line Graph Test #{size}"
    g.labels = @labels
    g.baseline_value = 90
    g.theme_odeo
    @datasets.each do |data|
      g.data(data[0], data[1])
    end
    g.write("test/output/line_theme_odeo_#{size}.png")
  end

  def setup_pos_neg(size=800)
    g = Gruff::Line.new(size)
    g.title = "Pos/Neg Line Graph Test"
    g.labels = {
      0 => '5/6',
      1 => '5/15',
      2 => '5/24',
      3 => '5/30',
    }
    g.data(:apples, [-1, 0, 4, -4])
    g.data(:peaches, [10, 8, 6, 3])
    return g
  end


  def setup_all_neg(size=800)
    g = Gruff::Line.new(size)
    g.title = "All Neg Line Graph Test"
    g.labels = {
      0 => '5/6',
      1 => '5/15',
      2 => '5/24',
      3 => '5/30',
    }
    g.data(:apples, [-1, -5, -20, -4])
    g.data(:peaches, [-10, -8, -6, -3])
    g
  end
  
end
Something went wrong with that request. Please try again.