Home arrow Ruby-on-Rails arrow Creating Graphs with Ruby

Creating Graphs with Ruby

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).

Author Info:
By: Apress Publishing
Rating: 5 stars5 stars5 stars5 stars5 stars / 6
March 15, 2010
  1. · Creating Graphs with Ruby
  2. · Graphing Data
  3. · Dissecting the Code
  4. · Creating a Line Chart
  5. · Dissecting the New Code

print this article

Creating Graphs with Ruby
(Page 1 of 5 )

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)

require 'gruff'

line_chart = Gruff::Bar.new() line_chart.labels = {0=>'Value (USD)'} line_chart.title = "My Guitar Collection"

{"'70 Strat"=>2500,
"'69 Tele"=>2000,
 "'02 Modded Mexi Strat Squier"=>400}.each do |guitar, value|
line_chart.data(guitar, value )


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.)

You can get Gruff documentation, sample graphs, and sample code from http://nubyonrails.com/pages/gruff.

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.  A chart of guitar values created using Gruff

Listing 3-2. Creating a Simple Chart with Scruffy (widget_chart_scruffy.rb)

require 'scruffy'

sprocket_output = [["Jan",500],

widget_output =   [["Jan",350],

graph = Scruffy::Graph.new(
                   :title => "Widget and Sprocket Output",
                   :theme => Scruffy::Themes::Keynote.new)

graph.add(:bar, 'Sprockets', sprocket_output.map { |s| s[1] })
graph.add(:line, 'Widgets', widget_output.map { |w| w[1] })

graph.point_markers = widget_output.map { |w| w[0] }

graph.render(      :width => 800,
                   :to => 'widgets_and_sprockets.png')

Save this as widget_chart_scruffy.rb . You can run the script in Listing 3-2 as follows:

ruby widget_chart_scruffy.rb

If you open the file widgets_and_sprockets.png, you should see something similar to Figure 3-2.

Figure 3-2.  Player graph created using Scruffy

As you may have surmised from Listing 3-2, you can add data to a Scruffy graph using a command like this:

graph.add(:some_chart_type,'Some_text_for_the_legend', some_array_of_values) 

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.)

You can find out more about Scruffy's various chart types at the Scruffy home page (http://scruffy.rubyforge.org/).

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.

blog comments powered by Disqus

- Ruby-on-Rails Faces Second Security Flaw in ...
- Ruby 2.0 Prepped for February 2013 Release
- Why LinkedIn Switched from Ruby on Rails
- Adding Style with Action Pack
- Handling HTML in Templates with Action Pack
- Filters, Controllers and Helpers in Action P...
- Action Pack and Controller Filters
- Action Pack Categories and Events
- Logging Out, Events and Templates with Actio...
- Action Pack Sessions and Architecture
- More on Action Pack Partial Templates
- Action Pack Partial Templates
- Displaying Error Messages with the Action Pa...
- Action Pack Request Parameters
- Creating an Action Pack Registration Form

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials