If you've ever had to format your data as charts or graphs, you know what a hassle it can be -- but you also know how important it is for those trying to get the big picture. This article will show you how to use Gruff, a Ruby-based graphing library, to make this common job easier. It is excerpted from chapter three of the book Practical Reporting with Ruby and Rails, written by David Berube (Apress; ISBN: 1590599330).
For reporting, you'll often need to format data as charts, which can come in a variety of formats--line, bar, area, and more. Such graphs won't be judged on functionality alone.Their appearance is a very important factor, so you must strive to make your graphs as attractive and understandable as possible.
Fortunately, graphing is a very common task, and several graphing utilities are available. This chapter introduces a few of these utilities, and then demonstrates how to build charts with Gruff, a powerful Ruby-based graphing library.
Choosing a Graphing Utility
You have many choices for creating charts with Ruby. For example, you can do simple charting in straight Hypertext Markup Language (HTML) and Cascading Style Sheets (CSS). Chapter 7 shows you how to use Markaby, a templating language for Ruby, to create your own HTML bar charts. Chapter 11 demonstrates how to use CSS helpers to create charts in Rails. Here, well look at the Gruff and Scruffy graphing libraries, and then use Gruff in a couple of examples.
Gruff (http://gruff.rubyforge.org/) provides a simple, Ruby-based interface to enter data and display details. After writing the code, you call a simple command to render the graph to a file. For example, if you had a collection of vintage guitars and wanted to display a simple bar chart with their values, you could do so as shown in Listing 3-1.
Note You'll need Gruff, ImageMagick, and RMagick installed to run this example. ImageMagick, an image-manipulation toolkit used by Gruff, is available from http://imagemagick.org. RMagick is the Ruby interface to ImageMagick that Gruff uses. Install them by running the commands gem install -y gruff and gem install rmagick .
Listing 3-1. Creating a Simple Chart with Gruff (guitar_chart.rb)
{"'70 Strat"=>2500, "'69 Tele"=>2000, "'02 Modded Mexi Strat Squier"=>400}.each do |guitar, value| line_chart.data(guitar, value ) end
line_chart.write("guitar_chart.png")
You can run the example as follows :
ruby guitar_chart.rb
The resulting chart is shown in Figure 3-1.
As you can see, it's not particularly complicated to make a simple chart. The labels attribute takes a hash of labels for each column, so you can have multiple columns if you so desire. The data method takes a label for the row of data, as well as an array of values for that row. (If you have only one value, as in Listing 3-1, you don't need to pass it as an array.)
Scruffy (http://scruffy.rubyforge.org/) is another popular graphing library. Scruffy offers a number of features that are not available with Gruff, but it's slightly more difficult to use than Gruff. Currently, Gruff has much more documentation available online and is more mature than Scruffy.
With Scruffy, you can mix graph types in the same graph, so you could, for example, have a chart with both line and bar elements. Suppose you were charting the output of a factory that builds widgets and sprockets. You could use the code in Listing 3-2 to create a bar and line chart with the data.
Note To run this example, you'll need to install Scruffy with the command gem install scruffy. You'll also need RMagick installed, so install that with gem install rmagick, if you haven't already done so.
Figure 3-1.Achart of guitar values created using Gruff
Listing 3-2.Creating a Simple Chart with Scruffy (widget_chart_scruffy.rb)
The add command is quite similar to Gruff's data method. This example is a bit more complex, as it parses some hashes and uses the keys of the first hash as labels for the x axis. (This assumes that they both have the same keys, which may or may not be true.)
Tip A graphing plugin for Rails called ZiYa offers very attractive graphs and is easy to use. Unfortunately, it's based on a commercial SWF component, called XML/SWF Charts, so not only will your users need Flash, but you'll also need to purchase the component. (Actually, you can use ZiYa without paying, but if your users click the graph, they'll be taken to the XML/SWF Charts home page, which is unacceptable for many purposes.) If you're looking for a commercially supported graphing library, ZiYa may be a good choice. Another option is an open source Flash charting plugin, one of which is covered in Chapter 5.
Now let's use Gruff to generate some interesting graphs.