Module Sparklines
In: sparklines.rb

A library (in Ruby!) for generating sparklines.

Can be used to write to a file or make a web service with Rails or other Ruby CGI apps.

Idea and much of the outline for the source lifted directly from Joe Gregorio's Python Sparklines web service script.

Requires the RMagick image library.

Authors

Dan Nugent Original port from Python Sparklines library.

Geoffrey Grosenbachnubyonrails.topfunky.com — Conversion to module and addition of functions for using with Rails. Also changed functions to use Rails-style option hashes for parameters.

Tangent regarding RMagick

I had a heck of a time getting RMagick to work on my system so in the interests of saving other people the trouble here’s a little set of instructions on how to get RMagick working properly and with the right image formats.

  1. Install the zlib library
  2. With zlib in the same directory as the libpng library, install libpng
  3. Option step: Install the jpeg library (You need it to use jpegs and you might want to have it)
  4. Install ImageMagick from source. RMagick requires the ImageMagick headers, so this is important.
  5. Install RMagick from source. The gem is not reliable.
  6. Edit Magick-conf if necessary. I had to remove -lcms and -ltiff since I didn’t want those to be built and the libraries weren’t on my system.

Please keep in mind that these were only the steps that made RMagick work on my machine. This is a tricky library to get working. Consider using Joe Gregorio’s version for Python if the installation proves to be too cumbersome.

General Usage and Defaults

To use in a script:

        require 'rubygems'
        require 'sparklines'
        Sparklines.plot([1,25,33,46,89,90,85,77,42], :type => 'discrete', :height => 20)

An image blob will be returned which you can print, write to STDOUT, etc.

In Rails,

  • Install the ‘sparklines_generator’ gem (‘gem install sparklines_generator’)
  • Call ‘ruby script/generate sparklines’. This will copy the Sparklines controller and helper to your rails directories
  • Add "require ‘sparklines’" to the bottom of your config/environment.rb
  • Restart your fcgi’s or your WEBrick if necessary

And finally, add this to the controller whose view will be using sparklines:

        helper :sparklines

In your view, call it like this:

<%= sparkline_tag [1,2,3,4,5,6] %> <!— Gives you a smooth graph —>

Or specify details:

<%= sparkline_tag [1,2,3,4,5,6], :type => ‘discrete’, :height => 10, :upper => 80, :above_color => ‘green’, :below_color => ‘blue’ %>

Graph types:

 area
 discrete
 pie
 smooth

General Defaults:

 :type =>  'smooth'
 :height  =>  14px
 :upper  =>  50
 :above_color  =>  'red'
 :below_color  =>  'grey'
 :background_color  =>  'white'
 :line_color => 'lightgrey'

License

Licensed under the MIT license.

Methods

area   discrete   my_polyline   pie   plot   plot_error   plot_to_file   smooth  

Public Class methods

Creates a continuous area sparkline

  • results is an array of integer values between 0 and 100 inclusive
  • options is a hash that takes 4 parameters:

:step - An integer that determines the distance between each point on the sparkline. Defaults to 2.

:height - An integer that determines what the height of the sparkline will be. Defaults to 14

:upper - An ineger that determines the threshold for colorization purposes. Any value less than upper will be colored using below_color, anything above and equal to upper will use above_color. Defaults to 50.

:has_min - Determines whether a dot will be drawn at the lowest value or not. Defaulst to false.

:has_max - Determines whether a dot will be drawn at the highest value or not. Defaulst to false.

:has_last - Determines whether a dot will be drawn at the last value or not. Defaulst to false.

:min_color - A string or color code representing the color that the dot drawn at the smallest value will be displayed as. Defaults to blue.

:max_color - A string or color code representing the color that the dot drawn at the largest value will be displayed as. Defaults to green.

:last_color - A string or color code representing the color that the dot drawn at the last value will be displayed as. Defaults to red.

:above_color - A string or color code representing the color to draw values above or equal the upper value. Defaults to red.

:below_color - A string or color code representing the color to draw values below the upper value. Defaults to gray.

Creates a discretized sparkline

  • results is an array of integer values between 0 and 100 inclusive
  • options is a hash that takes 4 parameters:

:height - An integer that determines what the height of the sparkline will be. Defaults to 14

:upper - An integer that determines the threshold for colorization purposes. Any value less than upper will be colored using below_color, anything above and equal to upper will use above_color. Defaults to 50.

:above_color - A string or color code representing the color to draw values above or equal the upper value. Defaults to red.

:below_color - A string or color code representing the color to draw values below the upper value. Defaults to gray.

This is a function to replace the RMagick polyline function because it doesn’t seem to work properly.

  • draw - a RMagick::Draw object.
  • arr - an array of points (represented as two element arrays)

Creates a pie-chart sparkline

  • results - an array of integer values between 0 and 100 inclusive. Only the first integer will be accepted. It will be used to determine the percentage of the pie that is filled by the share_color
  • options - a hash that takes parameters:

:diameter - An integer that determines what the size of the sparkline will be. Defaults to 20

:share_color - A string or color code representing the color to draw the share of the pie represented by percent. Defaults to blue.

:remain_color - A string or color code representing the color to draw the pie not taken by the share color. Defaults to lightgrey.

Does the actually plotting of the graph. Calls the appropriate function based on the :type value passed. Defaults to ‘smooth.’

Draw the error Sparkline. Not implemented yet.

Writes a graph to disk with the specified filename, or "Sparklines.png"

Creates a smooth sparkline

  • results - an array of integer values between 0 and 100 inclusive
  • options - a hash that takes these optional parameters:

:step - An integer that determines the distance between each point on the sparkline. Defaults to 2.

:height - An integer that determines what the height of the sparkline will be. Defaults to 14

:has_min - Determines whether a dot will be drawn at the lowest value or not. Defaulst to false.

:has_max - Determines whether a dot will be drawn at the highest value or not. Defaulst to false.

:has_last - Determines whether a dot will be drawn at the last value or not. Defaulst to false.

:min_color - A string or color code representing the color that the dot drawn at the smallest value will be displayed as. Defaults to blue.

:max_color - A string or color code representing the color that the dot drawn at the largest value will be displayed as. Defaults to green.

:last_color - A string or color code representing the color that the dot drawn at the last value will be displayed as. Defaults to red.

[Validate]