Ruby Weekly is a weekly newsletter covering the latest Ruby and Rails news.

Scruffy: A beautiful graphing toolkit for Ruby

By Peter Cooper / August 17, 2006

Scruffy

Scruffy is a new graphing toolkit for Ruby developed by Brasten Sager. It's highly customizable and powerful. You can change the backgrounds, mix different types of graph together, change the graphics used for the points, change the line types, etc. You can also render to different types of output. Brasten presents some code examples at his blog. For demonstration, some is repeated below:

graph = Scruffy::Graph.new
graph.title = "Comparative Agent Performance"
graph.value_formatter = Scruffy::Formatters::Percentage.new(:precision => 0)
graph.add :stacked do |stacked|
	stacked.add :bar, 'Jack', [30, 60, 49, 29, 100, 120]
	stacked.add :bar, 'Jill', [120, 240, 0, 100, 140, 20]
	stacked.add :bar, 'Hill', [10, 10, 90, 20, 40, 10]
end
graph.point_markers = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']

graph.render(:width => 800, :as =>JPG’)

(Thanks to Brasten for fixing my sloppy cut and paste job. See comments.)

Comments

  1. Sean Cribbs says:

    Not a big deal, but might that first line of the code sample be missing a do |graph| ?

  2. brasten says:

    No, and the 'end' after graph.point_markers shouldn't be there. That stuff isn't supposed to be in a block:

    graph = Scruffy::Graph.new
    graph.title = "Comparative Agent Performance"
    graph.value_formatter = Scruffy::Formatters::Percentage.new(:precision => 0)
    graph.add :stacked do |stacked|
    stacked.add :bar, 'Jack', [30, 60, 49, 29, 100, 120]
    stacked.add :bar, 'Jill', [120, 240, 0, 100, 140, 20]
    stacked.add :bar, 'Hill', [10, 10, 90, 20, 40, 10]
    end
    graph.point_markers = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']

    graph.render(:width => 800, :as => ‘JPG’)

  3. Peter Cooper says:

    Oops. Sorry about that. It was a bit of a quick hack together. I will now fix with your code. Thanks!

  4. Peter Cooper says:

    Now fixed.

  5. brasten says:

    Thanks Peter! To be honest I'd read that block of code here a dozen times and didn't notice that until Sean said something. :)

Other Posts to Enjoy

Twitter Mentions