1 require File.dirname(__FILE__) + '/base'
7 class Gruff::PhotoBar < Gruff::Base
11 # define base and cap in yml
12 # allow for image directory to be located elsewhere
13 # more exact measurements for bar heights (go all the way to the bottom of the graph)
14 # option to tile images instead of use a single image
15 # drop base label a few px lower so photo bar graphs can have a base dropping over the lower marker line
18 # The name of a pre-packaged photo-based theme.
21 # def initialize(target_width=800)
23 # init_photo_bar_graphics()
28 return unless @has_data
30 return # TODO Remove for further development
32 init_photo_bar_graphics()
34 #Draw#define_clip_path()
35 #Draw#clip_path(pathname)
36 #Draw#composite....with bar graph image OverCompositeOp
40 # Draw.pattern # define an image to tile as the filling of a draw object
45 # Columns sit side-by-side.
47 @bar_width = @norm_data[0][DATA_COLOR_INDEX].columns
49 @norm_data.each_with_index do |data_row, row_index|
51 data_row[1].each_with_index do |data_point, point_index|
52 data_point = 0 if data_point.nil?
53 # Use incremented x and scaled y
54 left_x = @graph_left + (@bar_width * (row_index + point_index + ((@data.length - 1) * point_index)))
55 left_y = @graph_top + (@graph_height - data_point * @graph_height) + 1
56 right_x = left_x + @bar_width * spacing_factor
57 right_y = @graph_top + @graph_height - 1
59 bar_image_width = data_row[DATA_COLOR_INDEX].columns
60 bar_image_height = right_y.to_f - left_y.to_f
62 # Crop to scale for data
63 bar_image = data_row[DATA_COLOR_INDEX].crop(0, 0, bar_image_width, bar_image_height)
65 @d.gravity = NorthWestGravity
66 @d = @d.composite(left_x, left_y, bar_image_width, bar_image_height, bar_image)
68 # Calculate center based on bar_width and current row
69 label_center = @graph_left + (@data.length * @bar_width * point_index) + (@data.length * @bar_width / 2.0)
70 draw_label(label_center, point_index)
79 # Return the chosen theme or the default
86 # Sets up colors with a list of images that will be used.
87 # Images should be 340px tall
88 def init_photo_bar_graphics
89 color_list = Array.new
90 theme_dir = File.dirname(__FILE__) + '/../../assets/' + theme
92 Dir.open(theme_dir).each do |file|
93 next unless /\.png$/.match(file)
94 color_list << Image.read("#{theme_dir}/#{file}").first