Gruff Graphs for Ruby

The Gruff Graphing Library is a project to make beautiful graphs with Ruby. Can be used alone or with Ruby on Rails.

Author

Geoffrey Grosenbach boss@topfunky.com

Download

Install easily with Ruby Gems (RMagick required):

sudo gem install gruff

The easiest way to use it with a Rails project is to unpack the gem into vendor/plugins and require it from environment.rb.

Also see the tests for examples of how to make a graph.

Documentation

Samples

You can now graph negative numbers (line and bar), specify colors, and customize the graphs in other ways.

Code Sample


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

g.write('my_fruity_graph.png')

Install ImageMagick and RMagick

Yes, it can be a pain. I use DarwinPorts on Mac OS X and these general instructions (I didn’t have to manually edit the config as mentioned):

Ruby on Rails

Personally, I prefer to have a “Reports” controller that does a call to the database and uses send_data to send the graph back to the browser. That way, you can use Rails caching and save some of the load on your server.

However, you can also use a Rails helper as mentioned here.

Blog Posts About Gruff

Selected Examples of Gruff in Use

Linux Distros that Include Gruff

Other Tutorials

Planned Features

  • Image-based themes for 3-D looking bar and pie graphs.
  • Better rounding of left-hand markers.
  • Better scaling for use in small sizes (i.e. omitting some markers when graphs are less than 400px wide).
  • Ports to PDF::Writer and SVG for use in print.

Other Graphing Libraries

If Gruff doesn’t do what you need it to, you might try one of these other tasty delights:

Nuby on Rails

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

Ads by The Lounge

Manufactured with

Subscribe

Subscribe (RSS)