Sparklines Now Gem-i-fied

posted Monday, August 1, 2005 by glyph

Sparklines are now available as a gem, and as a Rails generator! There is also a RubyForge Project.

In order to not conflict with the existing Nitro Spark Ruby project, the name has been changed to ’Sparklines.’ So if you have tried it out in the last few days, change your code to reference ”sparklines” instead of ”spark.” The helper tag has also been changed to the singular sparkline_tag.

Also, there is a new :line_color option so you can set the color of the smooth graph’s line. Thanks to all those who offered suggestions.

At any rate, download the newest version!

Quick install

To install:

sudo gem install sparklines
sudo gem install sparklines_generator

Or download here for manual installation:

Sparklines 0.2.1

RDoc API Documentation

If you’re using Rails, you only have three more steps to get going:

  1. Run the generator for your Rails project (shown below)
  2. Add “require ‘sparklines’” to the bottom of your config/environment.rb
  3. Add “helper :sparklines” to your relevant controller
  4. Start making sparklines!

Note: You can still copy ‘sparklines.rb’ to your app’s ‘lib’ directory if you are working on a server without root access.

Step-by-Step

After installing both gems, you can run the Rails generator to copy the Sparkline controller and helper files.

ruby script/generate sparklines

Open up config/environment.rb and add the following line to the bottom of the file:

require 'sparklines'

Now, open a controller where you would like to use sparklines. Add the helper:

class SpecialReportController < ApplicationController
  helper :sparklines

end

Now you are in business! Go nuts in any of those controllers’ views!

<%= sparkline_tag [10,20,30], :type => 'pie' %>

Troubleshooting

No function sparklines_tag The helper function has been changed to the singular “sparkline_tag” instead of the plural. If you downloaded it during the 15 minutes that the plural version was up, just update your gem (‘gem install sparklines_generator’) and re-generate the files in your rails app (‘rails script/server generate sparklines’).

RMagick gem isn’t found If you installed RMagick separately of the gem system, you might have to reinstall. I just re-installed with “gem install rmagick” and it worked fine. If not, try editing your Magick-config file (mine was in /usr/local/bin) to remove the -lcms and/or -ltiff flags.

RMagick is installed, but graphs won’t show up If your server was running before you installed the gems, you might have to restart WEBrick or your FCGI processes.

Enjoy!

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)