]> projects.mako.cc - selectricity/blobdiff - app/controllers/graph_controller.rb
removed breakpointer
[selectricity] / app / controllers / graph_controller.rb
index 442bb65db975b9e4b650bd18815b560561595277..814eb363c437a0074abea9113fdc7e30e4a1a455 100644 (file)
@@ -3,11 +3,13 @@ class GraphController < ApplicationController
   class GruffGraff
   
     def initialize(options)
   class GruffGraff
   
     def initialize(options)
-      size = "700x400"
+      size = options[:size] ? options[:size] : "400x300" #allow custom sizing
       @graph = options[:graph_type].new(size)
       @graph = options[:graph_type].new(size)
-
-      @graph.theme = { :colors => ['#000000', '#00FFFF', '#FFCC00', '#990033'],
-                       :background_colors => ['#74ce00', '#ffffff'] }
+      
+      @graph.no_data_message = "No Voters"
+      
+      @graph.theme = { :colors => ['#005CD9', '#DC0D13', '#131313', '#990033'],
+                       :background_colors => ['#74CE00', '#FFFFFF'] }
       @graph.font = File.expand_path('/usr/X11R6/lib/X11/fonts/TTF/Vera.ttf',
                                    RAILS_ROOT)
       
       @graph.font = File.expand_path('/usr/X11R6/lib/X11/fonts/TTF/Vera.ttf',
                                    RAILS_ROOT)
       
@@ -72,6 +74,7 @@ class GraphController < ApplicationController
                             :data => data,
                             :interval_labels => labels,
                             :title => "Voters Over Time",
                             :data => data,
                             :interval_labels => labels,
                             :title => "Voters Over Time",
+                            :size => "700x400",
                             :x_axis_label => scale,
                             :y_axis_label => "Number of Votes")
     send_data(*graph.output)
                             :x_axis_label => scale,
                             :y_axis_label => "Number of Votes")
     send_data(*graph.output)
@@ -99,6 +102,7 @@ class GraphController < ApplicationController
     legend = Hash.new   
     alldata, labels = get_positions_info(@election)    
     @election.results unless @election.condorcet_result || @election.ssd_result
     legend = Hash.new   
     alldata, labels = get_positions_info(@election)    
     @election.results unless @election.condorcet_result || @election.ssd_result
+    ranked_candidates = @election.condorcet_result.ranked_candidates.flatten
     
     names = Hash.new
     candidates = @election.candidates.sort.collect {|candidate| candidate.id}
     
     names = Hash.new
     candidates = @election.candidates.sort.collect {|candidate| candidate.id}
@@ -106,7 +110,6 @@ class GraphController < ApplicationController
       names[candidate]= (Candidate.find(candidate)).name
     end
     
       names[candidate]= (Candidate.find(candidate)).name
     end
     
-    ranked_candidates = @election.condorcet_result.ranked_candidates.flatten
     ranked_candidates.each_with_index \
     {|candidate, index| legend[names[candidate]] = alldata[index]}
     
     ranked_candidates.each_with_index \
     {|candidate, index| legend[names[candidate]] = alldata[index]}
     
@@ -119,6 +122,24 @@ class GraphController < ApplicationController
     send_data(*graph.output) 
   end
   
     send_data(*graph.output) 
   end
   
+  def plurality_pie
+    @election = Election.find(params[:id])
+    @election.results unless @election.plurality_result || @election.approval_result
+    votes = @election.votes.size
+    data = Hash.new
+    names = @election.names_by_id
+    
+    @election.plurality_result.points.each do |candidate, votes|
+      data[names[candidate]] = votes
+    end
+     
+    pie = GruffGraff.new ( :graph_type => Gruff::Pie,
+                           :title => "Percentage of First Place Votes",
+                           :data => data)
+    send_data(*pie.output)
+                           
+  end
+  
  private 
   def get_positions_info(election)
     buckets = Hash.new
  private 
   def get_positions_info(election)
     buckets = Hash.new

Benjamin Mako Hill || Want to submit a patch?