+class GraphController < ApplicationController
+ class GruffGraff
+
+ 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
+
+ end
+
+ def output
+ return([@graph.to_blob, {:disposition => 'inline', :type => 'image/png'}])
+ end
+
+ end
+