Added the Gruff library to the lib/ directory of the the rails folder, and the
[selectricity] / lib / gruff-0.2.8 / lib / gruff / photo_bar.rb
1 require File.dirname(__FILE__) + '/base'
2
3 # EXPERIMENTAL!
4 #
5 # Doesn't work yet.
6 #
7 class Gruff::PhotoBar < Gruff::Base
8
9 # TODO
10 #
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
16 #
17
18   # The name of a pre-packaged photo-based theme.
19   attr_reader :theme
20
21 #   def initialize(target_width=800)
22 #     super
23 #     init_photo_bar_graphics()
24 #   end
25
26   def draw
27     super
28     return unless @has_data
29
30     return # TODO Remove for further development
31
32     init_photo_bar_graphics()
33     
34     #Draw#define_clip_path()
35     #Draw#clip_path(pathname)
36     #Draw#composite....with bar graph image OverCompositeOp
37     #
38     # See also
39     #
40     # Draw.pattern # define an image to tile as the filling of a draw object
41     # 
42
43     # Setup spacing.
44     #
45     # Columns sit side-by-side.
46     spacing_factor = 0.9
47     @bar_width = @norm_data[0][DATA_COLOR_INDEX].columns
48
49     @norm_data.each_with_index do |data_row, row_index|
50   
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
58       
59         bar_image_width = data_row[DATA_COLOR_INDEX].columns
60         bar_image_height = right_y.to_f - left_y.to_f
61       
62         # Crop to scale for data
63         bar_image = data_row[DATA_COLOR_INDEX].crop(0, 0, bar_image_width, bar_image_height)
64         
65         @d.gravity = NorthWestGravity
66         @d = @d.composite(left_x, left_y, bar_image_width, bar_image_height, bar_image)
67       
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)
71       end
72
73     end
74
75     @d.draw(@base_image)    
76   end
77
78
79   # Return the chosen theme or the default
80   def theme
81     @theme || 'plastik'
82   end
83
84 protected
85
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
91
92     Dir.open(theme_dir).each do |file|
93       next unless /\.png$/.match(file)
94       color_list << Image.read("#{theme_dir}/#{file}").first
95     end
96     @colors = color_list
97   end
98
99 end
100

Benjamin Mako Hill || Want to submit a patch?