Fixed choices_positions to use GruffGraff class. Initialize method now had a way of
[selectricity-live] / app / controllers / graph_controller.rb
index 17e157809aab4fb5020a4c76ebdd6d850115ce8f..825688476485d114f1583cdb6774c6962e40528d 100644 (file)
@@ -12,8 +12,15 @@ class GraphController < ApplicationController
                                    RAILS_ROOT)
       
       # fill in the data with the optional data name
+      #Check to see if multiple datasets, if so, fill them all!     
+      if options[:data].size > 1 && options[:data].all?  {|i| i.is_a?(Array)}
+        options[:data].each do |array|
+          @graph.data( options.fetch(:data_name, "Data"), array)
+        end
+      else #one dimensional array, just pass it in
       @graph.data( options.fetch(:data_name, "Data"), options[:data] )
-
+      end
+      
       # set the labels or create an empty hash
       @graph.labels = options[:interval_labels] \
         if options.has_key?(:interval_labels) and \
@@ -85,44 +92,24 @@ class GraphController < ApplicationController
     @election = Election.find(params[:id])
     pref_tally = make_preference_tally(@election)
     
-    fulldata = get_positions_info(@election)
-    (0...@election.candidates.size).each do |i|
-      sbar.data("Candidate#{i+1}", fulldata[i])
-    end
-    
-    sbar = Gruff::Bar.new
-    sbar.title = "Times Voted in Each Position"
-    #sbar.theme = { :background_colors => ['#73BF26', '#ffffff'] }
-    sbar.font = File.expand_path('/usr/X11R6/lib/X11/fonts/TTF/Vera.ttf',
-                                 RAILS_ROOT)
-    
+    fulldata, labels = get_positions_info(@election)
+    labels = @candidates
     graph = GruffGraff.new( :graph_type => Gruff::Bar,
                             :data_name => @election.name,
-                            :data => data,
+                            :data => fulldata,
                             :interval_labels => labels,
-                            :title => "Times Candidate Was Voted Each Position",
-                            :x_axis_label => "Rank",
-                            :y_axis_label => "Number of Times")
-                                       
-    
-   
-    
-    sbar.x_axis_label = "Rank"
-    sbar.y_axis_label = "Number of Times Ranked"
-     
-    sbar.minimum_value = 0.0
-    send_data(sbar.to_blob, :disposition => 'inline', :type => 'image/png')
-                              
-    
-    #data, labels = get_positions_info(@election)
-    
+                            :title => "Times Voted in Each Position",
+                            :y_axis_label => "Number of Times Ranked",
+                            :x_axis_label => "Rank")
+    send_data(*graph.output) 
   end
+  
  private 
  
   def get_positions_info(election)
     buckets = Hash.new
     buckets2= Hash.new
-    ranks = Hash.new
+    rank_labels = Hash.new
   
     election.candidates.each do |candidate|
       buckets[candidate.id] = []
@@ -141,7 +128,13 @@ class GraphController < ApplicationController
       end
     end
     
-    return buckets2.values
+    election.votes.each do |vote|
+      vote.rankings.size.times do |i|
+        rank_labels[i] = (i+1).to_s
+      end
+    end
+   
+    return buckets2.values, rank_labels
     
   end
    
@@ -202,7 +195,7 @@ class GraphController < ApplicationController
     
     # Make a hash, buckets, indexed by time intervals and containing empty arrays
     # The time object must come first in addition! 
-    # i would start at 0, i+1 goes from 0 up till numcols
+    # i would start at 0, i+1 goes from 1 up till numcols
     numcols.times {|i| buckets[starttime + ((i+1)*interval_length)] = []}
      
     # Put votes into bucket according to the time interval to which they belong,

Benjamin Mako Hill || Want to submit a patch?