Recipe 15.6. Visually Displaying Data with Gruff


Problem

You want to visually display two datasets simultaneously as a line graph.

Solution

Use the Gruff graphing library by Geoffrey Grosenbach.

First, download and install the Gruff RubyGem if you haven't already:

sudo gem install gruff

Include the following in config/environment.rb:

require 'gruff'

Now, create a Graph Controller, and add a show method as follows:

app/controllers/graph_controller.rb:

class GraphController < ApplicationController   def show     graph = Gruff::Line.new(400)     graph.title = "Ruby Book Sales"      graph.theme_37signals     # sales data:     graph.data("2005", [80,120,70,90,140,110,200,550,460,691,1000,800])             graph.data("2004", [10,13,15,12,20,40,60,20,10,80,100,95])     # month labels:     graph.labels = {       0 => 'Jan',       1 => 'Feb',       2 => 'Mar',       3 => 'Apr',       4 => 'May',       5 => 'Jun',       6 => 'Jul',       7 => 'Aug',       8 => 'Sep',       9 => 'Oct',       10 => 'Nov',       11 => 'Dec',     }     graph.replace_colors(['red','blue','black'])     send_data(graph.to_blob,                :disposition => 'inline',                :type => 'image/png',                :filename => "book_sales.pdf")   end end

Discussion

Gruff is a graphing library for Ruby that uses RMagick to generate great looking graphs. With it, you can plot multiple datasets in color in a variety of different themes. Gruff can be used to create line, bar, and pie graphs.

The show method in the solution creates an object named graph as an instance of the Gruff::Line class. We've passed in 400 as the width of graph that is generated.

Next, we set the title and theme for the graph. If you have a specific font you'd like to use, you can specify it with the font attribute of graph:

graph.font = File.expand_path('artwork/fonts/Vera.ttf', RAILS_ROOT)

In the solution, we have some pretend sales data for Ruby books in 2004 and 2005. There are 12 data points in each set. To load the data, we call graph.data for each year. The data method takes name of the set as the first argument, and an array of numbers as the second.

Then assign labels to each of the 12 points; months of the year, in this case. It's not necessary to assign a label to each point. We could just specify the month at the beginning of each quarter, such as:

# quarter labels: graph.labels = {   0 => 'Jan',   3 => 'Apr',   6 => 'Jul',   9 => 'Oct', }

Set custom colors for the lines of the graph in a call to replace_colors. Note that you need to have one more color than the number of datasets you intend to draw. In this case our data is to be red and blue, with black satisfying the argument requirement:

graph.replace_colors(['red','blue','black'])

Finally the graph is displayed with a call to send_data, for which we supply the data, disposition (inline or attachment), type, and filename:

    send_data(graph.to_blob,                :disposition => 'inline',                :type => 'image/png',                :filename => "book_sales.pdf")

Figure 15-3 shows a line graph from the solution's data on programming language book sales trends.

Figure 15-3. A graph comparing book sales, made using Gruff


See Also

  • For more about Gruff, see http://rubyforge.org/projects/gruff

  • Section 15.7"




Rails Cookbook
Rails Cookbook (Cookbooks (OReilly))
ISBN: 0596527314
EAN: 2147483647
Year: 2007
Pages: 250
Authors: Rob Orsini

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net