Charts in Ruby - Using Gruff

It is very common requirement for any reporting application to be able to draw charts based on some historical and statistical data. The languages with big user community like Java have various libraries available for creating charts. But when it comes to Ruby, there are not as many or as sophisticated choices. I was looking for a library in Ruby to do this sometime back and I found Gruff. It’s a very simple and efficient library.


To install Gruff, all you need to do is:

gem install gruff

You also need to have the RMagick installed that needs the ImageMagick to be installed. You can install ImageMagick using the package manager of your Linux distribution or download it directly from ImageMagick website and install. The RMagick installation can be done by either using gem or using the native OS package manager. On my Mandriva Linux box, the gem installation kept on failing during compilation. So, I just installed it using urpmi and it worked.
I wrote a small module in Ruby to draw the line charts. This module can be included in the scripts where you need to draw the charts. This module is called charts.rb. Here is the source code for it:

The arguments for drawChart function are as follows:

  1. Title of the chart
  2. A Hash of the data (the Caption for data => Array of data)
  3. A Hash of labels for the X axis
  4. Path of the final generated image
  5. The scale of the image from the original chart size of 700px*700px
  6. An array of hex colors if you do not want to use Gruff’s default color scheme

Now, here is how you can use the chart module and the drawChart function:


That’s pretty much all the code. In the end you should have a chart generated like following:
A chart generated by Gruff

Share and Enjoy:
  • Digg
  • del.icio.us
  • description
  • Technorati
  • Reddit
  • Facebook
  • blogmarks
  • YahooMyWeb
  • Ma.gnolia

6 Comments »

  1. AnĂ­bal Rojas said,

    November 10, 2006 @ 9:44 am

    Unfortunalely I had a lot of trouble trying to install RMagick in Ubuntu :-(

    Off Topic: Register you blog at RubyCorner.com, a directory for blogs related to the Ruby Programming Language or any of the related technologies.

  2. Atif Khan said,

    November 10, 2006 @ 11:32 am

    Anibal,
    Did you try both gem and apt on Ubuntu to get RMagick? I don’t use my Ubuntu box for Ruby, but last time I checked it was one version behind in Ruby package for 64-bit version.

  3. Ivan said,

    February 25, 2007 @ 4:44 pm

    Thanks, using charts is essential in anyone Programming Lenguage , and to know it on Ruby is an advantage.

    Centro Software Libre

  4. The kettle sings said,

    April 25, 2007 @ 1:59 pm

    Atif,
    Thanks for the demo code to get me going.
    The demo call to the drawChart function does leave out variable declarations for violation1Arr, violation2Arr and timelineGHash. Easy enough to fix, but i dn’t kow if you meant it that way.

  5. Atif Khan said,

    April 26, 2007 @ 1:48 pm

    kettle sings,
    I am glad that you found this useful. Yes I did leave the variable declarations on purpose. That’s why I have “…” in the code block calling the drawChart() function. That means that you will have extra code in there.

  6. [TECH NOTES] Configuring Good Web Development Environment « Techii said,

    May 5, 2008 @ 1:53 pm

    [...] For charting support inmy web apps, the popular approach of using ‘gruff’ wrapping ImageMagick seemed to generate a lot of help requests, so I went for the lighter approach of using client side flash as described in the following blog. [...]

RSS feed for comments on this post · TrackBack URI

Leave a Comment