1 class GraphsController < ApplicationController
3 # To make caching easier, add a line like this to config/routes.rb:
4 # map.graph "graph/:action/:id/image.png", :controller => "graph"
6 # Then reference it with the named route:
7 # image_tag graph_url(:action => 'show', :id => 42)
11 # Uncomment to use your own theme or font
12 # See http://colourlovers.com or http://www.firewheeldesign.com/widgets/ for color ideas
14 # :colors => ['#663366', '#cccc99', '#cc6633', '#cc9966', '#99cc99'],
15 # :marker_color => 'white',
16 # :background_colors => ['black', '#333333']
18 # g.font = File.expand_path('artwork/fonts/VeraBd.ttf', RAILS_ROOT)
20 g.title = "Gruff-o-Rama"
22 g.data("Apples", [1, 2, 3, 4, 4, 3])
23 g.data("Oranges", [4, 8, 7, 9, 8, 9])
24 g.data("Watermelon", [2, 3, 1, 5, 6, 8])
25 g.data("Peaches", [9, 9, 10, 8, 7, 9])
27 g.labels = {0 => '2004', 2 => '2005', 4 => '2006'}
29 send_data(g.to_blob, :disposition => 'inline', :type => 'image/png', :filename => "gruff.png")
32 #The following section has been pasted directly fromworking pollarize graphs
33 #and hasn't been adopted to fit Selectricity yet
36 @poll = Poll.find(params[:id])
37 line = Gruff::Line.new
38 line.title = "Voters Per Day"
39 line.font = File.expand_path('/usr/X11R6/lib/X11/fonts/TTF/Vera.ttf', RAILS_ROOT)
40 line.data("#{@poll.name}", voter_days["voters_per_day"] )
41 line.labels = voter_days["days_hash"]
42 line.x_axis_label = "Date"
43 line.y_axis_label = "Number of Votes"
44 line.minimum_value = 0.0
46 send_data(line.to_blob,
47 :disposition => 'inline',
49 :filename => "dayvotes#{@poll.id}.png")
53 @poll = Poll.find(params[:id])
54 voter_times = Array.new
55 unique_days = Array.new
56 voters_per_day = Array.new
59 @poll.questions.each do |qstn|
60 qstn.votes.each do |vote|
61 voter_times << vote.time unless voter_times.any? {|utime| utime == vote.time}
65 #find all times in voter_times with the same date, and then concatenate
66 #that number onto votes_per_day
68 #doesn't work jsut yet
69 voter_times.each_with_index do |time, index|
71 unless unique_days.any? { |d1| d1.eql?(time.mon.to_s+"/"+time.day.to_s) }
72 unique_days << (time.mon.to_s+"/"+time.day.to_s)
73 count += (voter_times[(index+1)..-1].find_all {|t| t.mon == time.mon && t.day == time.day}).size
74 voters_per_day << count
78 unique_days.each_with_index do |fmtdate, index|
79 days_hash[index] = fmtdate
81 return { "voters_per_day" => voters_per_day, "days_hash" => days_hash }
84 #end copy/pasted section