lib/lilygraph.rb in lilygraph-0.2.5 vs lib/lilygraph.rb in lilygraph-0.3.0

- old
+ new

@@ -2,11 +2,11 @@ # # Author:: Christopher Giroir <kelsin@valefor.com> require 'color' -# This is the main graph to use if you want to create a graph! +# This is the main class to use if you want to create a graph! # # graph = Lilygraph.new(:title => "My Awesome Graph") # graph.data = [1,2,3] # graph.labels = ['One','Two','Three'] # graph.render @@ -37,10 +37,32 @@ # For a simple bar graph: # graph.data=[1,2,3] # For a grouped bar graph: # graph.data=[[1,10],[2,20],[3,30]] attr_accessor :data + + # This allows you to set colors for the bars. + # + # If you just want a single color: + # graph.colors='#0000aa' + # If you want to make each bar (or bar group) different colors: + # graph.colors=['#aa0000','#00aa00','#0000aa'] + # If you want every bar group to be the same, but each bar in the groups to have a different color + # graph.colors=[['#aa0000','#00aa00','#0000aa']] + # If you want to set every bar group color: + # graph.colors=[['#aa0000','#00aa00','#0000aa'],['#bb0000','#00bb00','#0000bb']] + # Last but not least you can set the color value to any object that responds to call (like a Proc). The proc takes four arguments. + # data_index: The index of the current bar (or group) + # number_index: The index of the current bar INSIDE of the current bar group (always 0 if you don't have grouped bars) + # data_size: total number of bar or groups. + # number_size: total number of bars in the current group (always 1 if you don't have bar groups) + # + # The default proc looks like: + # graph.colors=Proc.new do |data_index, number_index, data_size, number_size| + # Color::HSL.from_fraction(Float(data_index) / Float(data_size), 1.0, 0.4 + (Float(number_index) / Float(number_size) * 0.4)).to_rgb.html + # end + attr_accessor :colors # Returns a new graph creator with some default options specified via a hash: # height:: String to use as height parameter on the svg tag. Default is <tt>'100%'</tt>. # width:: String to use as width parameter on the svg tag. Default is <tt>'100%'</tt>. # indent:: Indent option to the XmlMarkup object. Defaults to <tt>2</tt>. @@ -54,10 +76,14 @@ # graph = Lilygraph.new(:title => 'Testing a title', :indent => 4) def initialize(options = {}) @options = DEFAULT_OPTIONS.merge(options) @data = [] @labels = [] + + @colors = Proc.new do |data_index, number_index, data_size, number_size| + Color::HSL.from_fraction(Float(data_index) / Float(data_size), 1.0, 0.4 + (Float(number_index) / Float(number_size) * 0.4)).html + end end # Updates the graph options with items from the passed in hash. Please refer # to new for a description of available options. def update_options(options = {}) @@ -140,16 +166,29 @@ x = (@options[:margin][:left] + (dx * data_index)).round # Rectangles data.each_with_index do |number, number_index| - color = Color::HSL.from_fraction(data_index * (1.0 / @data.size),1.0, 0.4 + (number_index * 0.2)).to_rgb + color = if @colors.respond_to? :call + @colors.call(data_index, number_index, @data.size, data.size) + elsif @colors.class == Array + first = @colors[data_index % (@colors.size)] + + if first.class == Array + first[number_index % (first.size)] + else + first + end + else + @colors + end + height = ((dy / 10.0) * number).round bar_x = (x + ((dx - width) / 2.0) + (number_index * bar_width)).round bar_y = @options[:viewbox][:height] - @options[:margin][:bottom] - height - xml.rect :fill => color.html, :stroke => color.html, 'stroke-width' => 0, :x => bar_x, :width => bar_width, :y => bar_y, :height => height - 1 + xml.rect :fill => color, :stroke => color, 'stroke-width' => 0, :x => bar_x, :width => bar_width, :y => bar_y, :height => height - 1 end # Text if @options[:bar_text] data.each_with_index do |number, number_index|