]> projects.mako.cc - selectricity/blobdiff - app/controllers/graph_controller.rb
add votemail
[selectricity] / app / controllers / graph_controller.rb
index ebc5da7c8f066535509028632a22cc408425940c..f6dc5d5fc3f32fc2012f6435932503728b49f69f 100644 (file)
-class GraphController < ApplicationController
+# Selectricity: Voting Machinery for the Masses
+# Copyright (C) 2007, 2008 Benjamin Mako Hill <mako@atdot.cc>
+# Copyright (C) 2007 Massachusetts Institute of Technology
+#
+# This program is free software. Please see the COPYING file for
+# details.
 
-  # produce a graph of votes per day during an election
-  def votes_per_day
+require 'date'
+class GraphController < ApplicationController  
+  class GruffGraff
     
-    @election = Election.find(params[:id])
-    data, labels = get_votes_per_day_data(@election)
+    COLORS = ['#74CE00', '#005CD9', '#DC0D13', '#131313', '#A214A4', '#EFF80E',
+              '#90E5E6', '#F58313', '#437D3D', '#0E026C']
+    BACKGROUND_COLORS = ['#74CE00', '#FFFFFF'] #for green and white background
+    
+    def initialize(options)
+      size = options[:size] ? options[:size] : "400x300" #allow custom sizing
+      @graph = options[:graph_type].new(size)
+      
+      @graph.no_data_message = "No Voters"
+      
+      @graph.theme = { :colors => COLORS,
+                       :background_colors => ['#e5e5e5', '#FFFFFF']  }
+      @graph.font = File.expand_path('/usr/X11R6/lib/X11/fonts/TTF/Vera.ttf',
+                                   RAILS_ROOT)
+      
+      if options[:legend_font_size]
+        @graph.legend_font_size = options[:legend_font_size] 
+      end
+      
+      if options[:title_font_size]  
+        @graph.title_font_size = options[:title_font_size]
+      end
+      
+      #marker count doesn't include minimum value line, default is 4
+      @graph.marker_count = options[:marker_count] if options[:marker_count]
+      
+      @graph.marker_font_size = options[:marker_font_size] if options[:marker_font_size]
+      
+      @graph.marker_color = options[:marker_color] if options[:marker_color]
+      
+      # fill in the data with the optional data name
+      #Check to see if multiple datasets, if so, fill them all!
+      #Sort by biggest first piece of data.
+      if options[:data].is_a?(Hash) 
+        options[:data].sort {|a,b| b[1][0] <=> a[1][0]}.each do |dataset|
+          @graph.data(dataset[0], dataset[1])
+        end
+      #if each dataset nameless, will have only multiple arrays    
+      elsif options[:data].size > 1 && options[:data].all?  {|i| i.is_a?(Array)}
+        options[:data].each do |array|
+          @graph.data( options.fetch(:data_name, "Data"), array)
+        end
+      else #one dimensional array, just pass it in
+      @graph.data( options.fetch(:data_name, "Data"), options[:data] )
+      @graph.hide_legend = true
+      end
+      
+      # set the labels or create an empty hash
+      @graph.labels = options[:interval_labels] \
+        if options.has_key?(:interval_labels) and \
+           options[:interval_labels].class == Hash
+      @graph.x_axis_label = options[:x_axis_label] \
+        if options.has_key?(:x_axis_label)
+      @graph.y_axis_label = options[:y_axis_label] \
+        if options.has_key?(:y_axis_label)
+      @graph.title = options[:title] if options.has_key?(:title)
+      
+      @graph.minimum_value = 0.0
 
-    line = Gruff::Line.new
-    line.title = "Voters Per Day"
-    line.font = File.expand_path('/usr/X11R6/lib/X11/fonts/TTF/Vera.ttf',
-                                 RAILS_ROOT)
+    end
 
-    line.data("#{@election.name}", data )
-    line.labels = labels
+    def output
+      return([@graph.to_blob, {:disposition => 'inline', :type => 'image/png'}])
+    end
 
