From 6a1c545f073430dd46bf5ad89b0450a16c659b00 Mon Sep 17 00:00:00 2001 From: Date: Tue, 21 Aug 2007 12:57:40 -0400 Subject: [PATCH] Finished organzing preferential vote tables into one partial. Also changed RubyVote's victories_and_ties method to return a hash of hashes which stores both victories and ties. --- app/controllers/graph_controller.rb | 11 +++++- .../{_pref_table.rhtml => _pref_tables.rhtml} | 34 +++++++++++++++---- app/views/quickvote/_victories_ties.rhtml | 17 ---------- app/views/quickvote/results.rhtml | 3 +- lib/rubyvote/condorcet.rb | 19 +++++------ 5 files changed, 47 insertions(+), 37 deletions(-) rename app/views/quickvote/{_pref_table.rhtml => _pref_tables.rhtml} (50%) delete mode 100644 app/views/quickvote/_victories_ties.rhtml diff --git a/app/controllers/graph_controller.rb b/app/controllers/graph_controller.rb index 442bb65..c72da85 100644 --- a/app/controllers/graph_controller.rb +++ b/app/controllers/graph_controller.rb @@ -99,6 +99,7 @@ class GraphController < ApplicationController 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} @@ -106,7 +107,6 @@ class GraphController < ApplicationController 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]} @@ -119,6 +119,15 @@ class GraphController < ApplicationController send_data(*graph.output) end + def plurality_pie + @election = Election.find(params[:id]) + + + + pie = GruffGraff.new ( :graph_type => Gruff::Pie, + :data => , + :title => "Percentage of First Plce Votes") + private def get_positions_info(election) buckets = Hash.new diff --git a/app/views/quickvote/_pref_table.rhtml b/app/views/quickvote/_pref_tables.rhtml similarity index 50% rename from app/views/quickvote/_pref_table.rhtml rename to app/views/quickvote/_pref_tables.rhtml index 011fcbe..66e0cbf 100644 --- a/app/views/quickvote/_pref_table.rhtml +++ b/app/views/quickvote/_pref_tables.rhtml @@ -1,17 +1,19 @@ -<% candidates = @election.candidates.sort.collect {|candidate| candidate.id}-%> +<% candidates = @election.condorcet_result.ranked_candidates.flatten -%> <% voters = @election.voters.size %> <% matrix = @election.condorcet_result.matrix %> -<% names = Hash.new -%> -<% candidates.each do |candidate| -%> - <%names[candidate] = Candidate.find(candidate).name -%> -<% end -%> +<% victories = @election.condorcet_result.victories_and_ties %> +<% names = @election.names_by_id %> + + + <% candidates.each do |candidate| -%> <% end -%> - + <% candidates.each do |winner| -%> @@ -30,3 +32,23 @@ <% end -%>
<%=h names[candidate] -%>
+ + + + <% candidates.each do |victor| %> + + + <% victories[victor].keys.each do |loser| %> + <% margin = victories[victor][loser]%> + + <% end -%> + + <% end -%> +
<%=h names[victor] %><%=h names[loser] %> + <% if margin == 0%> + Tied! + <% else -%> + (<%= margin%>) + <% end -%> +
+ diff --git a/app/views/quickvote/_victories_ties.rhtml b/app/views/quickvote/_victories_ties.rhtml deleted file mode 100644 index 993caa8..0000000 --- a/app/views/quickvote/_victories_ties.rhtml +++ /dev/null @@ -1,17 +0,0 @@ -<% victories, tied = @election.condorcet_result.victories_and_ties %> -<% names = @election.names_by_id %> -<% %> - - <% victories.keys.each do |victor| %> - - - <% victories[victor].keys.each do |loser| %> - - <% end -%> - - <% end -%> -
<%=h names[victor] %><%=h names[loser] %> (<%= victories[victor][loser] %>)
- - - - diff --git a/app/views/quickvote/results.rhtml b/app/views/quickvote/results.rhtml index 2999e6b..874f725 100644 --- a/app/views/quickvote/results.rhtml +++ b/app/views/quickvote/results.rhtml @@ -176,8 +176,7 @@ by several other names.

<% end %> -<%= render :partial => 'victories_ties' %> -<%= render :partial => 'pref_table' %> +<%= render :partial => 'pref_tables' %> <%=image_tag( graph_url( :action => 'votes_per_interval', :id => @election ))%>
diff --git a/lib/rubyvote/condorcet.rb b/lib/rubyvote/condorcet.rb index d0210ef..0cc1696 100644 --- a/lib/rubyvote/condorcet.rb +++ b/lib/rubyvote/condorcet.rb @@ -124,32 +124,29 @@ class CondorcetResult < ElectionResult def victories_and_ties victors = Array.new - ties = Array.new - victories = Hash.new + victories_ties = Hash.new 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] - elsif diff == 0 && ties.include?([challenger, candidate]) == false - ties << [candidate, challenger] + if diff >= 0 + victors << [candidate, challenger, diff] end end end victors.each do |list| - if victories.has_key?(list[0]) - victories[list[0]][list[1]] = list[2] + if victories_ties.has_key?(list[0]) + victories_ties[list[0]][list[1]] = list[2] else - victories[list[0]] = Hash.new - victories[list[0]][list[1]] = list[2] + victories_ties[list[0]] = Hash.new + victories_ties[list[0]][list[1]] = list[2] end end - return victories, ties + return victories_ties end def ranked_candidates -- 2.30.2