* Renamed graphs_controller to graph_controller.
author<mako@atdot.cc> <>
Wed, 8 Aug 2007 16:27:02 +0000 (12:27 -0400)
committer<mako@atdot.cc> <>
Wed, 8 Aug 2007 16:27:02 +0000 (12:27 -0400)
* Reviewed and fixed up the graph_controller code.

app/controllers/graph_controller.rb [new file with mode: 0644]
app/controllers/graphs_controller.rb [deleted file]
app/views/quickvote/results.rhtml
config/routes.rb

diff --git a/app/controllers/graph_controller.rb b/app/controllers/graph_controller.rb
new file mode 100644 (file)
index 0000000..ebc5da7
--- /dev/null
@@ -0,0 +1,61 @@
+class GraphController < ApplicationController
+
+  # produce a graph of votes per day during an election
+  def votes_per_day
+    
+    @election = Election.find(params[:id])
+    data, labels = get_votes_per_day_data(@election)
+
+    line = Gruff::Line.new
+    line.title = "Voters Per Day"
+    line.font = File.expand_path('/usr/X11R6/lib/X11/fonts/TTF/Vera.ttf',
+                                 RAILS_ROOT)
+
+    line.data("#{@election.name}", data )
+    line.labels = labels
+
+    line.x_axis_label = "Date"
+    line.y_axis_label = "Number of Votes"
+    line.minimum_value = 0.0
+
+    line.draw
+    send_data(line.to_blob, :disposition => 'inline', :type => 'image/png')
+  end
+  # generate the data and labels for each graph
+  def get_votes_per_day_data(election)
+
+    voter_times = Array.new
+    unique_days = Array.new
+    voters_per_day = Array.new
+    days_hash = Hash.new
+    
+    election.votes.each do |vote|
+      unless voter_times.any? {|utime| utime == vote.time}
+        voter_times << vote.time
+      end
+    end
+    
+    voter_times.sort!
+  
+    voter_times.each_with_index do |time, index| 
+      count = 1
+      # TODO: add comment
+      unless unique_days.any? { |d1| d1.eql?(time.mon.to_s + "/" + time.day.to_s) }
+        unique_days << (time.mon.to_s + "/" + time.day.to_s)
+        count += (voter_times[(index+1)..-1].find_all \
+          {|t| t.mon == time.mon && t.day == time.day}).size
+        voters_per_day << count
+      end      
+    end
+    
+    unique_days.each_with_index do |fmtdate, index|
+      days_hash[index] = fmtdate
+    end    
+
+    # return the data and the labels
+    return voters_per_day, days_hash
+   
+  end
+
+end
diff --git a/app/controllers/graphs_controller.rb b/app/controllers/graphs_controller.rb
deleted file mode 100644 (file)
index 101dc6d..0000000
+++ /dev/null
@@ -1,83 +0,0 @@
-class GraphsController < ApplicationController
-
-  # To make caching easier, add a line like this to config/routes.rb:
-  # map.graph "graph/:action/:id/image.png", :controller => "graph"
-  #
-  # Then reference it with the named route:
-  #   image_tag graph_url(:action => 'show', :id => 42)
-
-  def show
-    g = Gruff::Line.new
-    # Uncomment to use your own theme or font
-    # See http://colourlovers.com or http://www.firewheeldesign.com/widgets/ for color ideas
-#     g.theme = {
-#       :colors => ['#663366', '#cccc99', '#cc6633', '#cc9966', '#99cc99'],
-#       :marker_color => 'white',
-#       :background_colors => ['black', '#333333']
-#     }
-#     g.font = File.expand_path('artwork/fonts/VeraBd.ttf', RAILS_ROOT)
-
-    g.title = "Gruff-o-Rama"
-    
-    g.data("Apples", [1, 2, 3, 4, 4, 3])
-    g.data("Oranges", [4, 8, 7, 9, 8, 9])
-    g.data("Watermelon", [2, 3, 1, 5, 6, 8])
-    g.data("Peaches", [9, 9, 10, 8, 7, 9])
-
-    g.labels = {0 => '2004', 2 => '2005', 4 => '2006'}
-
-    send_data(g.to_blob, :disposition => 'inline', :type => 'image/png', :filename => "gruff.png")
-  end
-  
-  #I've started to modify the following section to fit Selectricity, still 
-  #desn't seem towork, issue seems tobe realted to path
-  def day_votes
-  
-    @election = Election.find(params[:id])
-    line = Gruff::Line.new
-    line.title = "Voters Per Day"
-    line.font = File.expand_path('/usr/X11R6/lib/X11/fonts/TTF/Vera.ttf', RAILS_ROOT)
-    line.data("#{@election.name}", voter_days["voters_per_day"] )
-    line.labels = voter_days["days_hash"]
-    line.x_axis_label = "Date"
-    line.y_axis_label = "Number of Votes"
-    line.minimum_value = 0.0
-    line.draw
-    send_data(line.to_blob,
-              :disposition => 'inline',
-              :type => 'image/png',
-              :filename => "dayvotes#{@poll.id}.png")
-  end
-  
-  def voter_days
-    @election = Election.find(params[:id])
-    voter_times = Array.new
-    unique_days = Array.new
-    voters_per_day = Array.new
-    days_hash = Hash.new
-    
-    @election.votes.each do |vote|
-        voter_times << vote.time unless voter_times.any? {|utime| utime == vote.time}
-    end
-    
-    voter_times.sort!
-  
-    voter_times.each_with_index do |time, index| 
-      count = 1
-      unless unique_days.any? { |d1| d1.eql?(time.mon.to_s+"/"+time.day.to_s) }
-        unique_days << (time.mon.to_s+"/"+time.day.to_s)
-        count += (voter_times[(index+1)..-1].find_all {|t| t.mon == time.mon && t.day == time.day}).size
-        voters_per_day << count
-      end      
-    end
-    
-    unique_days.each_with_index do |fmtdate, index|
-      days_hash[index] = fmtdate
-    end    
-  return { "voters_per_day" => voters_per_day, "days_hash" => days_hash }
-   
-  end
-  #end section
-
-
-end
index 7152072d4c54d26c641842f880f3c9384296ca84..e06b5d6b32b91dc855cc2ee46cb46c135457d61b 100644 (file)
@@ -162,6 +162,4 @@ by several other names.</p>
 <% end %>
 </table>
 
-<img src="<% url_for(:action => 'day_votes', :id => @election)%>" />
-<%= image_tag( graph_url( :action => 'day_votes', :id => @election ) ) %>
-<%= image_tag( graph_url( :action => 'show' ) ) %>
+<%= image_tag( graph_url( :action => 'votes_per_day', :id => @election ) ) %>
index a05b80376764f5dc4829db0b02f06bfc21a3961a..ee64eabaa4a50025e4a467d04ade5b11a782d1b9 100644 (file)
@@ -34,8 +34,7 @@ ActionController::Routing::Routes.draw do |map|
   #
   # Then reference it with the named route:
   #   image_tag graph_url(:action => 'show', :id => 42)
-  map.graph "graphs/:action/:id/image.png", :controller => "graphs"      
-  map.graph "graphs/:action/image.png", :controller => "graphs" #pics w/o id's
+  map.graph "graph/:action/:id/graph.png", :controller => "graph"
   
   # Install the default route as the lowest priority.
   map.connect ':controller/:action/:id.:format'

Benjamin Mako Hill || Want to submit a patch?