-    line.x_axis_label = "Date"
-    line.y_axis_label = "Number of Votes"
-    line.minimum_value = 0.0
+  end
 
-    line.draw
-    send_data(line.to_blob, :disposition => 'inline', :type => 'image/png')
+  # 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
+    size = "400x300"
+    size = "520x300" if @election.candidates.size >= 8
+
+   if @election.candidates.size >= 8
+     marker_font_size = 20
+     legend_font_size = 17
+   else
+     marker_font_size = 17
+     legend_font_size = 17
+   end
  
+    pie = GruffGraff.new( :graph_type => Gruff::Pie,
+                           :title => "Percentage of First Place Votes",
+                           :size => size,
+                           :marker_font_size => marker_font_size,
+                          :legend_font_size => legend_font_size,
+                           :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   
+  end
+   
   # generate the data and labels for each graph
   def get_votes_per_day_data(election)
-
-    voter_times = Array.new
+    voter_days = Array.new
     unique_days = Array.new
-    voters_per_day = Array.new
-    days_hash = Hash.new
+    total_per_day = Array.new
+    election_days = Hash.new
+    
+    #turn election startdate into date object, and create the range of election
+    startdate = Date.parse(election.startdate.to_s)
+    election_range = startdate..Date.today
+    
+    # create a hash with all the dates of the election in String format
+    # referenced by their order in the election
+    election_range.each_with_index do |day, index|
+      election_days[index] = day.to_s
+    end
     
+    # Now I need to create an array with all the times votes were made
     election.votes.each do |vote|
-      unless voter_times.any? {|utime| utime == vote.time}
-        voter_times << vote.time
-      end
+      next unless vote.time
+      voter_days << Date.parse(vote.time.to_s)
     end
+    voter_days.sort!
     
-    voter_times.sort!
-  
-    voter_times.each_with_index do |time, index| 
-      count = 1
-      # TODO: add comment
-      unless unique_days.any? { |d1| d1.eql?(time.mon.to_s + "/" + time.day.to_s) }
-        unique_days << (time.mon.to_s + "/" + time.day.to_s)
-        count += (voter_times[(index+1)..-1].find_all \
-          {|t| t.mon == time.mon && t.day == time.day}).size
-        voters_per_day << count
-      end      
+    # Now I need to count how many times each each date appears in voter_days,
+    # and put that number into a votes_per_day array, the 'data' for the graph    
+    #Create an array of unique days from voter_days
+    voter_days.each do |day|
+      unless unique_days.any? {|date| date.eql?(day)}
+        unique_days << day
+      end
     end
+    unique_days.sort!
     
-    unique_days.each_with_index do |fmtdate, index|
-      days_hash[index] = fmtdate
+    #find all dates where those days = date at current index, put size of returned
+    #array into total_per_day
+    unique_days.each_with_index do |date, index|
+      total_per_day << (voter_days.select {|day| day.eql?(date)}).size
     end    
 
     # return the data and the labels
-    return voters_per_day, days_hash
+    return total_per_day, election_days
    
   end
+  
+  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|
+      next unless vote.time
+      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] = "Start"
+      labels_hash[(numcols/2)-1] = fmt_decimal((timedelta/120)) #halfway
+      labels_hash[numcols-1] = fmt_decimal((timedelta/60))
+      interval_type = "Minutes After Start"
+    elsif timedelta < 2.days #more than 2 hours means use hours for labels
+      labels_hash[0] = "Start"
+      labels_hash[(numcols/2)-1] = fmt_decimal((timedelta/7200))
+      labels_hash[numcols-1] = fmt_decimal((timedelta/3600))
+      interval_type = "Hours After Start (Up to 48)"
+    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 fmt_decimal(number)
+    sprintf( "%0.1f", number)
+  end
+  
+  def get_borda_points(result)
+    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.points.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
 end

Benjamin Mako Hill || Want to submit a patch?