| 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.
Dan Nugent Original port from Python Sparklines library.
Geoffrey Grosenbach — nubyonrails.topfunky.com — Conversion to module and addition of functions for using with Rails. Also changed functions to use Rails-style option hashes for parameters.
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.
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.
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,
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'
Licensed under the MIT license.
Creates a continuous area sparkline
: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
: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.
Creates a pie-chart sparkline
: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.’
Creates a smooth sparkline
: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.