lib/sparklines.rb in sparklines-0.4.7 vs lib/sparklines.rb in sparklines-0.4.8

- old
+ new

@@ -72,11 +72,11 @@ Licensed under the MIT license. =end class Sparklines - VERSION = '0.4.7' + VERSION = '0.4.8' @@label_margin = 5.0 @@pointsize = 10.0 class << self @@ -119,11 +119,11 @@ options_sym = defaults.merge(options_sym) # Call the appropriate method for actual plotting. sparkline = self.new(data, options_sym) - if %w(area bar pie smooth discrete whisker).include? options_sym[:type] + if %w(area bar bullet pie smooth discrete whisker).include? options_sym[:type] sparkline.send options_sym[:type] else sparkline.plot_error options_sym end end @@ -496,10 +496,47 @@ @draw.draw(@canvas) @canvas.to_blob end + def bullet + height = @options[:height].to_f + @graph_width = @options.has_key?(:width) ? @options[:width].to_f : 100.0 + background_color = '#eeeeee' # @options[:background_color] + @thickness = height/3.0 + + create_canvas(@graph_width, height, background_color) + + @value = @norm_data + @good_value = @options[:good].to_f + + @graph_height = @options[:height] + + qualitative_range_colors = ['#bbbbbb', '#999999'] + [:satisfactory, :bad].each_with_index do |indicator, index| + next unless @options.has_key?(indicator) + @draw = @draw.fill(qualitative_range_colors[index]) + indicator_width_x = @graph_width * (@options[indicator].to_f / @good_value) + @draw = @draw.rectangle(0, 0, indicator_width_x.to_i, @graph_height) + end + + if @options.has_key?(:target) + @draw = @draw.fill 'black' + target_x = @graph_width * (@options[:target].to_f / @good_value) + half_thickness = (@thickness / 2.0).to_i + bar_width = 1.0 + @draw = @draw.rectangle(target_x.to_i, half_thickness, (target_x + bar_width).to_i, @thickness * 2 + half_thickness) + end + + # Value + @draw = @draw.fill 'black' + @draw = @draw.rectangle(0, @thickness.to_i, @graph_width * (@data.first.to_f / @good_value), (@thickness * 2.0).to_i) + + @draw.draw(@canvas) + @canvas.to_blob + end + ## # Draw the error Sparkline. def plot_error(options={}) create_canvas(40, 15, 'white') @@ -515,12 +552,15 @@ private def normalize_data @minimum_value = @data.min @maximum_value = @data.max - if @options[:type].to_s == 'pie' + case @options[:type].to_s + when 'pie' @norm_data = @data + when 'bullet' + @norm_data = @data else @norm_data = @data.map do |value| value = ((value.to_f - @minimum_value)/(@maximum_value - @minimum_value)) * 100.0 end end @@ -658,8 +698,7 @@ # accepts: an array, the population # returns: the standard deviation def standard_deviation(population) Math.sqrt(variance(population)) end - end