c8108536b214262130f3f5f96134ffced3abaa54
[selectricity] / app / controllers / graphs_controller.rb
1 class GraphsController < ApplicationController
2
3   # To make caching easier, add a line like this to config/routes.rb:
4   # map.graph "graph/:action/:id/image.png", :controller => "graph"
5   #
6   # Then reference it with the named route:
7   #   image_tag graph_url(:action => 'show', :id => 42)
8
9   def show
10     g = Gruff::Line.new
11     # Uncomment to use your own theme or font
12     # See http://colourlovers.com or http://www.firewheeldesign.com/widgets/ for color ideas
13 #     g.theme = {
14 #       :colors => ['#663366', '#cccc99', '#cc6633', '#cc9966', '#99cc99'],
15 #       :marker_color => 'white',
16 #       :background_colors => ['black', '#333333']
17 #     }
18 #     g.font = File.expand_path('artwork/fonts/VeraBd.ttf', RAILS_ROOT)
19
20     g.title = "Gruff-o-Rama"
21     
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])
26
27     g.labels = {0 => '2004', 2 => '2005', 4 => '2006'}
28
29     send_data(g.to_blob, :disposition => 'inline', :type => 'image/png', :filename => "gruff.png")
30   end
31   
32   #The following section has been pasted directly fromworking pollarize graphs
33   #and hasn't been adopted to fit Selectricity yet
34   
35   def day_votes
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
45     line.draw
46     send_data(line.to_blob,
47               :disposition => 'inline',
48               :type => 'image/png',
49               :filename => "dayvotes#{@poll.id}.png")
50   end
51   
52   def voter_days
53     @poll = Poll.find(params[:id])
54     voter_times = Array.new
55     unique_days = Array.new
56     voters_per_day = Array.new
57     days_hash = Hash.new
58     
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}
62       end
63     end
64     voter_times.sort!
65     #find all times in voter_times with the same date, and then concatenate 
66     #that number onto votes_per_day
67     #
68     #doesn't work jsut yet
69     voter_times.each_with_index do |time, index| 
70       count = 1
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
75       end      
76     end
77     
78     unique_days.each_with_index do |fmtdate, index|
79       days_hash[index] = fmtdate
80     end    
81   return { "voters_per_day" => voters_per_day, "days_hash" => days_hash }
82    
83   end
84   #end copy/pasted section
85
86
87 end

Benjamin Mako Hill || Want to submit a patch?