Search code examples
rubychartsimagemagickrmagickgruff

Gruff is not working well. What to do?


I`m facing a problem with Gruff and Rails. Examples on the site fail with:

ZeroDivisionError: divided by 0
from /home/prikha/.rvm/gems/ruby-1.9.3-p194@rubymine/gems/gruff-0.3.6/lib/gruff/base.rb:1066:in `label'
from /home/prikha/.rvm/gems/ruby-1.9.3-p194@rubymine/gems/gruff-0.3.6/lib/gruff/base.rb:590:in `setup_graph_measurements'
from /home/prikha/.rvm/gems/ruby-1.9.3-p194@rubymine/gems/gruff-0.3.6/lib/gruff/base.rb:532:in `setup_drawing'
from /home/prikha/.rvm/gems/ruby-1.9.3-p194@rubymine/gems/gruff-0.3.6/lib/gruff/base.rb:508:in `draw'
from /home/prikha/.rvm/gems/ruby-1.9.3-p194@rubymine/gems/gruff-0.3.6/lib/gruff/line.rb:53:in `draw'
from /home/prikha/.rvm/gems/ruby-1.9.3-p194@rubymine/gems/gruff-0.3.6/lib/gruff/base.rb:487:in `write'
from (irb):8
from /home/prikha/.rvm/rubies/ruby-1.9.3-p194/bin/irb:16:in `<main>'

Solution

  • I had the same problem. The way i solve it is the following: ZeroDivisionError appears in 1066 line of gruff/base.rb Let's look at this file closer

    label = if (@spread.to_f % @marker_count.to_f == 0) || !@y_axis_increment.nil?
    

    So, ZeroDivisionError was caused by the fact that @marker_count property was equal to zero. I know, it's not the best solution, but i've added an explicit assignment @marker_count = <non-zero value> before drawing graph. So, now example from site looks like:

    #!/usr/bin/ruby
    require 'rubygems'
    require 'gruff'
    
    g = Gruff::Line.new
    g.title = "My Graph" 
    
    g.data("Apples", [1, 2, 3, 4, 4, 3])
    g.data("Oranges", [4, 8, 7, 9, 8, 9])
    g.data("Watermelon", [2, 3, 1, 5, 6, 8])
    g.data("Peaches", [9, 9, 10, 8, 7, 9])
    g.marker_count = 4 #explicitly assign value to @marker_count
    g.labels = {0 => '2003', 2 => '2004', 4 => '2005'}
    
    g.write('my_fruity_graph.png')
    

    It works fine for me. I know that it's not the general solution for the problem, but this hack can help you to deal with that library up until this error will be fixed by developers.

    ADD
    @market_count is a count of markers on vertical axis. So you can play with this property to prettify your graph.