+ # produce a graph of votes per day during an election
+ def votes_per_day
+ @election = Election.find(params[:id])
+ data, labels = get_votes_per_day_data(@election)
+
+ graph = GruffGraff.new( :graph_type => Gruff::Line,
+ :data_name => @election.name,
+ :data => data,
+ :interval_labels => labels,
+ :title => "Voters Per Day",
+ :x_axis_label => "Data",
+ :y_axis_label =>"Number of Votes")
+ send_data(*graph.output)
+ end
+
+ #will place votes in a fixed number of intervals, and shows votes over time
+ def votes_per_interval
+ @election = Election.find(params[:id])
+ data, labels, scale = get_votes_per_interval_data(@election)
+
+ hide_legend = true
+
+ graph = GruffGraff.new( :graph_type => Gruff::Line,
+ :data_name => @election.name,
+ :data => data,
+ :interval_labels => labels,
+ :title => "Voters Over Time",
+ :size => "330x232",
+ :legend_font_size => 40,
+ :title_font_size => 50,
+ :marker_count => 2,
+ :marker_font_size => 30,
+ :marker_color => '#999999',
+ :x_axis_label => scale,
+ :y_axis_label => "Number of Votes")
+ send_data(*graph.output)
+ end
+
+ def borda_bar
+ @election = Election.find(params[:id])
+ @election.results unless @election.borda_result
+ data, labels = get_borda_points(@election.borda_result)
+
+ size = "400x300"
+ size = "580x300" if @election.candidates.size >= 5
+
+ if @election.candidates.size >= 5
+ marker_font_size = 17
+ else
+ marker_font_size = 20
+ end
+
+ graph = GruffGraff.new( :graph_type => Gruff::Bar,
+ :data_name => @election.name,
+ :data => data,
+ :interval_labels => labels,
+ :size => size,
+ :title => "Points Per Candidate",
+ :marker_color => '#999999',
+ :marker_font_size => marker_font_size,
+ :y_axis_label => "Points",
+ :x_axis_label => "Candidates")
+ send_data(*graph.output)
+ end
+ #Acording to Tufte, small, concomparitive, highly labeled data sets usually
+ #belong in tables. The following is a bar graph...but would it be better
+ #as a table?
+ def choices_positions
+ @election = Election.find(params[:id])
+ 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}
+ candidates.each do |candidate|
+ names[candidate]= (Candidate.find(candidate)).name
+ end
+
+ ranked_candidates.each_with_index \
+ {|candidate, index| legend[names[candidate]] = alldata[index]}
+
+ graph = GruffGraff.new( :graph_type => Gruff::Bar,
+ :data => legend,
+ :interval_labels => labels,
+ :title => "Times Voted in Each Position",
+ :y_axis_label => "Number of Times Ranked",
+ :x_axis_label => "Rank")
+ 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
+ buckets2= Hash.new
+ rank_labels = Hash.new
+
+ #attach the ranking to the candidate's array to which is belongs
+ #creating a key if necessary
+ election.votes.each do |vote|
+ vote.rankings.each do |ranking|
+
+ unless buckets.has_key?(ranking.candidate_id)
+ buckets[ranking.candidate_id] = []
+ end
+ buckets[ranking.candidate_id] << ranking.rank
+
+ end
+ end
+
+ #count how many times each candidate has been ranked at a certain level
+ buckets.each_pair do |id, array|
+ (1..election.candidates.size).each do |i|
+ buckets2[id] = [] unless buckets2.has_key?(id)
+ buckets2[id] << (array.find_all {|rank| rank == i}).size
+ end
+ end
+
+ #sort by amount of 1st place votes
+ sorted_data = buckets2.values.sort {|a,b| b[0] <=> a[0]}
+
+ election.votes.each do |vote|
+ vote.rankings.size.times do |i|
+ rank_labels[i] = (i+1).to_s
+ end
+ end
+
+ return sorted_data, rank_labels