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 \
@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] = []
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
# 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,