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[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]}
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
-<% 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 %>
+
+
+<!-- This table shows how many times each choice was ranked above the other,
+ with percentages-->
<table class="voterbox">
<tr>
<td> </td>
<% candidates.each do |candidate| -%>
<th><%=h names[candidate] -%></th>
<% end -%>
-</tr>
+ </tr>
<% candidates.each do |winner| -%>
<tr>
</tr>
<% end -%>
</table>
+
+<!-- This table generates a margin of victory -->
+<table class="voterbox">
+ <% candidates.each do |victor| %>
+ <tr>
+ <th><%=h names[victor] %></th>
+ <% victories[victor].keys.each do |loser| %>
+ <% margin = victories[victor][loser]%>
+ <td><%=h names[loser] %>
+ <% if margin == 0%>
+ Tied!
+ <% else -%>
+ (<%= margin%>)
+ <% end -%>
+ </td>
+ <% end -%>
+ </tr>
+ <% end -%>
+</table>
+
+++ /dev/null
-<% victories, tied = @election.condorcet_result.victories_and_ties %>
-<% names = @election.names_by_id %>
-<% %>
-<table class="voterbox">
- <% victories.keys.each do |victor| %>
- <tr>
- <th><%=h names[victor] %></th>
- <% victories[victor].keys.each do |loser| %>
- <td><%=h names[loser] %> (<%= victories[victor][loser] %>)</td>
- <% end -%>
- </tr>
- <% end -%>
-</table>
-
-
-
-
<% end %>
</table>
-<%= render :partial => 'victories_ties' %>
-<%= render :partial => 'pref_table' %>
+<%= render :partial => 'pref_tables' %>
<%=image_tag( graph_url( :action => 'votes_per_interval', :id => @election ))%>
<br />
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