From:
Date: Tue, 21 Aug 2007 17:56:23 +0000 (-0400)
Subject: Put in a pie graph for Plurality votes. ALso slimmed down victories_and_ties method...
X-Git-Url: https://projects.mako.cc/source/selectricity/commitdiff_plain/1fcd2d064dfef0dff9b07a40d0f5d30e662d1d86?ds=sidebyside
Put in a pie graph for Plurality votes. ALso slimmed down victories_and_ties method in rubyvote/condorcet.rb considerably.
---
diff --git a/app/controllers/graph_controller.rb b/app/controllers/graph_controller.rb
index c72da85..69577c6 100644
--- a/app/controllers/graph_controller.rb
+++ b/app/controllers/graph_controller.rb
@@ -121,12 +121,21 @@ class GraphController < ApplicationController
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,
- :data => ,
- :title => "Percentage of First Plce Votes")
+ :title => "Percentage of First Place Votes",
+ :data => data)
+ send_data(*pie.output)
+
+ end
private
def get_positions_info(election)
diff --git a/app/views/quickvote/results.rhtml b/app/views/quickvote/results.rhtml
index 874f725..3d5468c 100644
--- a/app/views/quickvote/results.rhtml
+++ b/app/views/quickvote/results.rhtml
@@ -181,4 +181,5 @@ by several other names.
<%=image_tag( graph_url( :action => 'votes_per_interval', :id => @election ))%>
<%= image_tag( graph_url( :action => 'borda_bar', :id => @election ) ) %>
-<%= image_tag( graph_url( :action => 'choices_positions', :id => @election ) ) %>
+<%= image_tag( graph_url( :action => 'choices_positions', :id => @election ) ) %>
+<%= image_tag(graph_url( :action => 'plurality_pie', :id => @election ) )%>
diff --git a/lib/rubyvote/condorcet.rb b/lib/rubyvote/condorcet.rb
index 0cc1696..0c6fd56 100644
--- a/lib/rubyvote/condorcet.rb
+++ b/lib/rubyvote/condorcet.rb
@@ -123,27 +123,18 @@ class CondorcetResult < ElectionResult
end
def victories_and_ties
- victors = Array.new
- victories_ties = Hash.new
+ victories_ties = {}
candidates = @matrix.keys.sort
candidates.each do |candidate|
candidates.each do |challenger|
next if candidate == challenger
diff = @matrix[candidate][challenger] - @matrix[challenger][candidate]
- if diff >= 0
- victors << [candidate, challenger, diff]
+ victories_ties[candidate] = {} unless victories_ties.key?(candidate)
+ if diff >= 0
+ victories_ties[candidate][challenger] = diff
end
end
- end
-
- victors.each do |list|
- if victories_ties.has_key?(list[0])
- victories_ties[list[0]][list[1]] = list[2]
- else
- victories_ties[list[0]] = Hash.new
- victories_ties[list[0]][list[1]] = list[2]
- end
end
return victories_ties