Gruff with Rails, Typo Rewrite 8 comments

posted Saturday, November 19, 2005 by topfunky

Faithful reader Greg asked how one can use Gruff with Rails.

There are a few different ways and I hope to make a Rails generator or rake task to make this a little easier. First, add to environment.rb:

require 'gruff'

You might also need to copy the Gruff library files to your Rails app’s ./lib directory (for a shared host).

I like to build a graph in a method and send it straight to the browser. You can use Rails caching with that method to store it on disk.

# ... Use inside any controller

# Send a graph to the browser
def line_graph_report
  g = Gruff::Line.new(400)
  g.title = "Scores for Bart" 
  g.font = File.expand_path('artwork/fonts/Vera.ttf', RAILS_ROOT)
  g.labels = { 0 => 'Mon', 2 => 'Wed', 4 => 'Fri', 6 => 'Sun' }

  # Modify this to represent your actual data models
  @data = Data.find(:all)
  @data.each do |d|
    # Build data into array with something like
    # built_array = d.collect { |e| e.some_number_field.to_f }
    g.data(some_label, built_array)
  end

  send_data(g.to_blob, 
            :disposition => 'inline', 
            :type => 'image/png', 
            :filename => "bart_scores.png")
end

Or, Carlos Villela wrote a helper so you can call Gruff from a view.

Download Gruff on Rails Sample Code

Typo RSS Feed

I upgraded to Typo 2.5.6 and noticed that the RSS feed had changed. Others also had problems with this. I had the most success by putting this at the top of my .htaccess Rewrite section:

RewriteRule ^xml/([a-z]+)$ /xml/rss20/feed.xml [R=301]

Adam Greenfield suggested the following, which may work for you:

RewriteRule ^xml/rss/feed.xml /xml/rss20/feed.xml [R=301]

There is code in Typo that is supposed to handle this, but caching seems to keep it from working correctly for all cases.

UPDATE: You also need to empty your page cache in the Typo admin, then the rewriting will work. Otherwise, the old cached feed will still be served.

8 comments

Leave a response

  • Hey, I know this isn’t exactly the place for support but darn if I can get Gruff to play nice on win32.

    Seemingly no problems when usig RMagick alone, but trying to require gruff results in errors such as :

    NameError: undefined method `assoc’ for class `Magick::ImageList’

    Any ideas? I tried reinstalling rmagick, after uninstalling gruff, still the same problem.

  • Gravatar icon Brett

    I finally got Gruff working on my installation recently, but I’m having trouble figuring out how to use the Gruff helper mentioned in this post. Does anyone have any code examples of the helper? Right now if I just use <%= gruff_tag %> then I get an empty image with a blue gradient background. Putting anything else after the gruff_tag only renders an image that says “Gruff Graph”

    Help?

    Thanks in advance, Brett

  • anyone else get this?

    /usr/local/lib/ruby/gems/1.8/gems/gruff-0.2.8/lib/gruff/base.rb:965: [BUG] Bus Error ruby 1.8.5 (2006-12-25) [i686-darwin8.8.3]

  • Gravatar icon Davis Kitchel

    I get a undefined method `collect’

  • Gravatar icon marcosvm

    I got a similar error: /usr/local/lib/ruby/gems/1.8/gems/gruff-0.2.8/lib/gruff/base.rb:965: [BUG] Bus Error

  • if you guys get Rmagick errors put

    require ‘RMagick.rb’

    in environment.rb

  • Gravatar icon mahesh

    hai thank you for your code, its working fine but i con’t able to chnage the given code to my own application can u give some more clear explanation regarding to this

  • Gravatar icon Jason Thomas

    The controller code is great but what about the view? I finally figured out a way to do it but I’m a noob so this may be dumb.

    In my show.html.erb file I do the following: <%= “” %>

    Where my controller has a function named graph which does something like: @item = Item.find(params[:id])

    Perhaps there is a better way but finding code examples for the view was very difficult.

Your Comment

Nuby on Rails

Geoffrey Grosenbach / Ruby / Code / Graphics / Design / Rails / Merb / Javascript / CSS

Manufactured with

Subscribe

Subscribe (RSS)