« Bottom Story of the Day | Main | Gruff Graph and Rails addendum »

December 12, 2005

Gruff Graph 0.0.7 and Rails

I am building a simple app using the Gruff graphing library. It's very young, but quite good. I saw a commentor on the site ask how to integrate it with Rails, and since I just did that this weekend, I thought I would write up what I found.

Step 0: Following the directions at the gruff website, make sure you have RMagick and ImageMagick installed. The easiest way to verify this is to run the simple demo (scroll down) from the gruff website.

Step 1: Get the .tar.gz version of the gruff library. Untar and ungzip it.
Step 2: Go into the lib directory, and copy the entire gruff directory and gruff.rb into your railsappname/lib directory.

For example, if my app is called 'fred', and is on /home/johnbr/apps/fred, then I would:

# cp -r /tmp/gruff-0.0.7/lib/gruff /home/johnbr/apps/fred/lib
# cp /tmp/gruff-0.0.7/lib/gruff.rb /home/johnbr/apps/fred/lib

Step 3: Put this code into one of your controllers:


def demogruff
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.labels = {0 => '2003', 2 => '2004', 4 => '2005'}

filename = 'my_fruity_graph.png'

# this writes the file to the hard drive for caching
# and then writes it to the screen.
#

g.write(filename)
send_file filename, :type => 'image/png', :disposition => 'inline'

end

Update
After looking at the TopFunky website, he's already written a similar post, with similar instructions. I didn't notice until after I had written mine, so I'll leave it up in case someone needs it.


Posted by jb at December 12, 2005 01:31 PM

Trackback Pings

TrackBack URL for this entry:
http://www.undefined.com/cgi-bin/mt/mt-tb.cgi/400