Sparklines Graph Library for Ruby 1 comments

posted Thursday, July 28, 2005 by glyph

Welcome to Newbie on Rails!

This site will post (approximately) weekly articles for the beginning Rails developer. You won’t find witty commentary, snappy charts or flowers, but you will find original, useful articles.

Episode One: Graph-o-Rama

UPDATE: Please see the updated instructions at my Sparklines page. UPDATE

Sparklines are small graphs that are usually embedded within the text of a paragraph or placed inside tables. Thanks to code from Dan Nugent, I put together a Ruby library that can be used alone or with Rails.

Pie—Shows a single percentage
Smooth—Shows continuous data
Area—Highlights data with a colored threshold
Discrete—Uses vertical lines

While it’s not as clever as why’s pure Ruby implementation, it works well and is easy to use (if you have RMagick installed). It also works in all modern browsers that support PNG and has the variations shown above.

In this article I will cover

  • How to Use It
  • How to Install
  • The Rough Underbelly of Ruby Symbols

How to use it

Installing it is dead simple, and so is using it.

In a controller, require the library and its helper:

require_dependency 'spark'

class MyController < ApplicationController
  helper :spark

end

In a view, call the spark_tag helper with some data and some options.

<%= spark_tag [12, 22, 34, 42, 33], 
    :type => 'smooth',
    :height => 15 %>

Defaults are set such that you really don’t need to provide anything other than an array of numbers, but you can specify many options, such as height, colors, and type of graph.

And there’s no reason you need to stop at tiny graphs only! Here’s a pie graph with diameter of 200:

Installing

UPDATE: Please see the updated instructions at my Sparklines page.

Other Features

The library can also be used independently. For example, to write a graph to a file:

#!/usr/bin/ruby
require 'spark'

Spark.plot_to_file('pie_spark.png', 
    [12,32,44], 
    :type => 'pie'

Other Documentation

RDoc-style documentation is also provided.

The Rough Underbelly of Ruby Symbols

When I first started using Ruby, symbols were a little confusing and a little exotic. But mostly, they made my text editor show pretty colors! Ooohhh…

What are symbols? They are unchangeable strings. As with everything else in Ruby, they are an object. And since they are a different object than String, they act differently than a string.

For example:

"foo" == "foo" 
=> true

:foo == "foo" 
=> false

:foo == "foo".to_sym
=> true

:foo.to_s == "foo" 
=> true

So the string “foo” doesn’t equal the symbol :foo. However, you do have a few methods available for switching between the two:

a_string.to_sym -- Converts a string to a symbol

:a_symbol.to_s -- Converts a symbol to a string

Since symbols are a different object than a string, you have to be careful when you use them in hashtables:

hash = { :foo => "a",
  "foo" => "b"}

hash[:foo]
=> "a" 

hash["foo"]
=> "b" 

How does Rails do it? HashWithIndifferentAccess allows things like @params and @session to work with either Symbols or Strings, treating them equally.

However, in your own libraries or other use within Rails, be consistent and use one or the other.

Geoffrey Grosenbach

1 comments

Leave a response

Your Comment

Nuby on Rails

Geoffrey Grosenbach / Ruby / Code / Graphics / Design / Rails / Merb / Javascript / CSS

Ads by The Lounge

Manufactured with

Subscribe

Subscribe (RSS)