From: Date: Tue, 14 Aug 2007 09:25:59 +0000 (-0400) Subject: Fixed choices_positions to use GruffGraff class. Initialize method now had a way of X-Git-Url: https://projects.mako.cc/source/selectricity/commitdiff_plain/82e34dc91e6c198a2fb46991f66ce81520100862?hp=45d47676ca3cc1c19b457f83a9482ba8b906f829 Fixed choices_positions to use GruffGraff class. Initialize method now had a way of dealign with multiple data arrays being passed in. Next need to compensate for giving each set of data it's own name, possible a hash with "name" => data as the key, value pairs... --- diff --git a/app/controllers/graph_controller.rb b/app/controllers/graph_controller.rb index 17e1578..8256884 100644 --- a/app/controllers/graph_controller.rb +++ b/app/controllers/graph_controller.rb @@ -12,8 +12,15 @@ class GraphController < ApplicationController RAILS_ROOT) # fill in the data with the optional data name + #Check to see if multiple datasets, if so, fill them all! + if 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] ) - + end + # set the labels or create an empty hash @graph.labels = options[:interval_labels] \ if options.has_key?(:interval_labels) and \ @@ -85,44 +92,24 @@ class GraphController < ApplicationController @election = Election.find(params[:id]) pref_tally = make_preference_tally(@election) - fulldata = get_positions_info(@election) - (0...@election.candidates.size).each do |i| - sbar.data("Candidate#{i+1}", fulldata[i]) - end - - sbar = Gruff::Bar.new - sbar.title = "Times Voted in Each Position" - #sbar.theme = { :background_colors => ['#73BF26', '#ffffff'] } - sbar.font = File.expand_path('/usr/X11R6/lib/X11/fonts/TTF/Vera.ttf', - RAILS_ROOT) - + fulldata, labels = get_positions_info(@election) + labels = @candidates graph = GruffGraff.new( :graph_type => Gruff::Bar, :data_name => @election.name, - :data => data, + :data => fulldata, :interval_labels => labels, - :title => "Times Candidate Was Voted Each Position", - :x_axis_label => "Rank", - :y_axis_label => "Number of Times") - - - - - sbar.x_axis_label = "Rank" - sbar.y_axis_label = "Number of Times Ranked" - - sbar.minimum_value = 0.0 - send_data(sbar.to_blob, :disposition => 'inline', :type => 'image/png') - - - #data, labels = get_positions_info(@election) - + :title => "Times Voted in Each Position", + :y_axis_label => "Number of Times Ranked", + :x_axis_label => "Rank") + send_data(*graph.output) end + private def get_positions_info(election) buckets = Hash.new buckets2= Hash.new - ranks = Hash.new + rank_labels = Hash.new election.candidates.each do |candidate| buckets[candidate.id] = [] @@ -141,7 +128,13 @@ class GraphController < ApplicationController end end - return buckets2.values + election.votes.each do |vote| + vote.rankings.size.times do |i| + rank_labels[i] = (i+1).to_s + end + end + + return buckets2.values, rank_labels end @@ -202,7 +195,7 @@ class GraphController < ApplicationController # 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 0 up till numcols + # 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, diff --git a/app/views/quickvote/results.rhtml b/app/views/quickvote/results.rhtml index 33bb038..0c53367 100644 --- a/app/views/quickvote/results.rhtml +++ b/app/views/quickvote/results.rhtml @@ -164,6 +164,6 @@ by several other names.

<%= image_tag( graph_url( :action => 'votes_per_day', :id => @election ) ) %>
<%= image_tag( graph_url( :action => 'votes_per_interval', :id => @election ))%>
-<%= image_tag( graph_url( :action => 'borda_bar', :id => @election ) ) %> +<%= image_tag( graph_url( :action => 'borda_bar', :id => @election ) ) %>
<%= image_tag( graph_url( :action => 'choices_positions', :id => @election ) ) %>