+
+ def get_votes_per_interval_data(election)
+ labels_hash = Hash.new
+ buckets = Hash.new
+ total_per_interval = Array.new
+ interval_type = ""
+
+ starttime = election.startdate
+ timedelta = Time.now - starttime
+ numcols = 10
+ interval_length = timedelta/numcols
+
+ # Make a hash, buckets, indexed by time intervals and containing empty arrays
+ # The time object must come first in addition!
+ # i would start at 0, i+1 goes from 1 up till numcols
+ numcols.times {|i| buckets[starttime + ((i+1)*interval_length)] = []}
+
+ # Put votes into bucket according to the time interval to which they belong,
+ # referenced by their key
+ # Will build a graph over time, as each successive interval will have more
+ # vote objects
+ election.votes.each do |vote|
+ buckets.keys.sort.each do |inter|
+ if vote.time < inter
+ buckets[inter] << vote
+ end
+ end
+ end
+
+ total_per_interval = buckets.keys.sort.collect {|key| buckets[key].size}
+
+ # Create the hash for the labels. Each graph has ten columns, and three
+ # will be labeled
+ if timedelta < 2.hours #under two hours use minutes for labels
+ labels_hash[0] = starttime.min.to_s
+ labels_hash[(numcols/2)-1] = (starttime + (timedelta/2)).min.to_s
+ labels_hash[numcols-1] = Time.now.min.to_s
+ interval_type = "Minute of the Hour"
+ elsif timedelta < 2.days #more than 2 hours means use hours for labels
+ labels_hash[0] = starttime.hour.to_s
+ labels_hash[(numcols/2)-1] = (starttime + (timedelta/2)).hour.to_s
+ labels_hash[numcols-1] = Time.now.hour.to_s
+ interval_type = "Hour of the Day on 24 hour scale"
+ else #more than 2 days means use dates for labels
+ labels_hash[0] = (Date.parse(starttime.to_s)).to_s
+ labels_hash[(numcols/2)-1] = (Date.parse((starttime + (timedelta/2)).to_s)).to_s
+ labels_hash[numcols-1] = (Date.today).to_s
+ interval_type = "The Date"
+ end
+
+ # Make sure to return an array for data and hash for labels
+ return total_per_interval, labels_hash, interval_type
+ end
+
+ def get_borda_points(result)
+ #points holds how mnay points each candidate has received in array form
+ #becasue Gruff::Bar#data takes only an array
+ points = Array.new
+ labels = Hash.new
+
+ #Populate points with an sorted array from election.votes hash
+ #biggest to smallest will go from left to right
+ points = result.election.votes.sort do |a, b|
+ b[1] <=> a[1]
+ end.collect {|i| i[1]}
+
+ #make the labels
+ result.ranked_candidates.each_with_index do |candidate, index|
+ labels[index] = Candidate.find(candidate).name
+ end
+
+ return points, labels
+ end
+
+ #most vote result objects require an array of vote arrays, which this will make
+ def make_preference_tally(election)
+ preference_tally = Array.new
+ @election.voters.each do |voter|
+ next unless voter.voted?
+ preference_tally << voter.vote.rankings.sort.collect \
+ { |ranking| ranking.candidate.id }
+ end
+ return preference_tally
+ end