Prev | Current Page 61 | Next

David Berube

"Practical Reporting with Ruby and Rails"

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


Pages:
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
Timo Maas Limahl Moby Loona obroncyaskalonu