]> projects.mako.cc - selectricity/blob - vendor/plugins/sparklines/generators/sparklines/templates/controller.rb
1c69f290525082bfb9c65b50127c4f632c03ea70
[selectricity] / vendor / plugins / sparklines / generators / sparklines / templates / controller.rb
1 class SparklinesController < ApplicationController
2
3   # Handles requests for sparkline graphs from views.
4   #
5   # Params are generated by the sparkline_tag helper method.
6   #
7         def index
8                 # Make array from comma-delimited list of data values
9                 ary = []
10                 if params.has_key?('results') && !params['results'].nil?
11                   params['results'].split(',').each do |s|
12                           ary << s.to_i
13                   end
14                 end
15                 
16                 send_data( Sparklines.plot( ary, params ), 
17                                         :disposition => 'inline',
18                                         :type => 'image/png',
19                                         :filename => "spark_#{params[:type]}.png" )
20         end
21
22
23   # Use this type of method for sparklines that can be cached. (Doesn't work with the helper.)
24   #
25   # To make caching easier, add a line like this to config/routes.rb:
26   # map.sparklines "sparklines/:action/:id/image.png", :controller => "sparklines"
27   #
28   # Then reference it with the named route:
29   #   image_tag sparklines_url(:action => 'show', :id => 42)
30   def show
31     send_data(Sparklines.plot(
32                 [42, 37, 89, 74, 70, 50, 40, 30, 40, 50],
33                 :type => 'bar', :above_color => 'orange'
34               ), 
35               :disposition => 'inline', 
36               :type => 'image/png', 
37               :filename => "sparkline.png")
38   end
39
40 end

Benjamin Mako Hill || Want to submit a patch?